LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   filenames with spaces (http://www.linuxquestions.org/questions/programming-9/filenames-with-spaces-368484/)

antony.booth 09-30-2005 10:36 AM

filenames with spaces
 
I am trying to process a directory and determine attributes of each file. I am using a for x in $( ls ) loop to iterate through the directory, but files with spaces in the name populate ${x} for each word. E.G.

A simple script to list directories

for i in $( ls );
do
if [ -d "${i}" ]; then
echo ${i};
fi;
done

If I have a subdirectory called "This is a file" (without the quotes)

It will look for files: -

This
Is
A
File

which do not exist.

Anyone have any ideas how best to list the directory and just get the filenames individually?

Many Thanks.

deiussum 09-30-2005 11:11 AM

Change the IFS variable like so. (For more info on this variable, try man bash and search for IFS)

Code:


IFS='
'

for i in $( ls -1 );
do
if [ -d "${i}" ]; then
echo ${i};
fi;
done


anomie 09-30-2005 11:11 AM

Try
Code:

ls | awk '{ print }'
And find the person who is creating filenames with spaces in them - make him sit in the corner for awhile.

koodoo 09-30-2005 09:04 PM

Check this out :
Code:


#!/bin/sh
for i in *;
do
if [ -d "$i" ]; then
echo "$i";
fi;
done

Hope this helps !


PS : Don't forget to put quotes around $i

bigearsbilly 10-01-2005 06:39 AM

koodoo wins

or:
Code:


ls | while read file;do
  echo $file
done


RGummi 11-01-2005 04:49 AM

I use the following

oldIfs=$IFS
IFS='
'
l=$(find $srcFolder -iname '*.jpg')
for jpgitem in $l; do
IFS=$oldIfs
# do what to do
# cp -v "$jpgitem" "$targetFolder
IFS='
'
done

RGummi


All times are GMT -5. The time now is 12:22 PM.