Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mass rename files 2

Status
Not open for further replies.

babeo

Technical User
Mar 30, 2000
398
CA
Hi,

I often have to rename a lot of files, and it's a pain for me to do 1 file at a time, I want to do a mass rename filenames by using 1 command, but I don't know how to, eg I want to rename 200 files in 1 directory by adding an extention to these files, which means I still keep the 200 files with the same name AND having extention of '.doc',

eg: MyServer/MyDir% ls
qwerty asdfgh
zxcvbn sdfhgj .....

Now I want to change all these files to become:

qwerty.doc asdfgh.doc
zxcvbn.doc sdfhgj.doc .....

I have tried to mv with grep, but it does not work that way, so I think I have to use either 'sed' or 'awk', but I am not good about that, could someone help me with an proper command please.

Thanks
 
cd /dir/in/question

for blah in `ls` ; do mv $blah $blah.doc; done

 
Here are some linesI use to add extensions, change extensions, etc. I think I found this somewhere on Tek-Tips.

This changes all file with a .txt extension to have a .doc extension.

old_ext=txt
new_ext=doc
for file in $(ls *.$old_ext)
do
mv $file ${file%${old_ext}}${new_ext}
done

If the files have an extension and you want them renamed with just the first part of the filename:

old_ext=.txt
new_ext=
for file in $(ls *$old_ext)
do
mv $file ${file%${old_ext}}${new_ext}
done

If the files don't have an extension and you want them to:

old_ext=
new_ext=.txt
for file in $(ls *$old_ext)
do
mv $file ${file%${old_ext}}${new_ext}
done
 
Thanks Bi,
It works beautiful.

Dorga,
You gave the similar ideas, but your command does not work properly. All the files are delete, but not renamed them. !!

Thank you for all the helps and ideas.
 
Uh, yeah, whatever.. the command works as designed, it does not delete anything, there is no rm listed in the command anywhere.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top