find with xargs and tar

我是研究僧i 提交于 2019-12-06 19:47:43

问题


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 6 subdirs depth, and then once pruning is done, tar up the rest.

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.


回答1:


What worked for me is a combination of the -print flag for find, and then --files-from on the tar command. In my case I need 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 -



回答2:


I remember doing something like below line to tar bunch of files together. I was specific about the files i want 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




回答3:


If your find is returning directories, then those will be passed to tar, and the full contents will be included, regardless of the exclusions in your find command.

So, I think you need to include a "-type f" in the find.




回答4:


I use a combination of the two approaches above - to backup a day's work I do this: rm -rf new.tgz; find . -type f -mtime 0 | xargs tar cvf new.tgz;




回答5:


To use files-from without an option was the only to make it work for me. All other options included all files in the directory rather than my generated list.

This was my solution:

find . ! -name '*.gz' -print | xargs tar cvzf ../logs.tar.gz --files-from


来源:https://stackoverflow.com/questions/11540964/find-with-xargs-and-tar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!