2013-06-03 111 views
3

我该如何使用unix shell来重命名目录中的文件,以便它们的名称是它们的crc32哈希+它们的原始扩展名?Shell脚本/命令将文件重命名为其crc32值?

例子:

1-s2.0-105687199400063A-main.pdf =>e3492cf3.pdf

+1

怎么办你在你的例子中得到了'pdf' ? – fedorqui

+0

@fedorqui:sry,fixed –

+0

您是否考虑过[Compress :: Zlib :: crc32](http://perldoc.perl.org/Compress/Zlib.html)? – devnull

回答

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