Linux: Excluding files based on extension and age with tar

If you are using tar to archive log files and find yourself needing to exclude certain extensions or older log files, tar can easily handle those type of situations.

For example, to create a tar that excludes any rolled over logs that are zipped, use an ‘exclude’ like below:

tar cvf mylogarchive.tar --exclude=*.gz --exclude=*.tgz *

To take only files that have been modified in the last day.

tar cvf mylogarchive.tar --newer-mtime '1 day ago' *

Other date specifications are available.

And if you need more control over file and folder exclusions, you can use ‘find’ to create the list of included and excluded files and use that as input of which files to tar.

find . -name -not -name "excludeme.txt" -not -path "*secretdir/*" | tar cfv mylogarchive.tar -T -

Would tar all files except the file named “excludeme.txt” in the root path and would exclude all files in the “secretdir” directory.

REFERENCES

https://linux.die.net/man/1/tar (man)

https://www.gnu.org/software/tar/manual/html_section/tar_52.html (tar dates)

stackoverflow, find files then tar

NOTES

Exclude special .git folder, files in all levels of dir ‘mydir’, files in ‘bin’ directory, files and folder ‘otherdir’.

tar cfvz my.tgz --exclude=mydir/** --exclude=otherdir --exclude=**/bin/* --exclude=.git indir/*

Find list of files and then tar them up

find . -type f -print0 | tar czvf /tmp/backup.tgz --null -T -