user2965031
user2965031

Reputation: 71

find and compress on the fly a file

I'm trying to find and compress on the fly a specific type of the file, but TAR compressed more files than I found. Fore example:

here I found no. 7

 /mnt/1 % find . -name *.pdf
./. /01.pdf
./slide/01.pdf
./slide/03.pdf
./slide/02.pdf
./.Trash-0/files/01.pdf
./.Trash-0/files/01.2.pdf
./.Trash-0/files/01 - Introduzione ISTI + note.pdf

but when I compressed on the fly, the archive contain also other files

 /mnt/1 % find . -name *.pdf | xargs tar czvf /root/Desktop/evidence/pdf.tar

deft8vm /mnt/1 % tar -tvf /root/Desktop/evidence/pdf.tar 
drwxr-xr-x root/root         0 2014-04-14 13:51 ././
drwxr-xr-x root/root         0 2014-04-15 08:27 ././. /
-rw-r--r-- root/root   9070641 2014-04-14 13:40 ././. /01.pdf
drwx------ root/root         0 2014-04-15 08:31 ././. /4Dell/
drwx------ root/root         0 2014-04-15 08:31 ././. /4Dell/4Dell.afd/
-rw-r--r-- root/root   4992592 2014-04-15 08:31 ././. /4Dell/4Dell.afd/file_000.aff.csv
-rw-r--r-- root/root 1051669804 2014-04-15 08:31 ././. /4Dell/4Dell.afd/file_000.aff
-rw-r--r-- root/root       1524 2014-04-15 08:31 ././. /4Dell/4Dell.afd.txt
drwx------ root/root          0 2014-04-14 11:14 ././lost+found/
drwxr-xr-x root/root          0 2014-04-14 13:51 ././slide/
hrw-r--r-- root/root          0 2014-04-14 13:40 ././slide/01.pdf link to ././. /01.pdf

 /mnt/1 % tar -tf /root/Desktop/evidence/pdf.tar | wc -l

29

Upvotes: 0

Views: 2745

Answers (2)

PradyJord
PradyJord

Reputation: 2160

One more way of doing this

find . -iname "*.pdf" -exec tar --append --file=somefile.tar {} \;

Upvotes: 0

that other guy
that other guy

Reputation: 123470

This is why you should never use find and xargs without -print0 and -0 or compatible options.

The filename ./. /01.pdf is split up into ./. and /01.pdf, and ./. is equivalent to ., i.e. the entire current directory.

There's another, more subtle problem too: xargs doesn't run a command with input as parameters. It runs multiple commands with chunks of input as parameters. This means that if you have enough files, they will be split over multiple tar commands, overwriting each other.

Instead, if you're using GNU, you can use find -print0 to print \0 separated filenames, and tar --null -T to read them:

find . -name '*.pdf' -print0 | tar czvf pdf.tar --null -T -

Upvotes: 3

Related Questions