焦油:避免归档大于特定大小的文件

我想存档文件(与焦油)的大小在3 MB以下。 但我也想保留这些文件存在的目录。 (所以我不能使用find命令)。 我只想避免大小超过3 MB的文件。 如何才能做到这一点?

比你想象的更简单:

 $ tar cf small-archive.tar /big/tree --exclude-from <(find /big/tree -size +3M) 

在一个半相关的说明(涉及你的声明,你不能使用find)得到一个path下的所有文件(包括目录)的列表减去大于3MiB的文件,使用:

 $ find . -size -3M -o -type d 

你可以这样做:

 $ tar cf small-archive.tar --no-recursion --files-from <(find /big/tree -size -3M -o -type d) 

但我更喜欢第一个,因为它更简单,明确地expression你想要的,并会导致更less的意外。

如果文件名包含方括号,在某些系统中,需要明确排除。 例如

 $ mkdir test $ echo "abcde123456" > ./test/a[b].txt $ echo "1" > ./test/a1.txt $ ls -la ./test total 16 drwxrwxr-x 2 user user 4096 Jan 10 16:38 . drwx------ 4 user user 4096 Jan 10 16:38 .. -rw-rw-r-- 1 user user 2 Jan 10 16:38 a1.txt -rw-rw-r-- 1 user user 12 Jan 10 16:38 a[b].txt $ tar -zcvpf a.tar.gz ./test ./test/ ./test/a[b].txt ./test/a1.txt $ tar -zcvpf a3.tar.gz ./test --exclude-from <(find ./test -type f -size +3c) ./test/ ./test/a[b].txt ./test/a1.txt $ tar -zcvpf ax.tar.gz ./test --exclude-from <(find ./test -type f -size +3c) --exclude '*\[*' ./test/ ./test/a1.txt 

如果你想通过SSH在服务器上执行此操作,则不能正常工作。 要解决这个问题,你可以使用pipe道和xargs:

 find /path/to/dir -type f -size -3M | xargs tar cf archive.tar