[kwlug-disc] Reminder: bash's find has option -exec

Ron ron at bclug.ca
Wed Nov 12 05:01:19 EST 2025


D. Hugh Redelmeier wrote on 2025-11-08 07:09:

> Batching is awkward to get right so xargs is exactly the right place
> to take care of it.

I'll try it out - I've never used xargs myself.


>> 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'
>
> It is possible to have filenames containing newline.  Yuck.  This
> would muck up you script.
> 
> There might be a simple fix.  The GNU sed seems to have the option -z
> or --null-data.  I infer that it causes SED to treat (only?) NUL as a
> line separator.  The manual is unclear.  The name --null-data is
> pretty poor.
Good catch.

New version handles files with new-lines by re-arranging lines and 
adding --null-data:

## NOTE: %T is mtime (mod), %B: birth, %C: status changed, %A: access
## NOTE: -print0 does not accept a format clause, use printf "...\0":
##
find "${search_path}" -printf "%TY-%Tm-%Td %TH:%TM:%TS %p\0"  \
   | sort --zero-terminated                      \
   | sed --null-data --quiet -e 1p -e '$p'       \
   | tr '\0' '\n'


NOTE: also requires -type f because directories' "changed" time changes 
when running this script - quite annoying!  Requires a follow-up post: 
`find` does *not* recognize birth (creation) times.




More information about the kwlug-disc mailing list