1

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.

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

5 Answers 5

4

Try ls -f which does not sort.

1
  • Wow, that's a lot faster, and doing ls -f | sort | less is way faster too! Madness.
    – BeepDog
    Oct 14, 2012 at 1:56
0

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

3
  • +1 despite more used just before a redirection
    – mouviciel
    Nov 30, 2009 at 13:25
  • "less is more" - If you have less use it instead of more Nov 30, 2009 at 14:20
  • There is also "most" (someone had a hard time naming his program :)
    – alfplayer
    Dec 1, 2009 at 0:00
0

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

2
  • that's the same as head the list of files. and head or ls are C programs
    – Anonymous
    Nov 30, 2009 at 14:08
  • 1
    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, 2009 at 23:35
0

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.

0

You can use head:

ls -l | head

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .