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

Unix Newbie- How do I loop thru files, rename and move?

Status
Not open for further replies.

brainchild1977

Programmer
Feb 1, 2005
9
US
Hello,

I need to be able to loop through all the files in a certain directory (e.g directory A). The files am looking for would have an EMS extension (i.e *.ems). Once it locates these files, my script should rename the files i.e from testfile.ems to testfile.txt. It should keep the orginal filename(prefix) so the only thing am changing is the extension. After renaming, i need to move the newly renamed files to a seperate directory (e.g Directory B)

Am very new to Unix scripting so any help would be appreciated.

 
man find
man basename
man mv

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I found this line of code online which seems to do what i want what am getting errors

#!/bin/sh

OLDSUFFIX=ems
NEWSUFFIX=failed
for FILE in *."$OLDSUFFIX"
do NEWNAME=´echo "$FILE" | sed -e "s/${OLDSUFFIX}\$/NEWSUFFIX/"´
mv "$FILE" "$NEWNAME"
done

any ideas on a simpler way?
 
#!/usr/dt/bin/dtksh

OLDSUFFIX=ems
NEWSUFFIX=failed
for FILE in *.${OLDSUFFIX}
do
file=${FILE%%.*}
mv ${file}.${OLDSUFFIX} ${file}.${NEWSUFFIX}
done
 
Thanks for your quick reply. when i ran your revised script, i get an error that says "can't access *.ems"

I do have some ems files in the same directory that the script is running in.

any ideas?
 
To be sure, add a cd command before the loop.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I added a "cd" command before the for loop. But am still encountering the same problem.

aby ideas?
 
So, for debugging purpose, add a pwd command too.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top