LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Spaces in filenames with BASH (https://www.linuxquestions.org/questions/programming-9/spaces-in-filenames-with-bash-281613/)

edenning 01-24-2005 09:37 AM

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

wapcaplet 01-24-2005 10:34 AM

Try using:

Code:

for file in *; do echo "$file"; done
Not sure what you could do for showing only directories, though.

Samsara 01-24-2005 10:35 AM

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

edenning 01-24-2005 11:27 AM

thanks alot wapcaplet, that worked great :) not quite sure why i didnt think about that

bigearsbilly 01-25-2005 04:18 AM

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

dustu76 01-26-2005 12:12 AM

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.

bigearsbilly 01-26-2005 03:45 AM

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.

dustu76 01-26-2005 10:17 PM

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

bigearsbilly 01-27-2005 05:12 AM

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.


homey 01-27-2005 06:43 AM

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 {} \;


dustu76 01-27-2005 06:46 AM

Isn't -depth taken by default?

homey 01-27-2005 06:52 AM

Yeah, that's just a canned example I use and change a few things around for different situations. :)

bigearsbilly 01-27-2005 07:10 AM

we were just discussing the various quirks of
certain methods.


All times are GMT -5. The time now is 07:19 AM.