2
$ find /tmp/a1
/tmp/a1
/tmp/a1/b2
/tmp/a1/b1
/tmp/a1/b1/x1

simply trying

find /tmp/a1 -exec tar -cvf dirall.tar {} \;

simply doesn't work

any help

jamessan
  • 41,569
  • 8
  • 85
  • 85
soField
  • 2,536
  • 9
  • 36
  • 44

4 Answers4

3

The command specified for -exec is run once for each file found. As such, you're recreating dirall.tar every time the command is run. Instead, you should pipe the output of find to tar.

find /tmp/a1 -print0 | tar --null -T- -cvf dirall.tar

Note that if you're simply using find to get a list of all the files under /tmp/a1 and not doing any sort of filtering, it's much simpler to use tar -cvf dirall.tar /tmp/a1.

jamessan
  • 41,569
  • 8
  • 85
  • 85
  • Or even tar -cvf \`find /tmp/a1\` now that I think about it :) Actually, if you tar /tmp/a1, you get it all anyway. I might be misunderstanding the problem. –  Jun 02 '10 at 14:22
  • Yeah, I was just assuming that the `find` was actually more complex and that the question had been boiled down to a simple scenario. – jamessan Jun 02 '10 at 14:42
1

You're one character away from the solution. The find command's exec option will execute the command for each file found, so you should replace -c with -r to put tar into append mode. Each time find invokes it, it'll tack on one more file:

rm -f dirall.tar
find /tmp/a1 -exec tar -rvf dirall.tar {} \;
Rob Davis
  • 15,597
  • 5
  • 45
  • 49
  • You can only specify one "function" option. Here you have two -- `-c` and `-r`. Drop the `-c` (and quote or escape the `{}`) and it should work. – jamessan Jun 02 '10 at 20:15
  • Thanks for catching the typo -- I should have tried the command first. I've edited the answer to remove the -c option. Also, no need to escape the {} in a Bourne-based shell. – Rob Davis Jun 02 '10 at 21:45
0

I'd think something like "find /tmp/a1 | xargs tar cvf foo.tar" would work. But make sure you have backups first!

0

Does hpux have cpio ? That will take a list of files on stdin and some versions will write output in tar format.

Steven D. Majewski
  • 2,127
  • 15
  • 16