[kwlug-disc] Reminder: bash's find has option -exec
CrankyOldBugger
crankyoldbugger at gmail.com
Sat Nov 8 11:03:15 EST 2025
Newer versions of find have some more built in commands, in particular
-delete.
Daily I run a script of mine that does a number of daily maintenance
things. One part in particular looks for any invalid .srt files (
opensubtitles.org often sticks one line advertisements in a .srt instead of
the actual subtitles). So I run the following in my videos folders:
find /mnt/athenavids/ -type f -size -150c -iname "*.srt" -delete -print
What this does is look in my video folder for any files with the extension
.srt less than 150 bytes (those advertisements are 102 bytes, usually),
then it deletes them and prints the filename so I can watch to see where
it's finding them.
That -delete part in particular used to be -exec rm {} \; -- nice that
they made it easier to use in the newer versions. Running "man find" will
list other neat tricks.
On Sat, 8 Nov 2025 at 03:47, Ron <ron at bclug.ca> wrote:
> 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.
>
>
> _______________________________________________
> kwlug-disc mailing list
> To unsubscribe, send an email to kwlug-disc-leave at kwlug.org
> with the subject "unsubscribe", or email
> kwlug-disc-owner at kwlug.org to contact a human being.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kwlug.org/pipermail/kwlug-disc_kwlug.org/attachments/20251108/2ae87650/attachment.htm>
More information about the kwlug-disc
mailing list