LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 11-08-2004, 04:08 AM   #1
shy
Member
 
Registered: Dec 2002
Location: Russia
Distribution: ASP linux
Posts: 94

Rep: Reputation: 15
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:
Code:
a b
c d
?
 
Old 11-08-2004, 05:09 AM   #2
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
I hope this helps:
Code:
#!/bin/bash

OLDIFS="$IFS"
IFS='
'
for fic in $(ls); do
        echo $fic
done
IFS="$OLDIFS"
Yves.
 
Old 11-08-2004, 05:15 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 683Reputation: 683Reputation: 683Reputation: 683Reputation: 683Reputation: 683
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.
 
Old 11-08-2004, 05:28 AM   #4
shy
Member
 
Registered: Dec 2002
Location: Russia
Distribution: ASP linux
Posts: 94

Original Poster
Rep: Reputation: 15
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// /_}?
 
Old 11-08-2004, 07:18 AM   #5
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
`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.
 
Old 11-08-2004, 07:43 AM   #6
shy
Member
 
Registered: Dec 2002
Location: Russia
Distribution: ASP linux
Posts: 94

Original Poster
Rep: Reputation: 15
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash scripting: filenames with spaces joanq Programming 2 05-24-2005 12:50 PM
Bash double spaces in filenames ilikejam Programming 2 02-01-2005 10:34 AM
Spaces in filenames with BASH edenning Programming 12 01-27-2005 07:10 AM
bash and filenames with spaces doctorwebbox Linux - General 2 12-15-2004 08:55 PM
BASH scripting problem, spaces in filenames / using basename textures Programming 24 11-16-2003 01:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration