bash - Script to rename files using a sha1() hash of their filename -


i'm building website , hash filenames of images.

how can create bash script file renames every file in directory sha1 of old filename ?

i've tried :

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

but doesn't work :(

edit

based on suggestions here tried :

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

this rename files, i'm not sure has been used hash file name. did extention hashed ? did path ?

info

i use php's sha1() function display images :

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

the code examples in answers far , in edit hash contents of file. if want create filenames hashes of previous filename, not including path or extension, this:

#!/bin/bash file in old_names/*     if [ -f "$file" ]             base=${file##*/}         noext=${base%.*}         newfile=$(printf '%s' "$noext" | openssl sha1)         cp "$file" "new_names/$newfile.png"     fi done 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -