bash 使用文件名的 sha1() 哈希重命名文件的脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5042138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Script to rename files using a sha1() hash of their filename
提问by Manu
I'm building a website and I would like to hash the filenames of my images.
我正在建立一个网站,我想散列我的图像的文件名。
How can I create a bash script file that renames every file in a directory with the sha1 of the old filename ?
如何创建一个 bash 脚本文件,用旧文件名的 sha1 重命名目录中的每个文件?
I've tried :
我试过了 :
#!/bin/bash
for file in *
do
if [ -f "$file" ];then
newfile="openssl sha1 $file"
mv "$file" $newfile"
fi
done
But that doesn't work :(
但这不起作用:(
EDIT
编辑
Based on suggestions here I tried this :
根据这里的建议,我尝试了这个:
#!/bin/bash
for file in old_names/*
do
if [ -f "$file" ];then
newfile=$(openssl sha1 $file | awk '{print }')
cp $file new_names/$newfile.png
fi
done
This does rename the files, but I'm not sure what has been used to hash the file name. Did the extention get hashed ? did the path ?
这确实重命名了文件,但我不确定用于散列文件名的内容。扩展是否被散列了?没有路径?
INFO
信息
I will then use PHP's sha1() function to display the images :
然后我将使用 PHP 的 sha1() 函数来显示图像:
echo "<img src=\"images/".sha1("$nbra-$nbrb-".SECRET_KEY).".png\" />\n";
采纳答案by Paused until further notice.
The code examples in the answers so far and in your edit hash the contentsof the file. If you want to create filenames that are hashes of the previous filename, not including the path or extension, then do this:
到目前为止答案中的代码示例以及您编辑中的散列文件内容。如果要创建的文件名是前一个文件名的哈希值,不包括路径或扩展名,请执行以下操作:
#!/bin/bash
for file in old_names/*
do
if [ -f "$file" ]
then
base=${file##*/}
noext=${base%.*}
newfile=$(printf '%s' "$noext" | openssl sha1)
cp "$file" "new_names/$newfile.png"
fi
done
回答by Eugene Yarmash
Try this:
尝试这个:
newfile=$(openssl sha1 $file | awk '{print }')
mv $file $newfile
回答by Adam Gross
I was trying to do the same sorta thing but the snippets here weren't /exactly/ what I needed, plus I'm brand new to bash scripting... sorry... In the end I stuck several ideas together into the script that does what I need which is -- look at the files in ./pics and rename them to their old hash while maintaining the current extension. I've tested this on a bunch of different pictures and so far it works as intended. I imagine another newbie such as myself would be able to copy/paste this in and be good to go if your end result happens to be the same as mine. Thanks everyone for the help!
我试图做同样的事情,但这里的片段不是/正是/我需要的,而且我是 bash 脚本的新手......对不起......最后我把几个想法放在了脚本中这就是我需要的——查看 ./pics 中的文件并将它们重命名为旧的哈希,同时保持当前的扩展名。我已经在一堆不同的图片上测试了这个,到目前为止它按预期工作。我想另一个像我这样的新手将能够复制/粘贴这个,如果你的最终结果恰好和我的一样,那就很好了。感谢大家的帮助!
#!/bin/bash
for file in ./pics/*
do
newfile=$(openssl sha1 $file | awk '{print }')
ext=${file##*.}
mv "$file" "./pics/$newfile"."$ext"
done
回答by kurumi
try
尝试
newfile=$(openssl sha1 $file)
mv "$file" "${newfile##*= }"

