help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Loop with files coming through sorted by name


From: Seth David Schoen
Subject: Re: Loop with files coming through sorted by name
Date: Fri, 15 Oct 2021 20:31:04 -0700

Seth David Schoen writes:

> Typically people will suggest instead using the find command, either
> with the -exec flag or with the -print0 flag.  This is extensively
> described in
> 
> https://stackoverflow.com/questions/9612090/how-to-loop-through-file-names-returned-by-find
> 
> A disadvantage there is that the command run by the -exec can only be
> a single command, not a bunch of shell scripting or even a pipeline.

I didn't realize, but there is a newer approach which involves a bunch of
historically recent bashisms

https://stackoverflow.com/questions/23356779/how-can-i-store-the-find-command-results-as-an-array-in-bash/54561526#54561526

You can thus do

readarray -d "" myarray < <$(find . -type f -print0 | sort -zn)
for i in "${myarray[@]}"; do
  # arbitrary shell stuff with "$i"
done

Classical Bourne shell scripting it isn't. :-)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]