[ale] find + ls commands: commentary [was: Bash script]
Bob Toxen
transam at verysecurelinux.com
Mon Apr 14 21:59:18 EDT 2025
WOW!
find . -exec something \+
^^
Never heard of it! Not in "man find" on any system of mine, except my
newest, a high powered gaming laptop purchased in the Brooklyn Microcenter
in late 2023! (As of 08/23/2024 Florida's first Microcenter is open,
in Miami!)
I never read the man page for find on it until just now 'cause it hasn't
changed in decades.
I'm impressed!
Bob
On Sun, Apr 13, 2025 at 11:01:22PM -0700, Ron via Ale wrote:
>
> I'm self-replying because, instead of getting all defensive, I'm gonna
> share an explanation on what is going on with `find` so anyone still
> reading this thread can benefit.
>
>
> I chose `find` because it traverses directories by default, and this can
> be controlled with the -maxdepth and -mindepth options.
>
>
> One can have find execute a command on the found items via the -exec option.
>
>
> It gets a bit tricky if one wants to pipe that output to somewhere else,
> as `find` wants to send everything after the -exec to the specified
> program... until it encounters a ";", which needs to be escaped as "\;".
>
> However, that will invoke the -exec program once for each found item.
>
> If one were to use "\+" instead, `find` will bundle up all the found
> results and pass them along at once.
>
>
> This is perfect for our `ls` command: it can take everything found
> anywhere on the SSD and sort it internally, which it's built for and
> there's no gotcha on file sizes, etc.
>
>
> There is a wee gotcha with `ls` though.
>
> If including directories in the `find` search, and then we run `ls` on
> them, their *contents* will be listed, along with "total: ###", blank
> lines, etc.
>
> So, -d or --directory will "list directories themselves, not their
> contents".
>
>
> Finally, we pipe through `tail` (not `head` since we'll get that "signal
> 13" error) and trim to the number of lines needed.
>
> Should be one line, but it depends on whether `ls -a` or `ls -A` (--all
> or --almost-all) are used, where the `find` is run in relation to the
> searched directories, and other things I can't think of.
>
>
> Hopefully someone benefits from this and I haven't made too many errors
> in writing it up.
>
>
>
> Here's some tests one can play with to see what's going on:
>
> find /tmp -exec ls -dltrA {} \+ | less
>
>
> Notice that /tmp itself is the newest entry which may or may not be desired.
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> https://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo
More information about the Ale
mailing list