Will Smith
Will Smith

Reputation: 11

list of files in a directory

I'm looking for a way to list the first 100 files (by created date) in a directory without having to first do a full listing of the directory and then piping it to another utility to truncate the results. The reason being that there a LOT of files in the directory.

Running (ls -l | head -n 100) takes too long to complete the first part. I'd like to quit once I get the first 100 without having to read full directory contents.

Is this possible to do in RHE Linux?

Upvotes: 1

Views: 79

Answers (2)

kjohri
kjohri

Reputation: 1224

Since you are processing directory from a program and not the command line, the system calls to process directory, opendir, closedir and readdir should be used. It is straightforward to use these system calls from a C program. I think there is a way to use these calls from Java as well.

Upvotes: 0

Paul J
Paul J

Reputation: 1519

find seems to be slightly quicker than ls. I was looking at a directory with a thousand empty files.

The time command is useful for ... you guessed it ... determining how long stuff takes.

time ls -l | head -n 100
real    0m0.014s
user    0m0.007s
sys     0m0.008s

time ls -1 |head -n 100
real    0m0.009s
user    0m0.006s
sys     0m0.006s

time find . -maxdepth 1 -type f |head -n 100
real    0m0.007s
user    0m0.003s
sys     0m0.005s

Upvotes: 1

Related Questions