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

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 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

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

Related

Check if files exist in case some files contain [ Bash

I've got a set of files, let say
file1.txt
File2.txt
File [3].txt
file 4.txt
In my script, I store the path of each file in a var called $file.
Here is my issue:
in bash, testing the existence of it with following command
[[ ! -f "$file" ]]
WILL WORK (= system see that the file exists) for regular file like
file1.txt
File2.txt
file 4.txt BUT WILL NOT WORK (= system don't find the file - as it is not existing) with file containing [ ] in it, like File [3].txt does.
I assume it is because of the [ ] that interfer with the double [[. Testing with
test ! -f "$file"
is the same, system do not see it and return a missing file.
What can I do to escape the [ or to avoid such behaviour ? I've tried to find the solution on the net, but as I type "check if file exist with filename containing [" there is a bias as [ / [[ is used to check the existence..
Thanks for your help !
EDIT - 2022-01-15
Here is the loop I'm using
while read -r file; do
if [[ ! -f "$file" ]]; then
echo "Missing file $file"
fi
done < Compil.all ;
where Compil.all is a text file containing the path of file :
$cat Compil.all
/media/veracrypt1/file1.txt
/media/veracrypt1/File2.txt
/media/veracrypt1/File [3].txt
/media/veracrypt1/file 4.txt
$
AS I don't want to have issue with space in filenames, I've put the following code in the beginning of the script. Could it be the reason ?
IFS=$(echo -en "\n\b")
How are you storing the file var?
Simply iterating works as shown below:
$ ls
file1.txt File2.txt 'File [3].txt' 'file 4.txt'
$ for file in ./* ;do if [[ -f "$file" ]];then echo $file; fi; done
./file1.txt
./File2.txt
./File [3].txt
./file 4.txt
This also works:
$ [[ ! -f "File [3].txt" ]]
$ echo $?
1

Shell: Check If File Exists When Changing Format

I'm trying to write a script that, when run, renames all .htm files in the directory to .html for a server. NO PROBLEM!
for file in *.htm ; do mv $file `echo $file | sed 's/\(.*\.\)htm/\1html/'` ; done
However, if there is a .html equivalent of a file already, it should print out "$file.html already converted - contacted administrator" and exit with status 1
I've tried using -mv and exists, but no cigar. Any help appreciated.
You should first check for the file, then try to rename it by moving.
Something like this should suffice:
for file in *.htm; do
[ -f "${file%.*}.html" ] && mv "${file}" "${file%.*}.html" || printf "%s.html already converted - contacted administrator" "${file%.*}"
done
Note that also without any substitution you can just do mv "${file}" "${file}l".
Note that if do not use an amministrative user it is safer using an if-then-else as follows:
for file in *.htm; do
if [ -f "${file%.*}.html" ]; then
mv "${file}" "${file%.*}.html"
else
printf "%s.html already converted - contacted administrator" "${file%.*}"
fi
done

Shell Script to copy all files in a directory to a specified folder

I'm new in shell script and I am trying to figure out a way to write a script that copies all the files in the current directory to a directory specified from a .txt file and if there are matching names, it adds the current date in the form of FileName_YYYYMMDDmmss to the name of the file being copied to prevent overwritting.
Can someone help me out?
I saw thinking something along the lines of
#!/bin/bash
source=$pwd #I dont know wheter this actually makes sense I just want to
#say that my source directory is the one that I am in right now
destination=$1 #As I said I want to read the destination off of the .txt file
for i in $source #I just pseudo coded this part because I didn't figure it out.
do
if(file name exists)
then
copy by changing name
else
copy
fi
done
the problem is I have no idea how to check whether the name exist and copy and rename at the same time.
Thanks
How about this? I am supposing that the target directory is in the
file new_dir.txt.
#!/bin/bash
new_dir=$(cat new_dir.txt)
now=$(date +"%Y%m%d%M%S")
if [ ! -d $new_dir ]; then
echo "$new_dir doesn't exist" >&2
exit 1
fi
ls | while read ls_entry
do
if [ ! -f $ls_entry ]; then
continue
fi
if [ -f $new_dir/$ls_entry ]; then
cp $ls_entry $new_dir/$ls_entry\_$now
else
cp $ls_entry $new_dir/$ls_entry
fi
done
I guess this what you are looking for :
#!/bin/bash
dir=$(cat a.txt)
for i in $(ls -l|grep -v "^[dt]"|awk '{print $9}')
do
cp $i $dir/$i"_"$(date +%Y%m%d%H%M%S)
done
I assumed that a.txt contains only the name of the destination directory. If there are other entries, you should add some filter to the first statement(using grep or awk).
NB: I used full time stamp(YYYYMMDDHHmmss) instead of your YYYYMMDDmmss as it doesn't seem logical.

How can I locate a specific file in a directory within a loop?

I'm having trouble pointing to a specific file containing part of the string of another file in another directory.
If you look at the following command, lets say I have a file abc.foo in ./A, I need to apply a function by using abc_extendedname1.jpeg which is in ./B
for file in ./A/*;
do echo $file;
function $file -opt ./B/${file%.foo}_extendedname1.jpeg ./B/${file%.foo}_extendedname2.jpeg;
done
Any help would be greatly appreciated!
for file in ./A/*; do
basename=${file##*/}
basename_noext=${basename%.*}
echo "$file"
your_function "$file" -out \
"./B/${basename_noext}_extendedname1.jpeg" \
"./B/${basename_noext}_extendedname2.jpeg"
done
try
export d="`pwd`"
find ./A/*|while read file
do
echo "$file"
if [ -f "$d/B/${file}_extendedname1.jpeg" ]
then
echo "$d/B/${file}_extendedname1.jpeg" found
fi
done

Removing old directories with logs

My IM stores the logs according to the contact name. I have created a file with the list of active contacts. My problem is following:
I would like to create a bash script with read the active contacts names from the file and compare it with the directories. If the directory name wouldn't be found on the list, it would be moved to another directory (let's call it "archive"). I try to visualise it for you.
content of the list:
contact1
contact2
content of the dir
contact1
contact2
contact3
contact4
after running of the script, the content fo the dir:
contact1
contact2
contact3 ==> ../archive
contact4 ==> ../archive
You could use something like this:
mv $(ls | grep -v -x -F -f ../file.txt) ../archive
Where ../file.txt contains the names of the directories that should not be moved. It is assumed here that the current directory only contains directories, if that is not the case, ls should be replaced with something else. Note that the command fails if there are no directories that should be moved.
Since in the comments to the other answer you state that directories with whitespace in the name can occur, you could replace this by:
for i in *
do
echo $i | grep -v -x -q -F -f ../file.txt && mv "$i" ../archive
done
This is an improved version of marcog's answer. Note that the associative array requires Bash 4.
#!/bin/bash
sourcedir=/path/to/foo
destdir=/path/to/archive
contactfile=/path/to/list
declare -A contacts
while read -r contact
do
contacts[$contact]=1
done < "$contactfile"
for contact in "$sourcedir"/*
do
if [[ -f $contact ]]
then
index=${contact##*/}
if [[ ! ${contacts[$index]} ]]
then
mv "$contact" "$destdir"
fi
fi
done
Edit:
If you're moving directories instead of files, then change the for loop above to look like this:
for contact in "$sourcedir"/*/
do
index=${contact/%\/}
index=${index##*/}
if [[ ! ${contacts[$index]} ]]
then
mv "$contact" "$destdir"
fi
done
There might be a more concise solution, but this works. I'd strongly recommend prefixing the mv with echo to test it out first, otherwise you could end up with a serious mess if it doesn't do what you want.
declare -A contacts
for contact in "$#"
do
contacts[$contact]=1
done
ls a | while read contact
do
if [[ ! ${contacts[$contact]} ]]
then
mv "a/$contact" ../archive
fi
done

Resources