Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Delete files 6 months old or older?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
grant123
Veteran
Veteran


Joined: 23 Mar 2005
Posts: 1080

PostPosted: Sat Dec 29, 2012 7:52 pm    Post subject: Delete files 6 months old or older? Reply with quote

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
View user's profile Send private message
platojones
Veteran
Veteran


Joined: 23 Oct 2002
Posts: 1602
Location: Just over the horizon

PostPosted: Sat Dec 29, 2012 8:08 pm    Post subject: Re: Delete files 6 months old or older? Reply with quote

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
View user's profile Send private message
aCOSwt
Bodhisattva
Bodhisattva


Joined: 19 Oct 2007
Posts: 2537
Location: Hilbert space

PostPosted: Sat Dec 29, 2012 8:19 pm    Post subject: Reply with quote

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
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3921
Location: Hamburg

PostPosted: Sat Dec 29, 2012 10:25 pm    Post subject: Re: Delete files 6 months old or older? Reply with quote

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
View user's profile Send private message
platojones
Veteran
Veteran


Joined: 23 Oct 2002
Posts: 1602
Location: Just over the horizon

PostPosted: Sat Dec 29, 2012 10:45 pm    Post subject: Re: Delete files 6 months old or older? Reply with quote

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
View user's profile Send private message
frostschutz
Advocate
Advocate


Joined: 22 Feb 2005
Posts: 2977
Location: Germany

PostPosted: Sat Dec 29, 2012 11:30 pm    Post subject: Reply with quote

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
View user's profile Send private message
russK
l33t
l33t


Joined: 27 Jun 2006
Posts: 665

PostPosted: Sun Dec 30, 2012 5:50 pm    Post subject: Reply with quote

If you only want to delete files you should add "-type f" to your find command
Back to top
View user's profile Send private message
grant123
Veteran
Veteran


Joined: 23 Mar 2005
Posts: 1080

PostPosted: Thu Jan 03, 2013 9:43 pm    Post subject: Reply with quote

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
View user's profile Send private message
randalla
Tux's lil' helper
Tux's lil' helper


Joined: 14 Oct 2008
Posts: 79
Location: Seattle, WA

PostPosted: Sat Jan 05, 2013 8:09 am    Post subject: Reply with quote

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
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21490

PostPosted: Sat Jan 05, 2013 6:03 pm    Post subject: Reply with quote

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
View user's profile Send private message
grant123
Veteran
Veteran


Joined: 23 Mar 2005
Posts: 1080

PostPosted: Sun Jan 06, 2013 3:51 am    Post subject: Reply with quote

Thanks guys.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum