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

Re: for loop file order



2026-11-16 05:54:48 -0700, Ray Andrews:
> Thanks both:
> 
> >     for i (2 1) echo $i
> > 
> That I know.
> > 
> > 
> >     To concatenate all the non-hidden files in the current working
> >     directory, [o]rdered by [L]ength:
> > 
> >     set -o extendedglob
> >     cat ./^output(-.oL) > output
> > 
> Deep and powerful, but tricky as you explain.

The tricky part is that you need to remember to exclude the
output file from the list of input files. You'd have the same
problem with "ls".

You don't have the problem with things like:

cat dir/*.txt(Om) > merged.txt

(here reverse [O]rdering by [m]odification time).

> 
> On 2026-11-16 03:14, Mark J. Reed wrote:
> > * 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

That just moves the problem to file names containing newline
characters. You'd need recent versions of GNU ls and:

by_date=( ${(0)"$(ls --zero -tr)"} )

(where you could have just used by_date=( *(NOm) ))

by_date=( ${(0)"$(ls --zero -tr | grep -zxF output)"} )

To exclude "output" from the list of input files.

[...]
> > 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

You also need with

  cat -- $aa

Or

  cat < $aa

(the former not working properly if there's a file called "-" in
the current working directory).

That's why I used a ./ prefix.

And with "ls", you'd need:

by_date=( ${(0)"$(ls --zero -trd -- ./* | grep -zxF output)"} )

Also remember that if it's the files in "dir" you want to loop
over, you need:

for file in ${(0)"$(ls --zero -trd dir/*)"}; do
  ...
done

Or:

for file in ${(0)"$(ls --zero -tr dir/)"}; do
  file=dir/$file
  ...
done

If you do:

for file in dir/${(0)^"$(ls --zero -tr dir/)"}; do
  ...
done

That doesn't work properly, because the output of ls will be
something like file1\0file2\0. That's split on \0's with the 0
parameter expansion flag which yields 3 arguments: "file1",
"file2" and "". With ${(0)"$(ls --zero -tr dir/)"} alone, that
"" is ellided but not when prepending dir/ to each, so "dir/"
would be included in the list of files to loop over.

[...]
> > 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.

The limit is in the execve() system call, on the size of
arguments+environment passed to the invocation of an *external"
command.

That potentially affects:

ls --zero -trd -- ./*
cat ./*(oL)
cat -- ${(0)"$(...)"}

Where the "ls" or "cat" external commands may be passed a too
large number of files

but not in

for file in *(oL); do
  ...
done

for file in ${(0)"$(...)"}; do
  ...
done

print -rC1 -- *(oL)

Where there's no external command involved (you may run out of
memory, but that's usually not until you reach the millions on
files.

Using glob qualifiers means you can also sort by any arbitrary
sort criteria, and also filter the list of files by any
arbitrary criteria, which you can't do with "ls" alone.

-- 
Stephane




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