Bash for loops, spaces in variables

Tim Aslat tim at spyderweb.com.au
Tue Nov 28 00:44:09 CST 2006


In the immortal words of Andrew Reid  on 11/27/06 23:00:
> Suppose I have a bunch of files and directories, like so:
> 
> /tmp
> /tmp/My Photos
> /tmp/Your Photos
> /tmp/Her Photos
> 
> If I iterate over that list of directories, using something like:
> 
> for i in `ls /tmp/*` do
> echo $i
> done
> 
> I get a list like
> 
> My
> Photos
> Your
> Photos
> Her
> Photos
> 
> What I need is a for each item to be the complete path. Unfortunately, I 
> can't alter the input (I'm using vmware-cmd -l for this task), so I need 
> to do something in the script to make sure things don't get stuffed up 
> by having spaces in the file names.
> 
> Suggestions?

in this case, find is your friend.

something like this should work

find /tmp -type f -print0|xargs -0 echo

or

find /tmp -type f |xargs -I% echo '%'

Hope this helps

Cheers

Tim



-- 
Tim Aslat <tim at spyderweb.com.au>
Spyderweb Consulting
http://www.spyderweb.com.au
Mobile: +61 0401088479


More information about the linuxsa mailing list