Operating System - HP-UX
1846714 Members
3279 Online
110256 Solutions
New Discussion

Sort order of files in "find" results

 
SOLVED
Go to solution
Carlo Henrico_1
Regular Advisor

Sort order of files in "find" results

If I do a:

find . -name "STATS*" -exec abc.sh {} \;

the files are returned in an order not expected. I expect them to be returned in an alphabetic order.

The STATS* files have a date eg:
STATS20050203.txt
STATS20050201.txt
STATS20050202.txt

It seems as if they are returned in a date/time last updated order instead of "alphabetic" order (i.e. date order for the purposes of the abc.sh script).

ls -l does return them in the expected order.

How do I rectify this?

Thanks

Carlo
Live fast, die young - enjoy a good looking corpse!
7 REPLIES 7
Muthukumar_5
Honored Contributor
Solution

Re: Sort order of files in "find" results

Yes.

Try as,

for file in `find . -name "STATS*" | sort`
do
./abc.sh $file
done

hth.
Easy to suggest when don't know about the problem!
harry d brown jr
Honored Contributor

Re: Sort order of files in "find" results

find . -name "STATS*" -exec abc.sh {} \; | sort

live free or die
harry d brown jr
Live Free or Die
Muthukumar_5
Honored Contributor

Re: Sort order of files in "find" results

You can try as,

find . -name "STATS*" -exec abc.sh {} \; | sort

first:
$PATH variable has to have . included to execute it with out full path name or ./

second:
If you are abc.sh is going executing sorted files then it will be a problem more.

hth.


Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Sort order of files in "find" results

sorry. Prev. post says you can try as, But it is "you can not try as" brown's reply.
Easy to suggest when don't know about the problem!
harry d brown jr
Honored Contributor

Re: Sort order of files in "find" results

If you need the files sorted BEFORE you execute abc.sh FILENAME, then use this:

find . -name "STATS*" | sort | xargs -i ./abc.sh {}

live free or die
harry d brown jr
Live Free or Die
Rodney Hills
Honored Contributor

Re: Sort order of files in "find" results

Directories are files that contain a list of filenames and associated inode. The find command just processes the filenames in the order they happen to be in in the directory.

Example-
$ mkdir newdir
$ cd newdir
$ touch c b a
$ find . -print
.
./c
./b
./a
$ rm b
$ touch d
$ find . -print
.
./c
./d
./a

Notice the first find displayed the names in the order they were created. But when I deleted file "b" and created "d", it put "d" in the slot where "b" was.

The reason ls lists the files sorted by filename is because ls does an internal sort.

HTH

-- Rod Hills
There be dragons...
Gregory Fruth
Esteemed Contributor

Re: Sort order of files in "find" results

I don't believe "find" is guaranteed to
return its results in any particular
order, because the underlying directory
structure doesn't guarantee any particular
order, either. If you want the "find"
results to be in some order
(time/alpha/size/etc.) you MUST sort them
yourself, typically using "sort".