[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH] ls: add --files0-from=FILE option
From: |
Pádraig Brady |
Subject: |
Re: [PATCH] ls: add --files0-from=FILE option |
Date: |
Tue, 20 Apr 2021 13:57:53 +0100 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Thunderbird/84.0 |
On 19/04/2021 18:08, Carl Edquist wrote:
Greetings Coreutils,
I'm submitting for your consideration here a patch to add the standard
'--files0-from=FILE' option to ls.
(To read NUL-terminated names from FILE rather than using command line
arguments.)
Motivation for adding this to ls is mainly to let ls sort arbitrarily many
items, though it is also necessary for getting the correct aggregate
column widths to align long format (-l) output across all file names.
As a real example, if you want to use ls to list (say, in long format with
human sizes) all the sources in the linux kernel tree according to size,
you might naively try one of
[linux]$ find -name '*.[ch]' -exec ls -lrSh {} +
[linux]$ find -name '*.[ch]' -print0 | xargs -0 ls -lrSh
but you'll see the sizes spiral over and over, finally ending somewhere in
the middle:
...
-rw-r--r-- 1 kx users 81K Apr 15 04:30 ./arch/arm/boot/dts/imx6sll-pinfunc.h
-rw-r--r-- 1 kx users 83K Apr 15 04:30 ./arch/arm/boot/dts/imx6dl-pinfunc.h
-rw-r--r-- 1 kx users 87K Apr 15 04:30 ./arch/arm/mach-imx/iomux-mx35.h
-rw-r--r-- 1 kx users 107K Apr 15 04:30 ./arch/arm/boot/dts/imx7d-pinfunc.h
-rw-r--r-- 1 kx users 143K Apr 15 04:30 ./arch/arm/boot/dts/imx6sx-pinfunc.h
In this case, xargs batches *13* separate invocations of ls; so the
overall sorting is completely lost.
But with the new option:
[linux]$ find -name '*.[ch]' -print0 | ls -lrSh --files0-from=-
The sizes all scroll in order
One can also implement this functionality with the DSU pattern like:
nlargest=10
find . -printf '%s\t%p\0' |
sort -z -k1,1n | tail -z -n"$nlargest" | cut -z -f2 |
xargs -r0 ls -lUd --color=auto --
Arguably that's more scalable as the sort operation will not
be restricted by available RAM, and will use temp storage.
Also it's more robust as ls is the last step in the pipeline,
presenting the files to the user. If you wanted the largest 10
with ls --files0-from then file names containing a newline
would mess up further processing by tail etc.
Similarly, say you would like to view / scroll through your extensive mp3
collection in chronological order (based on when you added the files to
your collection). You can do it now with the new option:
[music]$ find -name \*.mp3 -print0 | ls -lrth --files0-from=-
I've used a https://www.pixelbeat.org/scripts/newest script
for a long time with similar find|xargs technique as I described above.
In saying all of the above, I do agree though that for consistency
commands that need to process all arguments with global context,
should have a --files0-from option.
Currently that's du and wc for total counts, and sort(1) for sorting.
Since ls has sorting functionality, it should have this option too.
thanks!
Pádraig