We use a program that creates multiple files based on an event number that is manually entered. If the event number is entered incorrectly, we have to change the name on many created files; however, the event number is only a portion of the file name. I only need to change the event number in the file name, is there a way to do that on all files within a specific directory through a script that requests the wrong event number and the correct event number from the user? I have researched sed for this but have not been able to get it to work. I am using Bash to write the script.
OLD="1234"
NEW="4567"
DIR="/path/to/dir"
ls $DIR/*OLD* | while read FILE
do
echo mv $FILE $(echo $FILE |sed "s/$OLD/$NEW/");
done
Please note that I put echo in front of the "mv" statement so you can do kind of a "dry run".
Remove the bolded echo statement to perform the real operation.
Further, I added the old and new numbers as well as the directory as variables.
If you don't know how to request user input to fill these variables please let me know.
wmp