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";
The most commonly used command in Linux to rename a filename is the 'mv' command. The syntax of this command is given below. Using any option with the 'mv' command is optional. To rename a file, you must type the original filename after the renamed filename with this command.
You can't change the hash without changing the content of the file. The hash is just what you get after doing some calculations on the content of the file.
To use mv to rename a file type mv , a space, the name of the file, a space, and the new name you wish the file to have. Then press Enter. You can use ls to check the file has been renamed.
To rename a file in Linux you use the mv command. The command accepts two or more arguments. For renaming files, only two arguments are needed, which are the source file and the target file. The mv command will take the source file specified and rename it to the target file.
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
Try this:
newfile=$(openssl sha1 $file | awk '{print $2}')
mv $file $newfile
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With