I made a mistake in a script and now need to go back and change allot of filenames. I need to change "v4" in filenames to "v3". I was thinking of something like this.
#!/bin/bash
FILELIST=$(ls -f -R *)
for FILE in $FILELIST
do
# create new filename
NEW_FILE_NAME=$(echo $FILE | sed 's/v4/v3/g')
# if a substitution was made
if [ "$NEW_FILE_NAME" -ne "$FILE" ]; then
# copy file to new name
cp $FILE $NEW_FILE_NAME
# remove original file
rm $FILE
fi
done
There are a rew issues with this approach. I'm not sure that ls -R -f is the best way to retrieve the list of files and since this will run recursively, I need to do some testing to make sure I don't do more harm the good. I'm also not sure about the ls -f option as far as filenames and paths go since I'm not changing directories.
This does not appear to work recursively, at least the echo version only gave output for files in pwd. This will need to work down through a fairly deep file tree to be practical.