sorting directory entries

sorting directory entries

Post by IB » Tue, 11 Aug 1998 04:00:00



My question is this:

        I would like to go through the contents of a directory,
filter out only the file extension I'm interested in, then sort
them in file creation order.

        I've looked at the man pages for scandir & dirent, and I
can't find anything that would let me sort on file creation time.

        Do I have to use scandir with a compare function that
opens each directory entry & calls fstat every time?
        I'm hoping their is a quicker & simpler way.

Thanks for your help

Indranil    

 
 
 

sorting directory entries

Post by voi » Wed, 12 Aug 1998 04:00:00


If I were you, I'd take a look at the source to ls and see how it does
this.



Quote:

>My question is this:

>    I would like to go through the contents of a directory,
>filter out only the file extension I'm interested in, then sort
>them in file creation order.

>    I've looked at the man pages for scandir & dirent, and I
>can't find anything that would let me sort on file creation time.

>    Do I have to use scandir with a compare function that
>opens each directory entry & calls fstat every time?
>    I'm hoping their is a quicker & simpler way.

>Thanks for your help

>Indranil    

--

 Ben

looking for admin/security work

 
 
 

sorting directory entries

Post by Steve Rountre » Fri, 14 Aug 1998 04:00:00




>My question is this:

>    I would like to go through the contents of a directory,
>filter out only the file extension I'm interested in, then sort
>them in file creation order.

>    I've looked at the man pages for scandir & dirent, and I
>can't find anything that would let me sort on file creation time.

>    Do I have to use scandir with a compare function that
>opens each directory entry & calls fstat every time?
>    I'm hoping their is a quicker & simpler way.

>Thanks for your help

>Indranil    

  I would create a structure that contains a character string for the file name
and either an entire file stat structure or just the field that you are
interested in (ctime).

  I would open the directory and read the entries, matching the entry name
against those that meet your extension criteria.

  Valid files I would stat and then load their information into one of the
aforementioned structures.  No need to actually open the file and incur the
additional overhead.

  Place this structure into a binary tree that is sorted by your field/value of
interest.

  When all pertinent files have been found do an inorder traversal of your
binary tree and your output is in the correct order.

 - Steve Rountree
+-------------------------------------------------+
| Incomprehensible?  But because you can not      |
| understand a thing, it does not cease to exist. |
|                                                 |
|                               - Pascal (1670)   |
+-------------------------------------------------+

 
 
 

sorting directory entries

Post by David Ste » Sat, 15 Aug 1998 04:00:00



>   I would open the directory and read the entries, matching the entry name
> against those that meet your extension criteria.

>   Place this structure into a binary tree that is sorted by your field/value of
> interest.

This may also be a problem where a language such as C++ or Objective-C
(see http://sunsite.unc.edu/pub/Linux/devel/lang/objc/), may provide a
reusable class, so that it's (a little bit) less work.  Like in ...

  p = opendir(dirname);
  files = [SortedCollection new];
  while ( (e=readdir(p)) ) [files add:[Entry dirent:e]];
  closedir(p);

 
 
 

sorting directory entries

Post by !**?#! » Sat, 15 Aug 1998 04:00:00


Quote:> ) >   I would open the directory and read the entries, matching the entry name
> ) > against those that meet your extension criteria.
> ) >
> ) >   Place this structure into a binary tree that is sorted by your
field/value of
> ) > interest.
> )
> ) This may also be a problem where a language such as C++ or Objective-C
> ) (see http://www.veryComputer.com/), may provide a
> ) reusable class, so that it's (a little bit) less work.  Like in ...
> )  
> )   p = opendir(dirname);
> )   files = [SortedCollection new];
> )   while ( (e=readdir(p)) ) [files add:[Entry dirent:e]];
> )   closedir(p);

This is a unix group, and on unix the simple solution is popen("ls *.ext","r").

Isn't popen inefficient? Sure, if this in a principal, innermost loop.
Otherwise, more cycles and programmer time have been consumed discussing
this than will ever be recoverred by the most efficient solution. It's a
hard lesson to learn, but the lesson is: don't waste time and effort by
optimising what is trivial.

--
They wait apart in waning day,      |       I don't use no smilies.

They rest their *, the rest is silence.|   www.geocities.com
Their empty years are ash and clay. | /SoHo/Studios/5079/index.html

 
 
 

sorting directory entries

Post by David Ste » Sat, 15 Aug 1998 04:00:00



> This is a unix group, and on unix the simple solution is popen("ls *.ext","r").

Yes, but with a -c flag to sort by creation time, and then dig the names
out of the byte stream (using fgets, malloc(), realloc() and strdup()).
 
 
 

1. sort sort: 0653-657 A write error occurred while sorting (4.1.3)

On a 4.1.3 system, we intermittently are seeing:

sort: 0653-657 A write error occurred while sorting

There is nothing in errpt, and the destination file system of
the sort is certainaly not full.

Does anyone have any suggestions as to what this could mean?

Thanks.

p.s. the actual sort operation is:

sort -d -f -u +0 -1 +1 -2 funds.load | awk '
  { printf "%s %s %s\n", $1, $2, $3 }' >funds.sorted

 *or it might be* :

sort -d -f -u +0 -1 acc_codes.update >acc_codes.sorted

(this is not my code, and I'm not familiar with its design).

Thanks,

--

2. foundry switch trunking and ethernet bonding

3. sort(1) closes fds on entry - bug?

4. Help-ports

5. I NEED ASSISTANCE: reversing sorted entries

6. How do you tell the OS to boot thru to a desktop environment?

7. too many entries too many entries too mant entries in the KDE menu

8. Cdrecord errors using Plextor USB burner

9. listing directory sizes in linux in a sort of similar way to treesize

10. Sorting files into directories by date

11. Sorting a directory in place

12. sorting directory list

13. cmdline sorting directory by size|size reverse