【问题标题】:find and compress on the fly a file即时查找并压缩文件
【发布时间】:2014-04-20 16:43:15
【问题描述】:

我正在尝试动态查找并压缩特定类型的文件,但 TAR 压缩的文件比我找到的要多。 例如:

在这里我发现没有。 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

但是当我动态压缩时,存档还包含其他文件

 /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

【问题讨论】:

  • 指定你要find files: find . -type f -name "*.pdf" | xargs tar czvf /root/Desktop/evidence/pdf.tar
  • 如果devnull's 上面的例子不清楚。你想要find 而不是shell 来处理*.pdf,所以你需要引用它(单引号和双引号都可以防止通配符)

标签: bash find tar


【解决方案1】:

这就是为什么在没有-print0-0 或兼容选项的情况下,您永远不应该使用findxargs

文件名./. /01.pdf分为././01.pdf./.等价于.,即整个当前目录。

还有另一个更微妙的问题:xargs 不运行将输入作为参数的命令。它以输入块作为参数运行多个命令。这意味着如果您有足够的文件,它们将被拆分为多个 tar 命令,相互覆盖。

相反,如果您使用的是 GNU,则可以使用 find -print0 打印 \0 分隔的文件名,并使用 tar --null -T 读取它们:

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

【讨论】:

    【解决方案2】:

    另一种方法

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

    【讨论】:

    • 此命令生成一个 .tar 但不压缩它。我做了类似find . -iname "*.pdf" -exec tar --append --file=somefile.tar {} \; 然后 gzip -9 somefile.tar
    猜你喜欢
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    • 2011-04-08
    • 2011-06-17
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多