[kwlug-disc] Reminder: bash's find has option -exec
Ron
ron at bclug.ca
Fri Nov 7 22:43:55 EST 2025
Mikalai Birukou via kwlug-disc wrote on 2025-11-07 13:46:
> As a reminder, bash's find has option -exec, that let's you do
> something during find's iteration.
That's one of my favourite options with `find`.
It should be noted that `find ... -exec ...` requires a semi-colon at
the end. But, that semi-colon needs to be escaped like this: \;
Unless... one doesn't want the default behaviour of whatever is being
exec'd to run *once per found item*.
If one would prefer everything found to be passed at once, finish the
statement with \+
Unless... there's a whole bunch of stuff being found, then it gets
buffered and passed to the -exec'd program when the buffer is full, not
just when the `find` is done.
How big is the buffer? Who knows, good luck.
Someone wanted to find the newest or oldest file on a failing disk.
I suggested something like:
find $mount_point -type f -exec ls -lta {} \+ | head -n 1
It worked perfectly on some test folders.
One an entire disk though? Too many entries returned, got sorted
chunks, completely failed.
I think the buffer consisted of about 300 entries, out of thousands.
Anyway, there's a thread about "find newest and oldest items on a disk"
with some of the craziest mailing list replies I've ever seen (makes me
rethink my skepticism about full moons) starting here:
https://mail.ale.org/pipermail/ale/2025-April/166848.html
Staggeringly incorrect answers abound, to the point where it's fun to
see "what will they come up with next?"
My final answer:
find $mount_point -printf "%TY-%Tm-%Td %TH:%TM:%TS %p\0" \
| sort --zero-terminated \
| tr '\0' '\n' \
| sed -n -e 1p -e '$p'
Probably contains subtle errors still.
More information about the kwlug-disc
mailing list