Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'find' with 'xargs' and 'tar'

Tags:

find

tar

xargs

I have the following I want to do:

find . -maxdepth 6 \( -name \*.tar.gz -o -name bediskmodel -o -name src -o -name ciao -o -name heasoft -o -name firefly -o -name starlink -o -name Chandra \) -prune -o -print | tar  cvf somefile.tar --files-from=-

I.e., exclude a whole lot of stuff, only look to six subdirectories depth, and then once pruning is done, 'tar' up the rest.

It is not hard. The bit before the pipe (|) works 100%. If I exclude the 'tar', then I get what I'm after (to the screen). But once I include the pipe, and the tar, it tars everything, including all the stuff I've just excluded in the 'find'.

I've tried a number of different iterations:

-print0 | xargs -0 tar rvf somefile.tar
-print0 | xargs -0 tar rvf somefile.tar --null --files-from=-
-print0 | tar cvf somefile.tar --null -T -

So what am I doing wrong? I've done this before; but now it's just giving me grey hairs.

like image 459
zinkeldonk Avatar asked Jul 18 '12 12:07

zinkeldonk


People also ask

How do you use find and tar together?

Use the find command to output path to whatever files you're looking for. Redirect stdout to a filename of your choosing. Then tar with the -T option which allows it to take a list of file locations (the one you just created with find!)


2 Answers

A combination of the -print flag for find, and then --files-from on the 'tar' command worked for me. In my case I needed to tar up 5000+ log files, but just using 'xargs' only gave me 500 files in the resulting file.

find . -name "*.pdf" -print | tar -czf pdfs.tar.gz --files-from -

You have "--files-from=-", when you just want "--files-from -" and then I think you need a - in front of cvf, like the following.

find . -maxdepth 6 ( -name *.tar.gz -o -name bediskmodel -o -name src -o -name ciao -o -name heasoft -o -name firefly -o -name starlink -o -name Chandra ) -prune -o -print| tar -cvf somefile.tar.gz --files-from -
like image 189
david.tanner Avatar answered Oct 20 '22 06:10

david.tanner


I remember doing something like the below line to 'tar' a bunch of files together. I was specific about the files I wanted to group, so I ran something like this:

find . -name "*.xyz" | xargs tar cvf xyz.tar;

In your case, I wonder why you are doing "-o" before the -print that seems to be including everything again.

like image 26
rajshenoy Avatar answered Oct 20 '22 04:10

rajshenoy