[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: ls feature request
From: |
Bernhard Voelker |
Subject: |
Re: ls feature request |
Date: |
Mon, 24 Feb 2020 23:02:24 +0100 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 |
On 2020-02-22 04:51, Kaz Kylheku (Coreutils) wrote:
> #!/bin/bash
>
> (find .* * -maxdepth 0 \
Using * globbing is dangerous with files with names like '-delete'.
I'd recommend to use
find . -mindepth 1 -maxdepth 1 ...
and changing the later substr to strip off the leading './'.
> \( -not -type d -printf "-%p\0" \) -o \
> \( -type d -printf "d%p\0" \) ) | \
Alternatively to having 2 OR-ed conditions, one could use %y (print file type).
> gawk 'BEGIN { RS = "\0" }
> /^-/ { nondir[NR] = substr($0, 2) }
> /^d/ { dir[NR] = substr($0, 2) }
> END { asort(nondir)
> asort(dir)
> for (l in nondir)
> printf("%s\0", nondir[l]);
> for (l in dir)
> printf("%s\0", dir[l]); }' | \
> xargs -0 ls -dU --color=auto
_______________________________^^
This fails (or gives surprising results) if the first file starts with
a minus; better tell 'ls' that the options end here with '--'.
Finally, if the directory is empty, xargs would still invoke 'ls' once
without an argument; thus use the -r, --no-run-if-empty option.
This makes:
$ find . -mindepth 1 -maxdepth 1 -printf '%y%p\0' \
| gawk 'BEGIN { RS = "\0" };
/^d/ { dir[NR] = substr($0, 4); next; };
{ nondir[NR] = substr($0, 4) };
END { asort(nondir)
asort(dir)
for (l in nondir)
printf("%s\0", nondir[l]);
for (l in dir)
printf("%s\0", dir[l]); }' \
| xargs -r0 ls -dU --color=auto --
Have a nice day,
Berny