0

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 

2 Answers 2

0

Try

find client_images -type f -mtime -760 -print0 | tar cvzf files.tar.gz --files-from=- --null --exclude=\*backup\*

This should instruct find to only match files, not directories, and output the names NUL-separated instead of lf-separated (ensuring strange filenames are handled safely). The reason is that tar will recurse down into directories on its own if given the chance.

The added options to tar ensure that the list of files to archive is read from stdin (--files-from=-), and are treated as NUL-separated (--null). --exlude will prevent it from archiving the unwanted files.

Oh, and I’m typing this on an ipad in bed, so this is all untested and may not work.

0
-1

Before 2005, the method you are trying to use was appropriate, but since 2005, there is libfind and as a result, it is possible to integrate the features from find(1) into any program where it may help.

So please just try something like:

star -cz -f newest-files.tar.gz  -find client_images -mtime -760 ! -path '*backup*'

star is part of the schilytols and available as binary packet for every well maintained distribution.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.