Bash 解压缩文件并在名称中添加时间戳

Bash unzip file and add timestamp in name

我有带结构的 zip

temp.zip

- file.csv
- readme.txt
- license.txt

如何解压缩 temp.zip,在名称中添加时间戳, 结果:

文件。142345687.csv
自述文件。142345687.txt
许可证。142345687.txt

使用 -l 选项列出文件,然后使用 -p 选项一个一个地提取它们:

unzip -l -q -q temp.zip | awk '{print $NF}' | while read file
do
  unzip -p temp.zip "${file}" > "${file%.*}.$(date +%s).${file##*.}"
done

哪里

  • -q -q 选项要求静默输出(在易于解析的列中);
  • awk$NF指向最后一列;
  • ${file%.*} 从文件名后面删除 .* 的最短匹配;
  • ${file##*.} 删除文件名前面最长的 *. 匹配;
  • $(date +%s) 输出自 1970-01-01 00:00:00 UTC
  • 以来的秒数

你可以尝试这样的事情;

#!/bin/bash
unzip temp.zip
for n in $(unzip -Z -1 "temp.zip"); do 
  e=${n#*.}
  fn="${n%.*}"
  DATE=`date +%s`
  newFileName="$filename.$DATE.$e"
  mv "$n" "$newFileName"
done