LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-24-2005, 09:37 AM   #1
edenning
LQ Newbie
 
Registered: Jan 2005
Location: NY / NJ
Distribution: slackware10, debian woody, fedora 2
Posts: 6

Rep: Reputation: 0
Spaces in filenames with BASH


hi all,

im trying to output a list of directories/files. however, directory/file names that contain spaces appear on multiple lines of the output. is there anyway i can have the name print to only one line?

i got up to this point before reaching problems. ive tried a bunch of greps and seds but cant get further than this:

Code:
for file in `dir -d *` ; do
	echo ":: $file"
done
thanks alot,
edenning

Last edited by edenning; 01-24-2005 at 09:39 AM.
 
Old 01-24-2005, 10:34 AM   #2
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Try using:

Code:
for file in *; do echo "$file"; done
Not sure what you could do for showing only directories, though.
 
Old 01-24-2005, 10:35 AM   #3
Samsara
Member
 
Registered: May 2003
Distribution: Ubuntu, Mac OS X Tiger
Posts: 481

Rep: Reputation: 32
You can change the file separation character:
Code:
IFS=";"
for file in `dir -d *` ; do
	echo ":: $file"
done
You can also:
Code:
for file in "`dir -d *`" ; do
	echo ":: $file"
done
Regards,

Samsara

Last edited by Samsara; 01-24-2005 at 10:39 AM.
 
Old 01-24-2005, 11:27 AM   #4
edenning
LQ Newbie
 
Registered: Jan 2005
Location: NY / NJ
Distribution: slackware10, debian woody, fedora 2
Posts: 6

Original Poster
Rep: Reputation: 0
Thumbs up

thanks alot wapcaplet, that worked great not quite sure why i didnt think about that
 
Old 01-25-2005, 04:18 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,284

Rep: Reputation: 172Reputation: 172
if you just want directories
It's a lot simpler, just do:

Code:
for file in */;do

It's a shell expansion, so works for say, 'ls' too,
list subdirs in /etc:
Code:
ls -d  /etc/*/
little known or used feature of bash and ksh
 
Old 01-26-2005, 12:12 AM   #6
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
Code:
[/home/soumen] $ time ls -d /tmp/*/
/tmp/flashgot/           /tmp/kde-soumen/   /tmp/mkbootdisk.dS2945/  /tmp/soumen-5qcrd9/
/tmp/gconfd-soumen/      /tmp/lost+found/   /tmp/orbit-root/         /tmp/ssh-ROunU733/
/tmp/hsperfdata_soumen/  /tmp/mcop-soumen/  /tmp/orbit-soumen/

real    0m0.011s
user    0m0.010s
sys     0m0.000s
[/home/soumen] $ time echo /tmp/*/
/tmp/flashgot/ /tmp/gconfd-soumen/ /tmp/hsperfdata_soumen/ /tmp/kde-soumen/ /tmp/lost+found/ /tmp/mcop-soumen/ /tmp/mkbootdisk.dS2945/ /tmp/orbit-root/ /tmp/orbit-soumen/ /tmp/soumen-5qcrd9/ /tmp/ssh-ROunU733/

real    0m0.001s
user    0m0.000s
sys     0m0.000s
[/home/soumen] $
The more the subdirs, the more the difference.

HTH.
 
Old 01-26-2005, 03:45 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,284

Rep: Reputation: 172Reputation: 172
Hmm.
I think that depends a lot on what order you do it.
The echo call will be globbing from cache memory.

If you swap you get:

e.g:
Code:
time echo /usr/*/*/
<snip>

real    0m4.05s
user    0m0.08s
sys     0m0.36s


time ls -d  /usr/*/*/
<snip>

real    0m0.20s
user    0m0.04s
sys     0m0.16s

time echo /usr/*/*/
<snip>

real    0m0.15s
user    0m0.09s
sys     0m0.06s
So echo is faster.
but I wouldn't use ls -d in a for, I'd use:
Code:
time for x in /usr/*/*/; do : ;done
real    0m0.15s
user    0m0.09s
sys     0m0.06s
though very interesting point.
 
Old 01-26-2005, 10:17 PM   #8
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
Yeah really interesting. This is even better:

Code:
SF1B : /supmis/soumen > for i in tmp/auth/*/*/ ; do
> echo ok
> done
ok
SF1B : /supmis/soumen > for i in $(echo tmp/auth/*/*/) ; do
> echo ok
> done
ok
SF1B : /supmis/soumen > for i in $(ls tmp/auth/*/*/) ; do^Jecho ok^Jdone
tmp/auth/*/*/: No such file or directory
SF1B : /supmis/soumen > ls -l tmp/auth/*/*/
tmp/auth/*/*/: No such file or directory
SF1B : /supmis/soumen > ls -l tmp/auth
total 982
-rw-r--r--   1 supmis   supmis      7847 Feb  3  2004 15kapp.snapshot.030204
-rw-r--r--   1 supmis   supmis      7656 Feb  3  2004 all.user
-rw-r--r--   1 supmis   supmis       355 Feb  3  2004 auth_users.com
-rw-r--r--   1 supmis   supmis    484706 Nov 23 13:39 paswd.zip
SF1B : /supmis/soumen >
So it turns out, for altogether different reasons, ls is a better choice

Regards,
Soumen
 
Old 01-27-2005, 05:12 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,284

Rep: Reputation: 172Reputation: 172
Very interesting point.

e.g:
Code:
billym.primadtpdev>for x in /usr/plopp/*/ ;do^Jecho x=$x^Jdone
x=/usr/plopp/*/
Hmmm.
That will explain some funny file names
on the system!

fascinating.


Last edited by bigearsbilly; 01-27-2005 at 05:14 AM.
 
Old 01-27-2005, 06:43 AM   #10
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
If you just want directories, try this find statement....
Code:
find /home/images -depth -type d -exec ls -d {} \;
If you want to display everything, try this find statement...
Code:
find /home/images -depth -name '*' -exec ls {} \;

or

find /home/images -depth -exec ls {} \;
 
Old 01-27-2005, 06:46 AM   #11
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
Isn't -depth taken by default?
 
Old 01-27-2005, 06:52 AM   #12
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Yeah, that's just a canned example I use and change a few things around for different situations.
 
Old 01-27-2005, 07:10 AM   #13
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,284

Rep: Reputation: 172Reputation: 172
we were just discussing the various quirks of
certain methods.
 
  


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
bash and filenames with spaces doctorwebbox Linux - General 2 12-15-2004 08:55 PM
bash, loops and spaces in filenames shy Programming 5 11-08-2004 07:43 AM
BASH scripting problem, spaces in filenames / using basename textures Programming 24 11-16-2003 01:41 AM


All times are GMT -5. The time now is 06:22 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration