An issue with find command.

Hi all,

I have a shell script(ksh) which has the code as follows.
------------------
cd $mydir

for i in `find ./ -type f -mtime +$k`
do
echo $i
done
-----------------------

And in $mydir , i have some files which have space in theie names like "Case att15".

The out put of the script is
./Case
./att15
where as i want the script to recognise the file as a single file.

I think this has something to do with IFS.
Can anyone pls. suggest what do i need to do??

Thanks in advance,
raju

It is very hard to support files with white space characters in their names. The case you mentioned can be handled with:

#! /usr/bin/ksh
IFS=""
find . -type f -mtime +$k | while read i ; do
      echo $i
done

I suspect you want to do more ... sophisticated processing in the loop than just echoing the file name. With that in mind, I suggest this slight modification to Perderabo's code:

find . -type f -mtime +$k | while read i ; do
    echo "$i"
done

I.e., for doing things with the files, you need to quote each file name as it comes through.

Good catch, criglerj! Actually those quotes in the echo statement are needed just to print the name if there are several contiguous spaces.

Thanks for that addition. After I read your reply, I checked it and you were right, of course. I'm glad to learn that!

hi,

by th way, you can get into trouble if changing the IFS, because the shell needs it sometimes for her work (i�m german so i don�t know exactly if shell is male or female, but sometimes the shell reacts as a female :wink: ) so make before a copy and undo it afterwards like this

OIFS="$IFS"
IFS=""
..
code
..
IFS="$OIFS"

cu

Actually, it is old Bourne shell that misbehaves if you change IFS. The Korn shell does not use IFS to intrepret the commands in the script. I often set IFS="" at the top of a ksh script and leave set to that value until I need a different value.

This mostly happens in two cases: when I want to split the data obtained via a "read" statement into words, and when I want to initialize an array with words that are all currently stored in a single variable. An occaisional third case is in some very complex uses of "eval".

Other than that, there is no harm in setting IFS as long as you are using ksh.