1753797 Members
8121 Online
108805 Solutions
New Discussion юеВ

using tar with xargs

 
SOLVED
Go to solution
Gary Yu
Super Advisor

using tar with xargs

Hi all,

I'm trying to tar a big number of files in a directory(I know it's not a good practice to have so many files in a single directory), to avoid the "arg line too longs..." error, I'm going to use xargs with tar, like

$ ls | xargs tar cvf myarc.tar

xargs will break all files into small sets and feed to tar for several times, but the problem is, it seemed that the latest invoke of tar always over write the tar file generated of last time, and finally I got the "myarc.tar" file with only the last set of files, is there any work around here ?

thanks,
Gary
9 REPLIES 9
harry d brown jr
Honored Contributor

Re: using tar with xargs


I'm confused?

why not

tar -cvf myarc.tar .

??


live free or die
harry
Live Free or Die
James R. Ferguson
Acclaimed Contributor

Re: using tar with xargs

Hi Gary:

Specify a non-rewind tape device, explicitly as the destination (e.g. /dev/rmt/omn) or use the '-r' option of 'tar' to append to the archive. See the man pages for more information.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: using tar with xargs

For your application (tar) the better way to do this is:

tar cvf /mydir/myarc.tar .

One tar and tar does all the argument expansion for you via the . to tar the current directory (and any subdirs).
If it ain't broke, I can fix that.
Rodney Hills
Honored Contributor

Re: using tar with xargs

I don't think you'll get the results you want. "xargs" runs tar multiple times, thus the last tar run will be the only thing on the tape.

If you have a list of files to copy you can use "cpio". If you want to tar all files is a directory, you can do-

cd directory
tar cvf myarc.tar .

hope this helps.


-- Rod Hills
There be dragons...
Wodisch_1
Honored Contributor

Re: using tar with xargs

Hi Gary,

you could try the *key* "r" (rewrite) instead of "c" (create).

Just my $0.02,
Wodisch
Jeff Schussele
Honored Contributor

Re: using tar with xargs

Hi Gary,

I agree with Wodisch.
touch /other_dir/myarc.tar
then
ls | xargs tar rvf /other_dir/myarc.tar
You need to add them to the archive - not create a new archive every pass.
Also you should tar them into another directory or you'll get a recursive tar file.

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Allan Pincus
Frequent Advisor
Solution

Re: using tar with xargs

Gary,

tar doesn't allow the "r" option (which appends files to the end of a tar) if the tar doesn't already exist. So if you don't mind getting a duplicate file in your archive (maybe some junk file called "hello" that contains the word "hello") do the following.

Put this file "hello" in the directory you plan to tar.

cd to the directory. Run the following two commands in sequence:

ls | xargs -i tar cf myTar.tar

then:

ls | xargs -i tar rf myTar.tar

I tested this and it worked perfectly. You could also do this to a tape.

You'll wind up with a double copy of the "hello" file, but it'll just overwrite itself when you extract anyway.

Hope this helps.

- Allan

Gary Yu
Super Advisor

Re: using tar with xargs

thank you all for the prompt response, I can always get help from here!

Yes, '-r' with tar does the work, but fyi Jeff, I did try "touch arch.tar" to create a tar file to use '-r', but tar complained about the file format :

Tar: blocksize = 0; broken pipe?

so I have to create a dummy file and make it into tar file.

I also learned that '.' with tar can also expand all the files ...

thank you all again.

Gary
Allan Pincus
Frequent Advisor

Re: using tar with xargs

Gary,

OOPS!!!

I missed something in my commands. Do the following two commands:

ls hello | xargs -i tar cf myTar.tar {}

then:

ls | xargs -i tar rf myTar.tar {}

That'll do it!!!!

- Allan