backup and remove files with access time older than 5 days.

tar -zcvpf backup_`date +"%Y%m%d_%H%M%S"`.tar.gz `find <target> -atime +5` 2> /dev/null | xargs rm -fr ;
create an archive of files with access time older than 5 days, and remove original files.

7
By: angleto
2009-05-26 17:15:52

1 Alternatives + Submit Alt

What Others Think

This command is dangerous because of xargs: mkdir a echo foo > 'a/dont keep_this' echo foo > 'keep_this' # wait 5 days tar -zcvpf backup_`date +"%Y%m%d_%H%M%S"`.tar.gz `find a -atime +5` 2> /dev/null | xargs rm -fr ; This will remove the file keep_this and will not backup 'dont keep_this'.
unixmonkey8046 · 585 weeks and 2 days ago
Hi unixmonkey8046, thanks for your comment. Note that with special char also tar will have some problem, in fact in your example you will also back up the file 'keep_this' created outside of the directory 'a'. You need to take care in any case your files have some special character. The problem is not xargs, you can quote the strings which you pass to it.
angleto · 585 weeks and 2 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: