Take the 2-minute tour ×
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
add comment

4 Answers

Have you tried

ls -U | head -4

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

http://stackoverflow.com/questions/40193/quick-ls-command

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
add comment

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 . -type d \! -name . -print -prune -o -print | head
share|improve this answer
add comment

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
add comment

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
add comment

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.