I have several hundred files I need to rename, and I'm would rather not hit F2 for each file individually to rename them.
Example of file:
large1961.jpg
What I need the file to be renamed as:
1961.jpg
I don't know what type of command I can execute within a shell script that would remove a specific unwanted part of the file name ("large") and leave the rest intact.
I am an obvious novice in shell scripting and would appreciate any help.
Thanks in advance.
Folks, It all worked fine until i hit a filename with spaces in between.. how do i overcome this?
ex..
i tried on files "a.txt", "b.txt", "c d.txt", i got..
mv: cannot stat `c': No such file or directory
mv: cannot stat `d.txt': No such file or directory
Here is a script to replace spaces with underscore of all files in a directory.
You should be able to edit this to do what you want.
#! /bin/bash
# blank-rename.sh
#
# Substitutes underscores for blanks in all the filenames in a directory.
ONE=1 # For getting singular/plural right (see below).
number=0 # Keeps track of how many files actually renamed.
FOUND=0 # Successful return value.
for filename in * #Traverse all files in directory.
do
echo "$filename" | grep -q " " # Check whether filename
if [ $? -eq $FOUND ] #+ contains space(s).
then
fname=$filename # Yes, this filename needs work.
n=`echo $fname | sed -e "s/ /_/g"` # Substitute underscore for blank.
mv "$fname" "$n" # Do the actual renaming.
let "number += 1"
fi
done
if [ "$number" -eq "$ONE" ] # For correct grammar.
then
echo "$number file renamed."
else
echo "$number files renamed."
fi
exit 0