Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Is there a way to limit the quantity of listed files on a ls command?

I've seen:

ls | head -4

but to get head to be executed I need to wait for ls to finish execution, and with directories with an enourmous quantity of files that can take considerable time.

I wish to execute a ls command that limits without using that head command.

share|improve this question

6 Answers 6

Have you tried

ls -U | head -4

This should skip the sorting, which is probably why ls is taking so long.

share|improve this answer
"ls -U" still reads the entire directory before print... I think that I'll write a small script to limit the files, but that question link is good reading material. Thanks niko. –  AndreDurao Sep 27 '11 at 16:43
@AndreDurao, GNU ls -U does not necessarily read the entire directory before printing. Try strace -e getdents,write ls -U > /dev/null in a large directory for instance. –  Stéphane Chazelas May 14 at 15:58

If your version of ls has a way not to sort files, such as -U for GNU ls, use it. With no option, ls will first read all the files, then sort the names, then start printing.

Another possibility is to run find, which prints names as it finds them.

find . -name . -o -prune | head

(note that since head is working on lines, that assumes file names don't contain newline characters).

share|improve this answer
Note that in the case of GNU find (as opposed to busybox or heirloom find or GNU ls -U), it seems it reads the whole directory before starting to print. –  Stéphane Chazelas May 14 at 16:11

Perhaps you are in need of a tool other than ls?

For example, Randal Schwartz has a blog entry about using perl on large directories that may contain some hints on building something that meets your needs.

In the blog posting Randal explains that both ls and find attempt to read in all directory entries before printing any, while the perl solution he proposes does not.

share|improve this answer
I also thought that could be the best option, because both ls and find read the entire directory before printing. I was planning to write a ruby script to do that instead of pearl, thanks for that AFresh1! –  AndreDurao Sep 27 '11 at 16:37
@AndreDurao, perl probably uses readdir(3) like ls or find. readdir(3) on current versions of GNU/Linux at least does call the getdents() system call with a large count (that actually does generally optimise performances by reducing the number of system calls being made). But in your case, if you want fewer files, it looks like you'd have to bypass readdir and use BSDs getdirentries(3) or the getdents(2) system call instead. –  Stéphane Chazelas May 14 at 16:35

Maybe less is better suited for your needs?

 ls /usr/bin | less

For me, it works instantaneously on a 5 years old laptop with classic HDD, but head is equally fast.

You can terminate less prematurely with q.

I guess your assumption about the source of the 1s delay is wrong, but maybe depends on your Unix-flavour or your shell, less or head command.

On Linux, with GNU-ls,

 ls -R /usr | less 

starts outputting immediately for me, while the whole output is running und running - so it is definitively not finished, before less starts. You might check, if you have a constant delay of 1s or maybe more, depending on the output or not.

I guess your 1s delay has a different reason, maybe the HDD is going to sleep and needs a wakeup?

Do you have such a delay for very few files too?

share|improve this answer
Thanks but I wasn't looking for something like that. Just like head the less is executed with the result of the entire ls result. I was looking for a way to ls itself to limit the results qty. –  AndreDurao Jan 24 '12 at 11:40
@AndreDurao: Moved my comments into the answer. –  user unknown Jan 25 '12 at 16:05

If performance is not the concern (as in the question that was closed as a duplicate of this one), and you want to list the first n files (as opposed to the first n lines of the output of ls) in the list of files sorted by filenames, with zsh, you can use:

ls -ld -- *([1,4])

to list the first 4 files. zsh will still read the full content of the directory though, even if you tell it not to sort with *(oN[1,4]) (note that ls also sorts that list).

share|improve this answer
ls -lrth | tail

ls -lrth | tail -n 10

ls -lrth | grep *.gz | tail
share|improve this answer

Your Answer


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.