Tons of threads out there, but nothing with that includes the entire hat trick. I'm trying to tar
all files newer than 2019-06-30 (±760 days) in a directory tree, but ignore any directories with the name backup
. I've looked at dozens of options and this is a close as I've gotten:
tar -cvzf newest-files.tar.gz --newer-mtime "760 days ago" client_images/ | grep -v 'backup'
This one does the proper filtering (date and ignore) but doesn't tar anything (empty archive
error):
sudo find client_images/. -mtime -760 | grep -v 'backup'
Can't figure out to get the tar
happening.
| tar -cvzf files.tar.gz
What am I missing?
Thanks.
UPDATE:
This came from my system admin (who wishes to remain anonymous):
This command line will run the tar command for every file it finds, create (-c) a new tar archive (always called newestfiles.tar.gz) for it and then tar and compress the file into that tar archive. So at the end you're left with one tar archive that contains the last file the "find" command found, all previous tar archives were overwritten (since it's always the same name).
To prevent this from happening you need to use the add (-r) instead of the create (-c) tar option. Unfortunately this doesn't work with compression in one command, so that has to happen in a separate step. And if you don't need compression, you can do it with just the one. So,
$ find client_images -type f -mtime -760 -exec tar -rvf newestfiles.tar
--exclude '*mcith'* {} \;
$ gzip newestfiles.tar