Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was wondering if there is a way to specify the first N files under a given a directory in zsh.

I am interested in solutions for recursive enumeration (i.e. any file recursively below a path is considered for the enumeration) and non-recursive (only those files strictly under a given folder are considered).


share|improve this question
up vote 9 down vote accepted

It's zsh, so indeed there's a glob qualifier for that.

echo *([1,42])         # The first 42 files in the current directory, in lexicographic order
echo **/*([1,42])      # The first 42 files in a depth-first traversal
echo **/*(od[1,42])    # The first 42 files in a breadth-first traversal

Other qualifiers that might be useful, for example the following expression includes dot files (D), restricts to regular files (.) and symbolic links (-) to regular files, and expands to an empty list if there is no match (N):

echo *(-.DN[1,42])
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.