我有很多目录,我想将它们全部压缩。
$ mkdir -p one two three
$ touch one/one.txt two/two.txt three/three.txt
$ ls -F
one/ three/ two/
我使用zip
并且它按预期工作:
$ zip -r one.zip one
adding: one/ (stored 0%)
adding: one/one.txt (stored 0%)
$ ls -F
one/ one.zip three/ two/
但是当我使用 zsh 在循环中使用它时,zip 文件是在其他地方创建的。
$ for dir in */; do
for> echo "$dir";
for> zip -r "$dir.zip" "$dir";
for> done
one/
adding: one/ (stored 0%)
adding: one/one.txt (stored 0%)
three/
adding: three/ (stored 0%)
adding: three/three.txt (stored 0%)
two/
adding: two/ (stored 0%)
adding: two/two.txt (stored 0%)
$ find . -name "*.zip"
./three/.zip
./two/.zip
./one/.zip
$ ls -F
one/ three/ two/
我期望这样的输出:
$ ls -F
one/ one.zip three/ three.zip two/ two.zip
这是怎么回事?
答案1
您可以在输出中看到它:
for dir in */; do
for> echo "$dir";
for> zip -r "$dir.zip" "$dir";
for> done
one/
[ . . . ]
由于您正在执行for dir in */
,因此该变量包含尾部斜杠。所以你的$dir
不是one
,它是one/
。因此,当您运行 时zip -r "$dir.zip" "$dir";
,您正在运行以下命令:
zip -r "one/.zip" "one";
zip
完全按照你的指示去做也是如此。我认为你想要的是这样的:
$ for dir in */; do dir=${dir%/}; echo zip -r "$dir.zip" "$dir"; done
zip -r one.zip one
zip -r three.zip three
zip -r two.zip two