将文件重命名为其CRC32值的Shell脚本/命令?

3
我该如何使用Unix shell重命名目录中的文件,使它们的名称为它们的crc32哈希值+原来的扩展名?
例如: 1-s2.0-105687199400063A-main.pdf => e3492cf3.pdf

1
你的示例中如何获取 pdf 文件? - fedorqui
@fedorqui:抱歉,已修复。 - Frantisek
你考虑过使用Compress::Zlib::crc32吗? - devnull
2个回答

2
for file in `ls`; do mv "${file}" `cksum "${file}" | cut -d' ' -f1`."${file##*.}"; done

也许使用 awk 比使用 cut 更好。

1
像这样的东西可能有效:

for file in /your/dir/*
do
  extension="${file##*.}"   #gets the block after the last dot, that is, the extension
  new_name=$(crc32 $file)   #calculates the crc32 value of the file
  mv $file ${new_name}.${extension} #renames by moving the original file to the new name
done

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接