| Author |
Message |
R-S-H
Joined: 18 Feb 2013 Posts: 280
|
Posted: Sat 02 Mar 2013, 16:12 Post subject:
How to replace spaces in file names with underscores? |
|
Please have a look at the third post of this thread.
I do re-use this one.
Thanks
_________________ Freiheit für Mollath - JETZT !!!
Free Mr. Mollath !!!
Send the People responsible to The Hague International Court for trial !!!
Last edited by R-S-H on Fri 08 Mar 2013, 13:17; edited 2 times in total
|
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 4006 Location: Arizona, U.S.A.
|
Posted: Mon 04 Mar 2013, 20:13 Post subject:
|
|
Hey RSH; Just rewrite and reuse this thread for another Q !
|
|
Back to top
|
|
 |
R-S-H
Joined: 18 Feb 2013 Posts: 280
|
Posted: Fri 08 Mar 2013, 13:16 Post subject:
How to replace spaces in file names with underscores? |
|
| sunburnt wrote: | | Hey RSH; Just rewrite and reuse this thread for another Q ! |
Yes, sunburnt. Did it!
Ok.
I want to search for files with spaces ' ' in its names and replace them with underscores '_'.
There could be just a single one but also three or four of those spaces ' '.
How can I do this?
Thanks
RSH
_________________ Freiheit für Mollath - JETZT !!!
Free Mr. Mollath !!!
Send the People responsible to The Hague International Court for trial !!!
|
|
Back to top
|
|
 |
jrb

Joined: 11 Dec 2007 Posts: 972 Location: Smithers, BC, Canada
|
Posted: Fri 08 Mar 2013, 14:38 Post subject:
Re: How to replace spaces in file names with underscores? |
|
| R-S-H wrote: | | sunburnt wrote: | | Hey RSH; Just rewrite and reuse this thread for another Q ! |
Yes, sunburnt. Did it!
Ok.
I want to search for files with spaces ' ' in its names and replace them with underscores '_'.
There could be just a single one but also three or four of those spaces ' '.
How can I do this?
Thanks
RSH |
Here's a script I've been using for some time: | Code: | #!/bin/sh
if [ -d $1 ]; then #check if $1 is a directory
cd $1
for file in *; do mv "$file" `echo $file | sed -e 's/ */_/g' -e 's/_-_/-/g'`; done
else
mv "$1" `echo $1 | sed -e 's/ */_/g' -e 's/_-_/-/g'`
fi |
It will work for a single file or a directory of files.
_________________ SFS-TCZ_Linker, http://puppylinuxstuff.meownplanet.net/choicepup/ user=puppy, password=linux
|
|
Back to top
|
|
 |
R-S-H
Joined: 18 Feb 2013 Posts: 280
|
Posted: Fri 08 Mar 2013, 15:08 Post subject:
|
|
| jrb wrote: | Here's a script I've been using for some time: | Code: | #!/bin/sh
if [ -d $1 ]; then #check if $1 is a directory
cd $1
for file in *; do mv "$file" `echo $file | sed -e 's/ */_/g' -e 's/_-_/-/g'`; done
else
mv "$1" `echo $1 | sed -e 's/ */_/g' -e 's/_-_/-/g'`
fi |
It will work for a single file or a directory of files. |
Oh, thank you very much.
I knew it would be needed such stuff, not programmable by myself!
I will try it.
Thanks again
RSH
_________________ Freiheit für Mollath - JETZT !!!
Free Mr. Mollath !!!
Send the People responsible to The Hague International Court for trial !!!
|
|
Back to top
|
|
 |
seaside
Joined: 11 Apr 2007 Posts: 836
|
Posted: Fri 08 Mar 2013, 16:31 Post subject:
|
|
RSH,
I use this one-
| Code: | for file in *.mp* *.flv;do
newfile=${file// /_}
mv -v "$file" $newfile;done |
Usually in directories with downloaded media. It could be used for any file with just *.
Regards,
s
|
|
Back to top
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 3843
|
Posted: Fri 08 Mar 2013, 22:51 Post subject:
|
|
in current dir?
| Code: | | for f in *;do mv "$f" "${f// /_}";done |
or recursively?
| Code: | | for f in `find .`;do mv "$f" "${f// /_}";done |
Note: for find you can use -name or -iname to filter
_________________ Puppy Web Desktop Now with pet packages - Pet Packaging 100 & 101
Last edited by technosaurus on Sat 09 Mar 2013, 00:24; edited 1 time in total
|
|
Back to top
|
|
 |
Flash
Official Dog Handler

Joined: 04 May 2005 Posts: 9850 Location: Arizona USA
|
Posted: Fri 08 Mar 2013, 22:59 Post subject:
|
|
What was the original title of this thread?
|
|
Back to top
|
|
 |
jrb

Joined: 11 Dec 2007 Posts: 972 Location: Smithers, BC, Canada
|
Posted: Sat 09 Mar 2013, 00:00 Post subject:
|
|
@Seaside - Nice, simpler than mine. I changed to: | Code: | cd $1
for file in *;do
newfile=${file// /_}
mv -v "$file" $newfile;done |
and it changed all files in my folder.
| technosaurus wrote: | in current dir?
| Code: | | for f in *;do mv "$f" "${f// /_};done |
or recursively?
| Code: | | for f in `find .`;do mv "$f" "${f// /_};done |
Note: for find you can use -name or -iname to filter |
I believe you have an extra " in there. I changed to | Code: | | for f in *; do mv "$f" ${f// /_}; done |
and it worked very well.
I like your technique. Is there an easy way to do both of the sed functions that I have in mine? | Code: | | sed -e 's/ */_/g' -e 's/_-_/-/g' |
_________________ SFS-TCZ_Linker, http://puppylinuxstuff.meownplanet.net/choicepup/ user=puppy, password=linux
|
|
Back to top
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 3843
|
Posted: Sat 09 Mar 2013, 00:25 Post subject:
|
|
nope, it was missing one, fixed.
new=`echo $old` #squeeze all tabs and spaces to a single space
new="${new// /_} #you have to do these 1by1, but I think "," and [] work
new="${new//_-_/-} #technically this could be in the mv step
mv "$old" "$new"
_________________ Puppy Web Desktop Now with pet packages - Pet Packaging 100 & 101
|
|
Back to top
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 3843
|
Posted: Sun 10 Mar 2013, 02:54 Post subject:
|
|
I found this script (/function) while looking for something else:
| Code: | #!/bin/ash
sr(){
[ "$1" ] && [ -d "$1" ] && cd $1
for x in * ; do
y=${x// /_}
[ "$x" != "$y" ] && mv "${x}" "${y}"
[ -d $y ] && sr $y &
done
}
sr $@ | it recurses into subdirectories without needing find, but is missing logic to not follow symlinks into a circular loop if ... ya know, there's a circular loop
_________________ Puppy Web Desktop Now with pet packages - Pet Packaging 100 & 101
|
|
Back to top
|
|
 |
|