View previous topic :: View next topic |
Author |
Message |
grant123 Veteran
Joined: 23 Mar 2005 Posts: 1080
|
Posted: Sat Dec 29, 2012 7:52 pm Post subject: Delete files 6 months old or older? |
|
|
Is there a 1-liner I can issue from the command line that will delete files 6 months old or older in a particular directory? |
|
Back to top |
|
|
platojones Veteran
Joined: 23 Oct 2002 Posts: 1602 Location: Just over the horizon
|
Posted: Sat Dec 29, 2012 8:08 pm Post subject: Re: Delete files 6 months old or older? |
|
|
grant123 wrote: | Is there a 1-liner I can issue from the command line that will delete files 6 months old or older in a particular directory? |
Sure, here's one:
Code: | find /directory/path -mtime +180 | xargs rm -f |
|
|
Back to top |
|
|
aCOSwt Bodhisattva
Joined: 19 Oct 2007 Posts: 2537 Location: Hilbert space
|
Posted: Sat Dec 29, 2012 8:19 pm Post subject: |
|
|
Of course platojones is correct.
Just in order to join the competition for fun, I suggest Code: | find /directory/path -mtime +180 -delete |
_________________
|
|
Back to top |
|
|
toralf Developer
Joined: 01 Feb 2004 Posts: 3921 Location: Hamburg
|
Posted: Sat Dec 29, 2012 10:25 pm Post subject: Re: Delete files 6 months old or older? |
|
|
platojones wrote: |
Sure, here's one:
Code: | find /directory/path -mtime +180 | xargs rm -f |
| Sure, that this will work (as *expected* I mean) with filenames like " * " ? |
|
Back to top |
|
|
platojones Veteran
Joined: 23 Oct 2002 Posts: 1602 Location: Just over the horizon
|
Posted: Sat Dec 29, 2012 10:45 pm Post subject: Re: Delete files 6 months old or older? |
|
|
toralf wrote: | platojones wrote: |
Sure, here's one:
Code: | find /directory/path -mtime +180 | xargs rm -f |
| Sure, that this will work (as *expected* I mean) with filenames like " * " ? |
Not sure if it will work with all 'pattern' filenames, but it does work with "*".
I never knew about the -delete option ACOSWT mentioned though...so that's pretty slick.
I would add one more option for correctness though, to both of those...it should probably be restricted to 'file' types only. I noticed find command finds the target directory as well, if it's within the time range...so it would probably be smart to restrict the command to files only, if that's what you want:
find /directory/path -mtime +180 -type f -delete
EDIT: Not sure about '*'...hard to test what impact that has on accidentally removing all files. |
|
Back to top |
|
|
frostschutz Advocate
Joined: 22 Feb 2005 Posts: 2977 Location: Germany
|
Posted: Sat Dec 29, 2012 11:30 pm Post subject: |
|
|
In this particular case you can use -delete (but please, make a print run first to verify you got your find conditions right!), for many other cases you can use -exec cmd ; or -exec cmd + (see the manpage).
If you ever need to pass on find output to xargs, the preferred method is Code: | find ... -print0 | xargs -0 ... | as that way you have special characters, spaces in filenames, etc. handled properly. |
|
Back to top |
|
|
russK l33t
Joined: 27 Jun 2006 Posts: 665
|
Posted: Sun Dec 30, 2012 5:50 pm Post subject: |
|
|
If you only want to delete files you should add "-type f" to your find command |
|
Back to top |
|
|
grant123 Veteran
Joined: 23 Mar 2005 Posts: 1080
|
Posted: Thu Jan 03, 2013 9:43 pm Post subject: |
|
|
Thanks everyone, I'm using:
Code: | find /directory/path -type f -mtime +180 -delete |
Can I consolidate these two lines into one?
Code: | find /directory/path -type f -mtime +3 -name "1-*.jpg" -delete
find /directory/path -type f -mtime +3 -name "2-*.jpg" -delete |
|
|
Back to top |
|
|
randalla Tux's lil' helper
Joined: 14 Oct 2008 Posts: 79 Location: Seattle, WA
|
Posted: Sat Jan 05, 2013 8:09 am Post subject: |
|
|
grant123 wrote: | Thanks everyone, I'm using:
Code: | find /directory/path -type f -mtime +180 -delete |
Can I consolidate these two lines into one?
Code: | find /directory/path -type f -mtime +3 -name "1-*.jpg" -delete
find /directory/path -type f -mtime +3 -name "2-*.jpg" -delete |
|
This should do what you want:
Code: | find /directory/path -type f -mtime +180 \( -name "1-*.jpg" -o -name "2-*.jpg" \) -delete |
Also, if you ever need to, you can also do goofy stuff like this:
Code: | find /directory/path -type f -mtime +180 \( -name "1-*.jpg" -o -name "2-*.jpg" \) | while read F; do rm -fv "$F"; done |
I only point that out because you can keep expanding out the actions using the semi-colon. You can get similar results with -exec off of find as well, but I don't like using it as much.
Adam. |
|
Back to top |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21497
|
Posted: Sat Jan 05, 2013 6:03 pm Post subject: |
|
|
randalla wrote: | Code: | find /directory/path -type f -mtime +180 \( -name "1-*.jpg" -o -name "2-*.jpg" \) | while read F; do rm -fv "$F"; done |
| This will misbehave in the presence of files with certain whitespace in the name. To avoid such problems, use only null terminated parsing. Use -print0 as your print predicate in find, and use read -d '' F in your read. The -print0 statement is a GNU find extension. |
|
Back to top |
|
|
grant123 Veteran
Joined: 23 Mar 2005 Posts: 1080
|
Posted: Sun Jan 06, 2013 3:51 am Post subject: |
|
|
Thanks guys. |
|
Back to top |
|
|
|