Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 30 Jul 2014, 21:32
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
How to replace spaces in file names with underscores?
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [11 Posts]  
Author Message
R-S-H

Joined: 18 Feb 2013
Posts: 490

PostPosted: 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

_________________
LazY Puppy Home
The new LazY Puppy Information Centre


Last edited by R-S-H on Fri 08 Mar 2013, 13:17; edited 2 times in total
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 5010
Location: Arizona, U.S.A.

PostPosted: Mon 04 Mar 2013, 20:13    Post subject:  

Hey RSH; Just rewrite and reuse this thread for another Q !
Back to top
View user's profile Send private message 
R-S-H

Joined: 18 Feb 2013
Posts: 490

PostPosted: 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! Laughing

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

_________________
LazY Puppy Home
The new LazY Puppy Information Centre

Back to top
View user's profile Send private message 
jrb


Joined: 11 Dec 2007
Posts: 1030
Location: Smithers, BC, Canada

PostPosted: 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! Laughing

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.
Back to top
View user's profile Send private message 
R-S-H

Joined: 18 Feb 2013
Posts: 490

PostPosted: 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! Laughing

I will try it.

Thanks again

RSH

_________________
LazY Puppy Home
The new LazY Puppy Information Centre

Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 886

PostPosted: 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
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4285

PostPosted: 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

_________________
Web Programming - Pet Packaging 100 & 101

Last edited by technosaurus on Sat 09 Mar 2013, 00:24; edited 1 time in total
Back to top
View user's profile Send private message 
Flash
Official Dog Handler


Joined: 04 May 2005
Posts: 10948
Location: Arizona USA

PostPosted: Fri 08 Mar 2013, 22:59    Post subject:  

What was the original title of this thread?
Back to top
View user's profile Send private message 
jrb


Joined: 11 Dec 2007
Posts: 1030
Location: Smithers, BC, Canada

PostPosted: 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'
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4285

PostPosted: 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"

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4285

PostPosted: 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
_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [11 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0717s ][ Queries: 12 (0.0042s) ][ GZIP on ]