Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 6 down vote accepted

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.

share|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. –  Dennis Williamson 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
share|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 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:



share|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 posting your answer, you agree to the privacy policy and terms of service.