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

??? spaces in file names ???

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
guidance needed... sh or ksh

I need to rename all filenames in the current directory that contain a space with an "_" (thats an underscore.)

any feedback is appreciated...Thanks:)

 
Do you want to it manually?

mv "file name" file_name should do it for you.

An automated script may be a bit more difficult, something in this form may or may not do the job, somehow I think not:

LIST=`ls *`
for i in $LIST do
mv $i $i_extension (you will have to wing it from here)
done


Else you will have to try your hand at an array, read each file into that and work from there.

IBM Certified Specialist - MQSeries
 
I need a script because there are hundreds of these files.

The for loop parses each word in the filename into separate files.
i.e. before: filename1="my favorite file"

after : filename1="my"
filename2="favorite"
filename3="file"

The following works but I don't know how to use the mv command with it to obtain my goal.

for file in `ls --format=single-column`
do
print $file | tr " " "_"
mv $file ????????<----what about second argument?
done

Maybe an array is the answer like you said.


 
Here is a script that will create a script to do all of the renames for you...

Script=&quot;mv.ksh&quot;
>$Script
for file in `ls|sed &quot;s/ /_/g&quot;` ; do
file1=`echo &quot;$file&quot;|sed &quot;s/_/ /g&quot;`
echo &quot;mv '&quot;${file1}&quot;' &quot;${file} >>$Script
done


When this script is done, run the script specified by the $Script variable. You can call it whatever you like.

Bill.
 
Nuff sed...LOL

I will try this too Bill, had the odd occasion when these space occurred in filenames, normally I did it manually as it was only a few.

One to add to my script archive....cheers!

Chris IBM Certified Specialist - MQSeries
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top