[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: RFE: new option affecting * expansion
From: |
Greg Wooledge |
Subject: |
Re: RFE: new option affecting * expansion |
Date: |
Mon, 16 Aug 2021 23:31:25 -0400 |
On Mon, Aug 16, 2021 at 10:35:12PM -0400, Dale R. Worley wrote:
> Back in the old, old days, there was a program named "glob" that did
> pathname expansions. So you wouldn't say
>
> cat *
>
> you'd say
>
> cat $( glob * )
Tcl still does it that way. Not with that syntax, but the command
name is "glob". (It doesn't sort them for you, though.)
> where glob would get one argument, "*", and output a list of file
> names. A glob-by-modification-date program would be a better solution
> for this need, IMHO.
Would this be a shell builtin, or a loadable bash builtin, or an external
program? Honestly it just sounds like more buck-passing.
There's a history of other projects refusing to provide this kind of
functionality, even when it would have been super easy, and even when
a patch was provided.[1]
Trying to reintroduce an external glob(1) utility will go over like a
lead balloon, I'm sure. Even if by some miracle you do manage to get
one included in a single Linux distribution, it won't be universally
available, which means it's pretty useless for shell scripting.
[1] https://lists.gnu.org/archive/html/coreutils/2014-02/msg00005.html
Right now, there are zero great ways to get a list of the files in a
directory sorted by mtime. If you happen to be on a recent GNU coreutils
system, you can use ls -t --quoting-style=shell-escape together with
bash's eval. This isn't too far off from your glob(1) suggestion,
but it only works on systems that have this particular version of ls(1),
and only in bash.
Otherwise, if you happen to have GNU find and GNU sort available, you
can cobble something together with find -printf '%T@ %p\0' and sort -zn.
It's clumsy and ugly, and it's still not portable, but it might be
an available option on some systems that don't have a new enough GNU
coreutils. Maybe.
What else... well, you could write your own Bubble Sort algorithm in bash,
using [[ $a -nt $b ]] to compare array entries and swap them. The good
news is that this would be portable to any system with bash. The bad
news: speed? What is speed? I guess you could live with it for a typical
log directory where you only have like 30 files (one month's worth). It
wouldn't be fun or elegant, though.
The most common solution? "Redesign your whole application so that the
filenames contain ASCII-sortable timestamps."
An option to change the glob sorting order would actually be useful,
not for a large number of scripts, but for a very specific problem domain
where the shell is often used. Is it critically important? No. But it
would be helpful.