coreutils
[Top][All Lists]
Advanced

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

[PATCH] ls: add --files0-from=FILE option


From: Carl Edquist
Subject: [PATCH] ls: add --files0-from=FILE option
Date: Mon, 19 Apr 2021 12:08:43 -0500 (CDT)

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, finally ending in

...
-rw-r--r-- 1 kx users 5.0M Apr 15 04:31 
./drivers/gpu/drm/amd/include/asic_reg/nbio/nbio_7_4_sh_mask.h
-rw-r--r-- 1 kx users 5.5M Apr 15 04:31 
./drivers/gpu/drm/amd/include/asic_reg/dcn/dcn_1_0_sh_mask.h
-rw-r--r-- 1 kx users 6.6M Apr 15 04:31 
./drivers/gpu/drm/amd/include/asic_reg/dce/dce_12_0_sh_mask.h
-rw-r--r-- 1 kx users  13M Apr 15 04:31 
./drivers/gpu/drm/amd/include/asic_reg/nbio/nbio_7_0_sh_mask.h
-rw-r--r-- 1 kx users  14M Apr 15 04:31 
./drivers/gpu/drm/amd/include/asic_reg/nbio/nbio_6_1_sh_mask.h


(The biggest files are where they belong, at the end of the listing.)


...

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=-


(Sidenote: Rob, the record store owner in High Fidelity (2000) [who was 
also known for his habit of making "Top 5" ordered lists], called this 
sorting of his records "autobiographical" [1].)

...


Additionally, note that ls can already list and sort an individual 
directory with arbitrarily many entries, but you run into trouble if you 
want to limit the output to a subset of those entries (eg, a particular 
glob pattern).

For instance if you have a system with many status files in a directory 
representing tasks, and want to list in chronological order the 
'completed' tasks, with something like:

    [tasks]$ ls -lrt *.completed

This will eventually fail, once the argument list limit is hit.

Again, a robust solution is possible with the new option:

    [tasks]$ find -mindepth 1 -maxdepth 1 -name \*.completed -printf '%f\0' |
             ls -lrt --files0-from=-


(The more complicated find expression is used here just to demonstrate how 
to match the behavior of the single-directory ls invocation with a glob 
pattern.)


That's about it.  The feature should already be well understood from other 
programs, but hopefully the examples demonstrate its utility within ls.


Any feedback / requests for improvement are of course welcome.


Carl


-=-=-+-=-=-


[1] https://youtu.be/AQvOnDlql5g

Attachment: 0001-ls-add-files0-from-FILE-option.patch
Description: Text Data


reply via email to

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