Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
11-08-2004, 04:08 AM
|
#1
|
Member
Registered: Dec 2002
Location: Russia
Distribution: ASP linux
Posts: 94
Rep:
|
bash, loops and spaces in filenames
Hi.
I'm frequently using bash scripts like this:
Code:
for i in `ls *`; do
#do something with $i
done
but I face some problems when files in current directory contain spaces in their names, because $i goes through words separated by spaces. For example,
Code:
$ touch "a b"; touch "c d";
$ for i in `ls *`; do echo $i; done
a
b
c
d
What should I do to force the output be like this:
?
|
|
|
11-08-2004, 05:09 AM
|
#2
|
Senior Member
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897
Rep:
|
I hope this helps:
Code:
#!/bin/bash
OLDIFS="$IFS"
IFS='
'
for fic in $(ls); do
echo $fic
done
IFS="$OLDIFS"
Yves.
|
|
|
11-08-2004, 05:15 AM
|
#3
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
Use double quotes around the variable.
example:
for song in *.mp3; do mv " ${song}" "${song// /_}"; done
will replace spaces in the filenames with the underscore.
You will need to place the variable in double quotes anyway in
case the filename might contain characters like '()*!' which are
not whitespace but have a special meaning to the shell.
|
|
|
11-08-2004, 05:28 AM
|
#4
|
Member
Registered: Dec 2002
Location: Russia
Distribution: ASP linux
Posts: 94
Original Poster
Rep:
|
Quote:
Originally posted by theYinYeti
I hope this helps:
|
Thanks, this helped
Quote:
Originally posted by jschiwal
for song in *.mp3; do mv " ${song}" "${song// /_}"; done
|
Well, quotes wasn't the case, you just don't use the output of ls, but use "*" directly. This is a bit simplier, thanks too.
But I'm wondering where can I learn more about renaming in the style of ${song// /_}?
|
|
|
11-08-2004, 07:18 AM
|
#5
|
Senior Member
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897
Rep:
|
`man bash` is a great source of information. Tip, after typing man bash[enter], type this: "/\#\#" and [enter] and you'll be right at the interesting location.
Yves.
|
|
|
11-08-2004, 07:43 AM
|
#6
|
Member
Registered: Dec 2002
Location: Russia
Distribution: ASP linux
Posts: 94
Original Poster
Rep:
|
Quote:
Originally posted by theYinYeti
`man bash` is a great source of information.
|
Thanks again, I was always to lazy to read `man bash` from start to end 
|
|
|
All times are GMT -5. The time now is 04:43 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|