How to replace spaces in file names with underscores?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
R-S-H
Posts: 487
Joined: Mon 18 Feb 2013, 12:47

How to replace spaces in file names with underscores?

#1 Post by R-S-H »

Please have a look at the third post of this thread.

I do re-use this one.

Thanks
Last edited by R-S-H on Fri 08 Mar 2013, 17:17, edited 2 times in total.
[b][url=http://lazy-puppy.weebly.com]LazY Puppy Home
The new LazY Puppy Information Centre[/url][/b]

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#2 Post by sunburnt »

Hey RSH; Just rewrite and reuse this thread for another Q !

R-S-H
Posts: 487
Joined: Mon 18 Feb 2013, 12:47

How to replace spaces in file names with underscores?

#3 Post by R-S-H »

sunburnt wrote:Hey RSH; Just rewrite and reuse this thread for another Q !
Yes, sunburnt. Did it! :lol:

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
[b][url=http://lazy-puppy.weebly.com]LazY Puppy Home
The new LazY Puppy Information Centre[/url][/b]

User avatar
jrb
Posts: 1536
Joined: Tue 11 Dec 2007, 19:56
Location: Smithers, BC, Canada

Re: How to replace spaces in file names with underscores?

#4 Post by jrb »

R-S-H wrote:
sunburnt wrote:Hey RSH; Just rewrite and reuse this thread for another Q !
Yes, sunburnt. Did it! :lol:

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: Select all

#!/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.

R-S-H
Posts: 487
Joined: Mon 18 Feb 2013, 12:47

#5 Post by R-S-H »

jrb wrote:Here's a script I've been using for some time:

Code: Select all

#!/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! :lol:

I will try it.

Thanks again

RSH
[b][url=http://lazy-puppy.weebly.com]LazY Puppy Home
The new LazY Puppy Information Centre[/url][/b]

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#6 Post by seaside »

RSH,

I use this one-

Code: Select all

 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

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#7 Post by technosaurus »

in current dir?

Code: Select all

for f in *;do mv "$f" "${f// /_}";done
or recursively?

Code: Select all

for f in `find .`;do mv "$f" "${f// /_}";done
Note: for find you can use -name or -iname to filter
Last edited by technosaurus on Sat 09 Mar 2013, 04:24, edited 1 time in total.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#8 Post by Flash »

What was the original title of this thread?

User avatar
jrb
Posts: 1536
Joined: Tue 11 Dec 2007, 19:56
Location: Smithers, BC, Canada

#9 Post by jrb »

@Seaside - Nice, simpler than mine. I changed to:

Code: Select all

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: Select all

for f in *;do mv "$f" "${f// /_};done
or recursively?

Code: Select all

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: Select all

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: Select all

sed -e 's/  */_/g' -e 's/_-_/-/g'

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#10 Post by technosaurus »

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"
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#11 Post by technosaurus »

I found this script (/function) while looking for something else:

Code: Select all

#!/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
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

Post Reply