Renaming multiple files in a directory

Hello,

I would like to rename all available files in a directory from Filename to Filename_Normal.

I tried to use below script but it is giving some error:

#!/bin/sh
 
for i in `ls`
do
echo Changing $i
mv $i $i_Normal
done

Error received:

Usage: mv [-I] [ -d | -e] [-i | -f] [-E{force|ignore|warn}] [--] src target
   or: mv [-I] [-d | -e] [-i | -f] [-E{force|ignore|warn}] [--] src1 ... srcN directory

Please help me.

try:

mv "$i" "${i}_Normal"

Thanks for your prompt reply but this renamed my file from rename_files to {rename_files}_Normal. However I wanted it as rename_files_Normal.

In the script shown:

Instead of line:

mv $i $i_Normal

try line:

mv "$i" "${i}_Normal"
ls | sed 's/.*/mv & &_Normal/' | sh

I tried this but itgave me output as {FileName}_Normal instead of FileName_Normal.

Try without quotes.

This worked perfect. Thanks for the solution. I understood the complete command except the use of "| sh" in last. Can you please explain this?

---------- Post updated at 12:41 AM ---------- Previous update was at 12:38 AM ----------

Already tried this as well but it gave the same {FileName}_Normal output.

mv $i {$i}_Normal

You can do it all in shell without invoking external programs:

for f in *; do
  mv -- "$f" "$f"_Normal
done

In addition, the shell-only solution won't break on pathological filenames.
You may also add nullglob (or test with -f) to avoid errors when there are no matching filenames.

1 Like
sh

is used to execute the piped output from sed command...you can use your "shell" name there

1 Like

Hello,

Directory is having following files:
A_Normal
B_Normal
C_Normal
Etc tc

I would like to rename all the files without �_Normal�. After rename the file name should be:
A
B
C
Etc etc

I have written following code:

#!/bin/sh
 
for i in `ls`
do
echo Changing $i .............................
filename = 'basename $i _Normal'
mv "$i" "$filename"
done

But it is giving error like:

Changing A_Normal .............................
ksh: filename:  not found.
mv: 0653-401 Cannot rename A_Normal to :
             A file or directory in the path name does not exist.

Please help me in rectifying this error.

---------- Post updated 11-07-12 at 03:22 AM ---------- Previous update was 11-06-12 at 06:25 AM ----------

I corrected it by myself, posting it if it may help anyone else:

13:48:50 $ cat rename_files
#!/bin/sh
 
for i in `ls`
do
echo Changing $i .............................
filename=`basename $i _Normal`
#echo $filename
mv "$i" "$filename"
done
13:48:59 $