On Wed, Oct 09, 2019 at 02:15:44AM +1100, Andrew McGlashan wrote:
> On 8/10/19 9:33 am, Craig Sanders via luv-main wrote:
> > Either with 'find ... -exec' or, if you need to process find's list
> > of filenames (with grep or sed or something) make sure you use NUL
> > separated output and tools that can handle NUL-separated input
> > (e.g. 'find ... -print0 | grep -z ... | head -z -n 10 | xargs
> > -0r')
>
> find badly needs an option for sorting the output via timestamps.

Yes, that would be useful.  Not essential, though, as there are other
ways to do it.


> Want the last version of a file:
>   ls -rt|tail -1
>
> How do you do that with find?

Similar to how you'd do it with stat: with a printf format string. i.e. use
find's -printf option to output the file's change time (in seconds since the
epoch), a TAB, the filename, and a NUL separator.

find /path/ -type f -printf '%C@\t%p\0' |
  sort -z -k1,1 -r -n |
  head -z -n 1 |
  cut -z -f2 |

sort is then used to sort find's output by timestamp (reverse numeric
sort), then head to get only the first match, and cut to get rid of the
no-longer-needed timestamp.

or you could pipe it into awk or perl or whatever instead of any or all of the
sort, head, and/or cut commands.


see "man find" and search for printf for other file data that can be printed.

BTW, you could then pipe the output of the above pipeline into xargs to do
something with the filename(s) matched. e.g. to move the matching file to
another directory:

  xargs -0r mv -T /destination/


Unlike ls, this will not break if any filename contains newlines or other
annoying but valid characters.

craig

--
craig sanders <[email protected]>
_______________________________________________
luv-main mailing list
[email protected]
https://lists.luv.asn.au/cgi-bin/mailman/listinfo/luv-main

Reply via email to