压缩 JPEG(用于压缩)通常是浪费精力
首先,尝试压缩已压缩的格式(如 JPEG 文件)通常是浪费时间,有时会导致存档比原始文件大。但是,为了方便将一堆文件放在一个包中,这样做有时很有用。
只是要记住的事情。YMMV。
使用 Find 的-execdir
标志
您需要的是find实用程序的-execdir
标志。GNU find 手册页说:
-execdir command {} +
Like -exec, but the specified command is run from the subdirec‐
tory containing the matched file, which is not normally the
directory in which you started find.
例如,给定以下测试语料库:
cd /tmp
mkdir -p foo/bar/baz
touch foo/bar/1.jpg
touch foo/bar/baz/2.jpg
您可以使用 find 压缩整个文件集,同时通过一次调用排除路径信息。例如:
find /tmp/foo -name \*jpg -execdir zip /tmp/my.zip {} +
使用 Zip 的--junk-paths
标志
许多系统上的 zip 实用程序都支持--junk-paths
标志。zip的手册页说:
--junk-paths
Store just the name of a saved file (junk the path), and do not
store directory names.
因此,如果您的 find 实用程序不支持-execdir
,但您确实有一个支持垃圾路径的 zip,您可以这样做:
find /tmp/foo -name \*jpg -print0 | xargs -0 zip --junk-paths /tmp/my.zip