Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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";
like image 422
Manu Avatar asked Feb 18 '11 13:02

Manu


People also ask

How do you rename a file in shell script?

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.

How do I change the SHA value of a file?

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.

How do I rename a file in bash?

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.

How do you rename a log file in Linux?

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.


3 Answers

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
like image 92
Dennis Williamson Avatar answered Sep 23 '22 08:09

Dennis Williamson


Try this:

newfile=$(openssl sha1 $file | awk '{print $2}')
mv $file $newfile
like image 2
Eugene Yarmash Avatar answered Sep 19 '22 08:09

Eugene Yarmash


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
like image 1
Adam Gross Avatar answered Sep 23 '22 08:09

Adam Gross