I have a directory containing hundreds of image files, each named differently, such as:
abdogf.png
abcatf.png
abhorsef.png
I have created a changenames.csv file containing two columns with oldstring in the first column and newstring in the second such as:
"dog","woof"
"cat","miaow"
"horse","neigh"
These strings are currently in quotation marks, as shown.
I would like to invoke a bash command or .sh script from the command line to replace each oldstring substring with each newstring substring in the directory's filenames (not within file contents), such that the directory newly contains files called:
abwooff.png
abmiaowf.png
abneighf.png
instead of the original files.
I have tried various solutions such as https://superuser.com/questions/508731/find-and-replace-string-in-filenames/508758#508758 and How to find and replace part of filenames from list without success.
For example, I have tried invoking the following within the directory with the files:
#!/bin/bash
inputfile=${1}
while read line
do
IFS=',' read -a names <<< "${line}"
for file in `find . -maxdepth 1 -type f -name "*${names[0]}*"`; do
rename "s/${names[0]}/${names[1]}/" *
done
done < ${inputfile}
using the command line command test.sh changenames.csv.
This produces no error but makes no changes to the filenames.
I have also tried this solution https://stackoverflow.com/a/55866613/10456769 which generated an error in which @echo was not a recognised command.
Thank you in advance for any help.
find . -maxdepth 1 -type f -name "*${names[0]}*"; do rename "s/${names[0]}/${names[1]}/" * done done < ${inputfile} Code above. Invoking from command line within the directory in which files are to be changed as test.sh changenames.csv, and trying the same command from within the parent directory of where the files are stored. No error doing this but just no change made. (I have first used chmod to allow me to run the .sh script.)"tokens=1,* delims=,"' ./test2.sh: line 3:for /f "tokens=1,* delims=," %%i in (myfile.csv) do ('echostatements to verify that yourrenamecommand is what you are expecting. Try running your rename command that was printed to ensure it works. Debug :-)