My Linux file system has tens of thousands of files per directory. Viewing a list of all the files takes a few minutes for my system to calculate, which is too slow. But I am not sure what files are in each directory so want to view a list of the first few, like what the head command does for large files. Is there a command that efficiently does this?

Edit: Piping ls to something else is no good because ls takes a few minutes to complete, before piping the output. I need a command that will only return the first few records.

link|improve this question

Why so many files in a directory? – mouviciel Nov 30 '09 at 13:26
Does it matter? Anyway, it is downloaded web content. – Richard Nov 30 '09 at 23:31
feedback

migrated from stackoverflow.com Nov 30 '09 at 14:14

This question came from our site for professional and enthusiast programmers.

5 Answers

up vote 2 down vote accepted

Try ls -f which does not sort.

link|improve this answer
feedback

For each of the directories you can do the following by a shell script

Try this, this gives one page full of entries

ls | more

You can even push this to some file if you want so that you can refer to it later, for that use this

ls | more > filenames.txt

Thanks

link|improve this answer
+1 despite more used just before a redirection – mouviciel Nov 30 '09 at 13:25
"less is more" - If you have less use it instead of more – Dennis Williamson Nov 30 '09 at 14:20
There is also "most" (someone had a hard time naming his program :) – alfplayer Dec 1 '09 at 0:00
feedback

If you want to list N first entries, I would just create a small C program that does an opendir call and then N calls to readdir

link|improve this answer
that's the same as head the list of files. and head or ls are C programs – Anonymous Nov 30 '09 at 14:08
I would rather not create my own script because I would want a solution that works on a few machines, without having to copy my script around. – Richard Nov 30 '09 at 23:35
feedback

It depends on the filesystem, but it's usually quicker to list filenames only (look at the directory object) versus all the file metadata (saves one stat() per file).

Have you tried ls -R?

Otherwise, it might pay you to run the GNU locate program in the background periodically, and use the generated DB to track it.

link|improve this answer
feedback

You can use head:

ls -l | head
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.