【问题标题】:Is there a script to compress every single file in a directory and ouput it to another Directory?是否有脚本可以压缩目录中的每个文件并将其输出到另一个目录?
【发布时间】:2020-05-29 14:07:54
【问题描述】:

所以我希望压缩给定目录中的文件,例如 /etc/input 并输出 /etc/output 中的文件,它应该是这样的:

$ ls /etc/input
file1
file2
file3

$ script.sh

$ ls /etc/output
file1.zip
file2.zip
file3.zip

$ ls /etc/input

现在,我写的是这样的:

find . -type f -print | while read fname ; do
    mkdir -p "../output/`dirname \"$fname\"`"
    gzip -c "$fname" > "../output/$fname.gz"
done

【问题讨论】:

标签: bash scripting compression gzip


【解决方案1】:

您可以使用find,但我认为使用纯 Bash 会更简单。

INPUT=/etc/input
OUTPUT=/etc/output

mkdir -p "$OUTPUT"
for file in "$INPUT"/* ; do
  gzip -c "$file" > "${OUTPUT}/${file}.gz"
done

更改 INPUTOUTPUT 以匹配您想要的。

【讨论】:

  • 当 idjit 的文件名中有空格时,For 循环会被搞砸。
猜你喜欢
  • 1970-01-01
  • 2022-01-08
  • 2020-08-31
  • 1970-01-01
  • 1970-01-01
  • 2012-02-16
  • 1970-01-01
  • 1970-01-01
  • 2017-02-14
相关资源
最近更新 更多