spaces in filenames

Hi

I hope someone will be able to resolve this little teaser!

I am running a script

for file in `ls directory`
do
echo "$file"
...other code here....
done

this works fine unless we receive a file with a name which has a space in it

ie

"filena me"

(I know its not good to have a space in a filename but we have no control over the filenames which are ftp'd to us.)

If the file "filena me" is in the directory, the above script will do the following:

filena
...do other stuff....

me
...do other stuff....

whereas I would like it to do

filena me
....do other stuff....

Can anyone help me please?

Thanks
Helen :confused:

Replace
for file in `ls directory`
with
cd directory
for file in *

Thanks Perderabo, This works fine!:wink:

u can also use

for fine in `ls directory | tr -s " "`
{
loop
}

I am trying to run the following

for fname in *
do
echo $fname
done

to list all the files in a specific directory. This does not seem to include the files starting with . character (e.g .classpath)

then i modified the program to include ls -A

for fname in $(ls -A)
do
echo $fname
done

This is including the . files. But it splits the file containing spaces into multiple entries (Hello name.txt => Hello, name.txt)

Just wondering is there a way to modify the * option to include the hidden files.

Any help is very much appreciated.

Thanks
ben

Try:
for name in * .* ; do

Get the spacing right! First asterisk is surrounded by spaces, 2nd is not.

Hi Ben,
I think "ls -A" is the best way to go, using "ls * .*" will also list contents of directories, even above, and I guess You don't want that. Well, just a guess anyway.

One of the beauties of ls (and many other utilities) is that when piped to another program it will regard each file/entry as a line, as opposed to when written to screen where it will all appear continuously, also when produced as a string for the "for fname in " construct.

So if You use "while" and try something like this instead,

ls -A | while read fname ; do echo $fname ; done

maybe You will be a bit closer to a solution? It of course depends on what You want to do with the names, are the entries interesting only if they are files, then use something like

ls -A|while read line;do [ -f "$line" ]  && echo $line;done

/Lakris

Thanks Lakris & Perderabo for your response

I tried the solution for fname in *. *, It worked.

The other solution suggested by Lakris also worked.

Can i use a simlar way to tackle the problem for the directory listing(directories containing spaces)?

i have the following,

for dir in $(find . -type d)
do
echo $dir
done

Converted to

find . -type d | while read dir
do
echo $dir
done

Just wondering whether are there any other specific things that i need to worry about here?

Thanks again for the earlier responses.

Ben

Hi again!
Yes, in general, whenever You want to use the $dir variable containing spaces other than just displaying it, for example as a parameter to another program, You must "protect" the spaces, using quotes.

But beware, it is common to get frustrated and confused (I have been several times) when You don't get the quotes right.

/L