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 all files with spaces in name 2

Status
Not open for further replies.

MoshiachNow

IS-IT--Management
Feb 6, 2002
1,851
IL
HI,

I need a short way to find all files in the directory that have a space in their name,and rename them so that the underscore replaces the space in name for all files found.

Appreciate any idea.
Thanks

Long live king Moshiach !
 
Here's a perl solution
Code:
perl -e '
foreach my $file ( glob "*" )
  {
  $file =~ /\w+\s+\w+/ or next;
  my $newname = $file;
  $newname =~ s/ /_/g;
  #print"$file $newname\n";
  system ( "mv \"$file\" $newname" );
  }
'
The perl gurus will probably laugh at my basic perl - there's almost certainly neater code - but this works.

Columb Healy
 
The ksh answer is to set IFS to a newline. The code becomes
Code:
export IFS='
'
# Note that this is ONLY a newline - NO SPACES
for file in $(ls | egrep " ")
do
  newname=$(echo $file | tr ' ' '_')
  mv "$file" $newname
done

Columb Healy
 
Code:
find . -type f -name '* *' | awk -F? '{ s=$1 ; gsub ( " +","_",s ) ; print "echo n | mv -i","\""$1"\"","\""s"\""}' | /bin/sh
 
Works on my system with Bash


"If you always do what you've always done, you will always be where you've always been."
 
Strange, works on my pdksh on Linux and ksh on AIX
Any error or does it just not do it?


"If you always do what you've always done, you will always be where you've always been."
 
find /dataVolumes/ -type f -name '* *'|head -5

/dataVolumes/proof7.4.0/.rsrc/Temporary Items
/dataVolumes/proof7.4.0/.rsrc/Black Overprint
/dataVolumes/proof7.4.0/.rsrc/Black Overprint#14065828.jt
/dataVolumes/proof7.4.0/.rsrc/Black Overprint#14065855.jt
/dataVolumes/proof7.4.0/.rsrc/Black Overprint#14072649.jt

find /dataVolumes/ -type f -name '* *' | awk -F? '{ s=$1 ; gsub ( " +","_",s ) ; print "echo n | mv -i","\""$1"\"","\""s"\""}' | /bin/sh

awk: No match.

Long live king Moshiach !
 
In ksh[/color red] you may use the following script to do this:

IFS='
'
for n in `find . -type f -name "* *"`
do
NN=`echo $n|tr ' ' '_'`
mv $n $NN
done
[/color blue]
or as 1-liner:

IFS='\n';for n in `find . -type f -name "* *"`;do NN=`echo $n|tr ' ' '_'`;mv $n $NN;done
[/color blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top