Backup files older than 1 day on /home/dir, gzip them, moving old file to a dated file.

find /home/dir -mtime +1 -print -exec gzip -9 {} \; -exec mv {}.gz {}_`date +%F`.gz \;
Useful for backing up old files, custom logs, etc. via a cronjob.
Sample Output
-rw-rw-r--    1 root     root        46705 Sep 13 14:30 file2.rpm
-rw-rw-r--    1 rms      rms         46705 Sep 13 14:30 file22.rpm

-rw-rw-r--    1 root     root        44227 Sep 13 14:30 file2.rpm_2009-09-15.gz
-rw-rw-r--    1 rms      rms         44228 Sep 13 14:30 file22.rpm_2009-09-15.gz

By: sagan
2009-09-15 06:49:59

What Others Think

Everyone forgets that find finds more than just files. You should specify the file type of 'f' for 'file'. find /home/dir -mtime +1 -type f -print -exec gzip -9 {} \; -exec mv {}.gz {}_`date +%F`.gz \;
pdxdoughnut · 604 weeks and 6 days ago
And if you don't want subdirectories to get the same treatment, put "-maxdepth 1" right before -mtime.
bwoodacre · 604 weeks and 6 days ago
If you don't want to backup mounted file systems in your home directory add -mount as an argument to the find command. find /home/dir -mount -mtime +1 -type f -print -exec gzip -9 {} \; -exec mv {}.gz {}_`date +%F`.gz \;
Trebacz · 554 weeks and 2 days ago
I would like to exclude some directories. I have multiple folders in my ftp directory. I would like to back up these folders and their subfolders individually. example /home/ftp /home/ftp/ben /home/ftp/ben2 and so on I want to exclude /home/ftp/export/ How do I back these up, I have around 100 folders each with 20+ folders
loreste · 416 weeks and 4 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? 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.


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: