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?
+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
2009-11-27 05:21:03
got the below errortr: two strings must be given when translatingmv: missing file argumentTry `mv --help' for more information.
Mithun P
2009-11-27 05:31:24
Agin error tr: too many argumentsTry `tr --help' for more information.mv: missing file argumentTry `mv --help' for more information.
Mithun P
2009-11-27 07:06:15
+1
A:
If you use bash:
for file in *; do mv "$file" ${file// /_}; done
Murali VP
2009-11-27 05:24:38
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
2009-11-27 05:32:12
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
2009-11-27 07:07:02
+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
2009-11-27 05:39:49
+4
A:
this should do it:
for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done
neesh
2009-11-27 05:42:03
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
2009-11-27 07:05:16
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
2009-11-27 07:08:33
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
2009-11-27 11:54:46
+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
2009-11-27 05:45:07
this will work *if* you have the perl-style rename and not the simpler redhat/fedora one
David Dean
2009-11-27 05:56:38
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
2010-04-02 20:33:16