I have a directory which I would like to scan through and add each file older than 50 days to a new archive named archive.tar
All files older than 50 days must be in one big tar not a tar for each file.
How can I do this?
I have a directory which I would like to scan through and add each file older than 50 days to a new archive named archive.tar
All files older than 50 days must be in one big tar not a tar for each file.
How can I do this?
This will do the trick:
# find /path/to/files -type f -mtime +50 | xargs tar cvf archive.tar
You can stick it in crontab and have it run daily.
Edit: Remember that this will not remove the files from the system, only add them to the archive.
To append to the archive, use the r option rather than c:
find dirname -type f -mtime +50 | xargs tar rvf archive.tar
To only append the files if they are newer than the copy that's already in the archive:
find dirname -type f -mtime +50 | xargs tar uvf archive.tar
find dirname -type f -mtime +50 |tar uvf archive.tar -T -
Use the find command to find files older than 50 days, and have the find command run tar to append the found file(s) to the tar. For performance improvement, it is common to have the output of the find command pass to the xargs program.
I did a Google search on "find tar xargs" and here are two good links:
http://content.hccfl.edu/pollock/Unix/FindCmd.htm
http://www.devdaily.com/blog/post/linux-unix/using-find-xargs-tar-create-huge-archive-cygwin-linux-unix
xargs can split a long list of arguments into multiple commands. If you use tar's c (create), you might end up running it multiple times, each time overwriting the original. To be safe, it probably makes sense to use tar's r (update) command. You might need to create an empty tar file to prime it, though.
– wfaulk
Oct 01 '09 at 17:06
tarfile each time it is run. This may or may not be desired. – Dennis Williamson Oct 01 '09 at 17:18