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!

Using awk or sed to change file names in batch mode

Status
Not open for further replies.

ericdlester

Technical User
Dec 19, 2000
1
US
I need to change the names of files in a directory in batch mode. i.e. I need to remove all '_' characters in the filenames and maintain the integrity of the files? I have done this previously using sed and/or awk, but it was some time ago. Can someone point me in the right direction? Thank you.
Eric D. Lester
 
Here is one way using shell and awk together.

It renames files like these:

file1_0_1_2
file2_1
file3
file4_3

into:

file1012
file21
file3
file43

by substituting the "_" for null ("") and then renaming
with the mv command.

Test by running as is and if this is what you need, then,
uncomment the mv command and comment out the echo command.


#!/usr/bin/sh

for file in *
do
newfile=`echo $file | nawk '{ gsub("_","") ; print }'`
echo $newfile
# mv -f $file $newfile
done


Hope this helps!


flogrr
flogr@yahoo.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top