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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename Files 101

Status
Not open for further replies.

migueljr3

Technical User
Jun 22, 2001
23
US
Greetings,

I would like to update one of our devices on our MRTG station as the name has changed, but would like to keep
all the data.

How do I rename all files with "mor1":
Such as:

map-mor1-rtr1_se0_0.1-month.png
map-mor1-rtr1_se0_0-week.png

To "mor161"

map-mor161-rtr1_se0_0.1-month.png
map-mor161-rtr1_se0_0-week.png

Please help with a quick shell script of some sort.

Linux Newbie


 
You could try the 'rename' command - just check out the manual page, it's quite a simple command provided you know regex.
 
How many files are there? If there are only a few, just use the mv command on each file, mv map-mor1-rtr1_se0_0.1-month.png map-mor161-rtr1_se0_0.1-month.png. If there are a lot of files, you may want to write a script to run through and change every occurrance.
 
#!/bin/bash
# Mass File Renamer
# By PcLinuxGuru
#
# Replace EXP1 with the expression your looking for (note leave the *s where they are....)
#
# Replace EXP2 with the expression you want it to be....
#
# Misuse of this script can disable your system so I am not responsible. Use at your own risk
#
echo
echo "Updating the Locate Database.."
echo "This could take awhile so please wait...."
locate -u;
echo "Database Updated"
echo
echo "Creating list of files that match your search query"
locate *EXP1* >> fileslist.txt
echo "Query complete. The list of files is saved as fileslist.txt"
echo
echo "Changing filenames....."
for FILE in *exp1*; do
mv $FILE `echo $FILE | sed 's/exp1/exp2/'`
done
echo
echo "All done. Please review the fileslist.txt file for the files I have changed."
echo


 
Miguel,
You can use the script above...

Replace each occurance of exp1 with mor1 and each occurance of exp2 with mor161 or whatever you like. I did test the script with the expression in the middle of the filename (I used it to changed 1000 or so .txt files to .html) I had to use the widcard before and after the expression so don't move them.

 
thanks all for the help, will test today!

Excellent!
 
PCLinux Guru

Thank you so much! How can run this script so I can add variables such as:

root@cheese#./renamer exp1 exp2


This is not working (bare with me, script newbie)

echo "Creating list of files that match your search query"
locate *$1* >> fileslist.txt
echo "Query complete. The list of files is saved as fileslist.txt"
echo
echo "Changing filenames....."
for FILE in *$1*; do
mv $FILE `echo $FILE | sed 's/$1/$2/'`



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top