使用文件名的 sha1() 哈希重命名文件的脚本

发布于 2024-10-18 02:04:27 字数 726 浏览 9 评论 0原文

我正在构建一个网站,我想对图像的文件名进行哈希处理。

如何创建一个 bash 脚本文件,使用旧文件名的 sha1 重命名目录中的每个文件?

我已经尝试过了:

#!/bin/bash
for file in *
do
  if [ -f "$file" ];then
    newfile="openssl sha1 $file"
    mv "$file" $newfile"
  fi
done

但这不起作用:(

编辑

根据这里的建议我尝试了这个:

#!/bin/bash
for file in old_names/*
do
  if [ -f "$file" ];then
    newfile=$(openssl sha1 $file | awk '{print $2}')
    cp $file new_names/$newfile.png
  fi
done

这确实重命名了文件,但我不确定使用什么来哈希文件名扩展名是否经过哈希处理?

INFO

然后我将使用 PHP 的 sha1() 函数来显示图像:

echo "<img src=\"images/".sha1("$nbra-$nbrb-".SECRET_KEY).".png\" />\n";

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 ?

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 $2}')
    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 :

echo "<img src=\"images/".sha1("$nbra-$nbrb-".SECRET_KEY).".png\" />\n";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雾里花 2024-10-25 02:04:27

到目前为止答案中的代码示例以及您编辑中的代码示例对文件的内容进行了哈希处理。如果要创建的文件名是前一个文件名的哈希值(不包括路径或扩展名),请执行以下操作:

#!/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

The code examples in the answers so far and in your edit hash the contents of 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
青衫儰鉨ミ守葔 2024-10-25 02:04:27

试试这个:

newfile=$(openssl sha1 $file | awk '{print $2}')
mv $file $newfile

Try this:

newfile=$(openssl sha1 $file | awk '{print $2}')
mv $file $newfile
水染的天色ゝ 2024-10-25 02:04:27

我试图做同样的事情,但这里的片段不是/完全/我需要的,而且我是 bash 脚本的新手......抱歉......最后我将几个想法组合到脚本中这就是我所需要的——查看 ./pics 中的文件并将它们重命名为旧的哈希值,同时保持当前的扩展名。我已经在一堆不同的图片上对此进行了测试,到目前为止它按预期工作。我想像我这样的另一个新手将能够复制/粘贴此内容,并且如果您的最终结果恰好与我的相同,那么就可以很好地进行。感谢大家的帮助!

#!/bin/bash
for file in ./pics/*
  do
    newfile=$(openssl sha1 $file | awk '{print $2}')
    ext=${file##*.}
    mv "$file" "./pics/$newfile"."$ext"
  done

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!

#!/bin/bash
for file in ./pics/*
  do
    newfile=$(openssl sha1 $file | awk '{print $2}')
    ext=${file##*.}
    mv "$file" "./pics/$newfile"."$ext"
  done
岁月无声 2024-10-25 02:04:27

尝试

newfile=$(openssl sha1 $file)    
mv "$file" "${newfile##*= }"

try

newfile=$(openssl sha1 $file)    
mv "$file" "${newfile##*= }"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文