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 Sun 21 Dec 2014, 15:37
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 Posts_count  
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


Edited_times_total
Back to top
View user's profile Send_private_message 
sunburnt


Joined: 08 Jun 2005
Posts: 5043
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: 1040
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: 888

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

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

Edited_time_total
Back to top
View user's profile Send_private_message 
Flash
Official Dog Handler


Joined: 04 May 2005
Posts: 11181
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: 1040
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: 4424

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

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:   Sort by:   
Page 1 of 1 Posts_count  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


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