Spaces in files

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Spaces in files

Post by Ollie Saunders »

I want to iterate over the results of a find. So I've been doing this:

Code: Select all

index=0
for file in `find`
do
    echo $index $file
    index=`expr $index + 1`
done
Only the problem is if I run this in a directory with contents like this

Code: Select all

-rw-r--r-- 1 root root 2 2007-10-02 11:56 bar
-rw-r--r-- 1 root root 2 2007-10-02 11:56 foo
-rw-r--r-- 1 root root 2 2007-10-02 11:57 foo bar
I'll get output like this:

Code: Select all

0 .
1 ./foo
2 ./bar
3 ./foo
4 bar
you can see that "foo bar" is being separated into "foo" and "bar". Any ideas how I can prevent this?

The normal output of find will be able to distinguish by using a newline, can i parse that into an array somehow?

Code: Select all

.
./foo
./bar
./foo bar
jeffery
Forum Contributor
Posts: 105
Joined: Mon Apr 03, 2006 3:13 am
Location: Melbourne, Australia
Contact:

Post by jeffery »

I encountered this sometime back. Here is an example:

Code: Select all

find $1 -type f -print0 | while read -d '' -r i;
    do
        var="$i";
    done
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

What's the purpose of the script? I'm asking because "find -exec" can be very useful sometimes :)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Well, I don't understand how that works, I mean, an empty string as a delimiter, what's all that about? But it seems to do the job fine and dandy. Thanks!
Post Reply