views:

320

answers:

8

I have a number of files in a folder, i want to replace every space character in all file names with under scores. How can I achieve this?

A: 

I believe your answer is here

Arthur Frankel
+1  A: 

Try something like this, assuming all of your files were .txt's :

for files in *.txt; do mv "$files" `echo $files | tr '''_'`; done

CORRECTION :

for files in *.txt; do mv “$files” `echo $files | tr ‘ ‘ ‘_’`; done
Amir Afghani
got the below errortr: two strings must be given when translatingmv: missing file argumentTry `mv --help' for more information.
Mithun P
Agin error tr: too many argumentsTry `tr --help' for more information.mv: missing file argumentTry `mv --help' for more information.
Mithun P
+1  A: 

If you use bash:

for file in *; do mv "$file" ${file// /_}; done
Murali VP
when i tried, i gotmv: when moving multiple files, last argument must be a directoryTry `mv --help' for more information.mv: when moving multiple files, last argument must be a directoryTry `mv --help' for more information.
Mithun P
Should be `${FILE/ /_}`
soulmerge
that only replace 1 space
Again error mv: missing file argumentTry `mv --help' for more information.mv: missing file argumentTry `mv --help' for more information.mv: missing file argumentTry `mv --help' for more information.mv: missing file argumentTry `mv --help' for more information.
Mithun P
@levislevis85: Thx, didn't know that.
soulmerge
+2  A: 

Use sh...

for i in *' '*; do   mv "$i" `echo $i | sed -e 's/ /_/g'`; done

If you want to try this out before pulling the trigger just change mv to echo mv.

DigitalRoss
This one also worked fine
Mithun P
+4  A: 

this should do it:

for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done
neesh
This worked fine!
Mithun P
A: 

quote your variables

for file in *; do echo mv "'$file'" "${file// /_}"; done

remove the "echo" to do actual rename

It is echoing the mv commands prperly, but not really renaming the file!
Mithun P
removing echo produces error like mv: cannot stat `\'1130 lake micigan view.jpg\'': No such file or directorymv: cannot stat `\'1130_1_bedroom_floor_plan.jpg\'': No such file or directorymv: cannot stat `\'1130_BedPicture_8.jpg\'': No such file or directorymv: cannot stat `\'1130_diningroom_table.jpg\'': No such file or directory
Mithun P
what is your OS?
what shell are you using?
LinuxLinux 2.6.9-42.0.3.EL.wh1smp #1 SMP Fri Aug 14 15:48:17 MDT 2009 i686 i686 i386 GNU/Linux
Mithun P
+3  A: 

I prefer to use the command 'rename', which takes Perl-style regexes:

rename "s/ /_/g" *

You can do a dry run with the -n flag:

rename -n "s/ /_/g" *
DF
this will work *if* you have the perl-style rename and not the simpler redhat/fedora one
David Dean
the fedora version would be `rename " " "_" *`
David Dean
A: 

What if you want to apply the replace task recursively? How would you do that?

Well, I just found the answer myself. No the most elegant solution (tries to rename also files that do not comply with the condition) but works. (BTW, in my case I needed to rename the files with '%20', not with an underscore)

#!/bin/bash
find . -type d | while read N
do
     (
           cd "$N"
           if test "$?" = "0"
           then
               for file in *; do mv "$file" ${file// /%20}; done
           fi
     )
done
javipas