Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: for loop file order
- X-seq: zsh-users 30755
- From: Stephane Chazelas <stephane@xxxxxxxxxxxx>
- To: Ray Andrews <rayandrews@xxxxxxxxxxx>
- Cc: Zsh Users <zsh-users@xxxxxxx>
- Subject: Re: for loop file order
- Date: Mon, 16 Nov 2026 08:08:59 +0000
- Archived-at: <https://zsh.org/users/30755>
- In-reply-to: <db0d5bca-690d-4b05-b918-503b21e2feda@eastlink.ca>
- List-id: <zsh-users.zsh.org>
- Mail-followup-to: Ray Andrews <rayandrews@xxxxxxxxxxx>, Zsh Users <zsh-users@xxxxxxx>
- References: <db0d5bca-690d-4b05-b918-503b21e2feda@eastlink.ca>
2026-11-15 19:59:52 -0700, Ray Andrews:
> for aa in *; do cat $aa >> output; done
>
> ... is there some way of controlling the order in which the files are
> processed? I'd expect alphabetical order to be the default, but suppose
> size or date were wanted?
"for" loops over the elements you pass to it in the order you
pass them to it.
In
for i (2 1) echo $i
It outputs 2 and 1.
In your case, it loops over the expansion of the "*" glob. glob
expansions are sorted lexically by default, but the sorting
order can be affected by the o, O and n glob qualifiers (see
also the numericglobsort option).
Run
info zsh qualifiers
for details.
To concatenate all the non-hidden files in the current working
directory, [o]rdered by [L]ength:
set -o extendedglob
cat ./^output(-.oL) > output
(note that we wnat to exclude "output" itself from the glob
expansion, or you'd run into a feed loop and we don't need a
loop here, "cat" is the command to con[cat]enate).
The other glob qualifiers used here are:
- "-" so the following qualifiers consider files *after* symlink
resolution.
- "." to restrict the glob expansion to files of type "regular"
(so including symlinks eventually resolving to regular files
with the previous "-").
"-" also affects the [o]rdering as it's the [L]ength (apparent
size in bytes) of the target of the symlinks that is taken into
account for the ordering.
--
Stephane
Messages sorted by:
Reverse Date,
Date,
Thread,
Author