Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

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

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

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

5 Answers 5

up vote 3 down vote accepted

Try ls -f which does not sort.

share|improve this answer
    
Wow, that's a lot faster, and doing ls -f | sort | less is way faster too! Madness. –  BeepDog Oct 14 '12 at 1:56

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

share|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

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

share|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. –  hoju Nov 30 '09 at 23:35

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.

share|improve this answer

You can use head:

ls -l | head
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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