I have a directory with many sub-directories. In some of those sub-directories I have files with *.asc
extension and some with *.xdr
.
I want to create a SINGLE tarball/gzip file which maintains the directory structure but excludes all files with the *.xdr
extension.
How can I do this?
I did something like find . -depth -name *.asc -exec gzip -r9 {} +
but this gzips every *.asc
file individually which is not what I want to do.
To do so, create a text file with the name of the files and directories you want to exclude. Then, pass the name of the file to the --exlude-from option.
For that, you need to make use of the “tar” command with the “-xvf” option and the name of a “tar” file while we are currently located at the “Downloads” folder. The option “x” is used for extraction, “-v” is used to display in ascending order, and “-f” is used to perform the extraction forcefully.
Exclude Multiple Files/Directories from Zip Archive You can define -x multiple times in a single zip command to exclude multiple files and directories from zip archive.
You need to use the --exclude
option:
tar -zc -f test.tar.gz --exclude='*.xdr' *
gzip will always handle files individually. If you want a bundled archive you will have to tar the files first and then gzip the result, hence you will end up with a .tar.gz file or .tgz for short.
To get a better control over what you are doing, you can first find
the files using the command you already posted (with -print instead of the gzip command) and put them into a file, then use this file (=filelist.txt) to instruct tar with what to archive
tar -T filelist.txt -c -v -f myarchive.tar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With