Getting the index of the last entry in a directory

Hello,

Is there anyway of getting the index of the last entry in a directory? I'm using a C program to read the entries, but I want to go to the last entry because the directory is very big and I don't want to read all. I was using the size of the directory file descriptor but when I remove files the size doesn't decrease. Any ideas? Thanks.

Something like

mos:/xtra $ ll|wc -l
2259
mos:/xtra $ ll -lt|head -2
total 1624588
-rw-rw-r--   1 oracle     dba           6645 Jun 20 23:00 oa....

When you do something like that, all the directory entries are read and only the latter two are printed. What I want is to go directly to the last entry. Has I said, the size of the directory file descriptor won't always work because when a directory grows, the file descriptor increases, but when files are removed, the file descriptor size doesn't decrease.

You want seekdir() and telldir() in dirent.h

However, be sure to read your manpage: seekdir() under POSIX basically has to know the offset ahead of time, there is no SEEK_END equivalent (as in lseek() ). Your implementation may behave differently.

The last entry in a directory with no subdirectories is the most recently created file. It can be of little value if you want the most recently updated file. Directory listings from "ls" always sort the directory.

# Last entry in directory
find . -type f -print | tail -1 | xargs ls -liad

Is this the same directory as in your previous posts (i.e. 180 Mb directory file). If so, did you manage to find out how many files there are in the directory and whether it has subdirectories?