Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude files from tar gzipping a directory in unix

Tags:

unix

gzip

tar

I have a directory (dir) (with files and subdirectories):

ls -1 dir
plot.pdf
subdir.1
subdir.2
obj.RDS

And then ls -1 for either subdir.1 or subdir.2:

plot.pdf
PC.pdf
results.csv
de.pdf
de.csv
de.RDS

I would like to tar and gzip dir (in unix) and I'd like to exclude all RDS files (the the level right below dir and the ones in its subdirectories).

What's the easiest way to achieve that? Perhaps in a one liner

like image 985
dan Avatar asked Dec 11 '25 04:12

dan


2 Answers

Something like:

find dir -type f -not -name '*.RDS' -print0 | 
              tar --null -T- -czf TARGET.tgz

should do it.

First, find finds the files, and then tar accepts the list via -T- (= --files-from /dev/stdin).

-print0 on find combined wth --null on tar protect from weird filenames.

-czf == Create gZipped File

You can add v to get verbose output.

To later inspect the contents, you can do:

tar tf TARGET.tgz
like image 168
PSkocik Avatar answered Dec 13 '25 08:12

PSkocik


tar --exclude=*.RDS -Jcf outputball.tar  dir_to_compress

this will ignore *.RDS across any dir or subdirs

decompress using

tar -xvf outputball.tar
like image 36
Scott Stensland Avatar answered Dec 13 '25 07:12

Scott Stensland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!