Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: for loop file order



* expands to the files sorted lexically by name under your current locale settings. If you want to sort them elsewise, you have to use something besides a wildcard. Easiest is probably to ask ls for them in the order you want; -t sorts by date, defaulting to newest first; adding -r switches to oldest first:

by_date=(${(f)"$(ls -1tr)"})

for aa in $by_date; do 

You can add -a to include hidden files, or -A to include hidden files other than "." and "..". Add -u to use time of last access instead of last modification.

FWIW, in your example code, it's more efficient if you redirect the entire loop instead of doing individual appends per-file:

for aa in whatever; do cat $aa; done >>$output

and if you're really doing cat, you don't need the loop at all.

cat * >>$output
cat $by_date >>$output

But all of the above mechanisms including the for loop expand all the filenames into a single shell command line, and so have limits on how big that expanded list can be. It's a large limit you are unlikely to run into in everyday circumstances, but as someone who remembers when all of cnn.com's weather data was stuffed into a single flat directory on the web server, I figure it's worth mentioning.  xargs, which will break up giant lists into smaller chunks, is a good way around such limits:

ls -1 | xargs cat >>contents-by-name
ls -1tr | xargs cat >>contents-by-modification_date
ls -1utr | xargs cat >>contents-by-access-date


On Mon, Nov 16, 2026 at 3:15 AM Stephane Chazelas <stephane@xxxxxxxxxxxx> wrote:
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



--
Mark J. Reed <markjreed@xxxxxxxxx>


Messages sorted by: Reverse Date, Date, Thread, Author