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.

| improve this answer | |
  • Oh, and adding --remove-files to the tar command WOULD remove the files from disk if that's what you're looking to do. – djhowell Oct 1 '09 at 17:11
  • This will overwrite the tar file each time it is run. This may or may not be desired. – Paused until further notice. Oct 1 '09 at 17:18
  • It didn't in my testing. I did a "touch 1 2 3" ran the command, did a "touch 4 5 6" and ran it again and the archive contained 1 2 3 4 5 6. – djhowell Oct 1 '09 at 17:24
  • Oh, but if you use the --remove-files option then yes, it would just contain "4 5 6". In which case you should change -cvf to -Avf to appeand. – djhowell Oct 1 '09 at 17:26
  • What happens if there are so many args that xargs runs tar twice? Better to use 'r' instead of 'c' in the tar command. Create an empty tar file before running the tar command, and then update it. devdaily.com/blog/post/linux-unix/… – steveha Oct 1 '09 at 18:26

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
| improve this answer | |
  • If find gives a huge number of files, then: find dirname -type f -mtime +50 |tar uvf archive.tar -T - – MaximKostrikin Dec 1 '14 at 15:24

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:



| improve this answer | |
  • Warning: 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 1 '09 at 17:06
  • FYI, the second link I posted is a long discussion of this very point. – steveha Oct 1 '09 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy