/usr/bin/find - binary operands howto

Karl Vogel vogelke+unix at pobox.com
Tue Jun 5 02:32:45 UTC 2012


>> On Sun, 3 Jun 2012 19:10:00 -0400, 
>> grarpamp <grarpamp at gmail.com> said:

G> Given a fs with millions of inodes, multiple find runs is expensive.  As
G> is performing the ch* on more than the minimum required inodes, which
G> also needlessly updates the inode ctime. So I want one find, doing the
G> ch* only if necessary.  So how should I write this? Do I want to use
G> -true/-false somehow?

   It might be more efficient to keep find output in either a flat file or DB,
   so you can avoid multiple walks over the filetree.  You'll need GNU find:

       #!/bin/sh
       export PATH=/usr/local/bin:/bin:/usr/bin
       test "$1" || set .

       echo '#filetype|inode|links|uname|gname|mode|size|mtime|pathname'
       gfind $@ -printf '%y|%i|%n|%u|%g|%m|%s|%T@|%p\n'
       exit 0

   Sample output:

       root# chown 1234 stuff
       root# chgrp 5678 stuff

       me% ls -l
       drwxr-sr-x 3 kev   local    512 04-Jun-2012 21:01:41 .
       drwxr-xr-x 2 kev   local    512 04-Jun-2012 21:38:47 mail
       -rw-r--r-x 1 kev   local  47072 04-Jun-2012 19:34:26 mail/junk*
       -rw-r--r-- 1 1234   5678     85 19-May-2012 23:28:30 stuff
       -rw-r--r-- 1 kev   local   8104 04-Jun-2012 19:43:44 testing

       me% [run script]
       #filetype|inode|links|uname|gname|mode|size|mtime|pathname
       d|873603|3|kev|local|2755|512|1338858101|.
       d|1188634|2|kev|local|2755|512|1338860327|./mail
       f|1188649|1|kev|local|645|47072|1338852866|./mail/junk
       f|955452|1|1234|5678|644|85|1337484510|./stuff
       f|873708|1|kev|local|644|8104|1338853424|./testing

   Run this first, then look for the conditions you want using awk or perl.
   Advantages:

   * Doesn't change ctime, no additional filetree-walking.

   * You can use this to create your locate DB, if you want to avoid a
     second pass through the filesystem.

   * Gives you a point-in-time picture of ownership, mode, etc. in case
     you need to back out your changes.

-- 
Karl Vogel                      I don't speak for the USAF or my company

When I read about the evils of drinking, I gave up reading.  --Henny Youngman


More information about the freebsd-questions mailing list