[英]Replace file names with their md5 hashes
I've the next list of files: 我有下一个文件列表:
~$: find . -type f -exec md5sum {} \;
2a1731ae57fb17659b21f180f9fcbebd ./temp.rd1
f792fb6f13996a91d1ccbe2a8e3242fc ./temp2.rd1
f792fb6f13996a91d1ccbe2a8e3242fc ./older1/temp.rd2
b69997ffce70ec116b7f46eabdfc7040 ./older1/sub/temp.rd1
d757a18d4a3c11c2a9a45e2f3815d3a1 ./older2/temp.rd3
d79cc7e5f45852b79ddc896cc8e2b58c ./older3/temp.rd3
The left column contains md5 sums of a files presented in right column. 左列包含右列中显示的文件的md5总和。 I need to rename files with next way:
我需要使用以下方法重命名文件:
./2a1731ae57fb17659b21f180f9fcbebd.rd1
./f792fb6f13996a91d1ccbe2a8e3242fc.rd1
./older/f792fb6f13996a91d1ccbe2a8e3242fc.rd2
./older/sub/b69997ffce70ec116b7f46eabdfc7040.rd1
./older2/d757a18d4a3c11c2a9a45e2f3815d3a1.rd3
./older3/d79cc7e5f45852b79ddc896cc8e2b58c.rd3
I'm tring to complete this with sed: 我想用sed完成此操作:
sed -e 's/\([^ ]*\) \(.*\(\..*\)\)$/mv -v \2 \1\3/e'
This renames files but directories in paths are lost, so I'm looking for a correct way to do this (with sed or awk). 这会重命名文件,但路径中的目录会丢失,因此我正在寻找正确的方法(使用sed或awk)。
Use awk
for an easier solution: 使用
awk
可获得更简单的解决方案:
I created an extra .sh
file for this job: 我为此工作创建了一个额外的
.sh
文件:
#!/bin/sh
MD5=$(md5sum "$1" | awk '{print $1'})
DIR=${1%/*}
FN=${1##*/}
EXT=${FN##*.}
if [ -n "$EXT" ]; then
mv "$1" "$DIR/${MD5}.$EXT"
else
mv "$1" "$DIR/${MD5}"
fi
Save it and chmod 777
, then you just run 保存并使用
chmod 777
,然后运行
find . -type f -exec rename-file.sh {} \;
This might work for you (GNU sed): 这可能对您有用(GNU sed):
sed -r 's|^\s*(\S+)\s*((\.(/[^/]*)*/)[^.]*(\..*))|mv \2 \3\1\5|e' file
Rearrange each line using back references and then evaluate the command produced. 使用向后引用重新排列每行,然后评估产生的命令。
NB Perhaps a safer way is to remove the e
flag and capture the output in a file. 注意也许更安全的方法是删除
e
标志并将捕获的结果捕获到文件中。 Then pipe the file to the shell after inspection. 然后在检查后将文件通过管道传输到外壳。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.