Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tar/gzip excluding certain files

Tags:

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.

like image 603
user1654183 Avatar asked Aug 01 '13 15:08

user1654183


People also ask

How do I exclude a file in Linux?

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.

How do I extract one file from a tar file?

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.

How do I exclude a zip file in Linux?

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.


2 Answers

You need to use the --exclude option:

tar -zc -f test.tar.gz --exclude='*.xdr' *

like image 145
Allolex Avatar answered Sep 22 '22 02:09

Allolex


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 
like image 25
Carsten Massmann Avatar answered Sep 20 '22 02:09

Carsten Massmann