zsh-users
 help / color / mirror / code / Atom feed
From: Lloyd Zusman <ljz@asfast.com>
To: zsh-users@sunsite.dk
Subject: Re: Emulating 'locate'
Date: Sat, 04 Oct 2003 13:05:53 -0400	[thread overview]
Message-ID: <m3znghq6fi.fsf@asfast.com> (raw)
In-Reply-To: <20031004151233.GC50@DervishD> (raul@pleyades.net's message of "Sat, 4 Oct 2003 17:12:33 +0200")

DervishD <raul@pleyades.net> writes:

>     Hi Lloyd :)
>
>  * Lloyd Zusman <ljz@asfast.com> dixit:
>>
>>
>> However, in that case, I target it to a specific directory tree,
>> and rarely, if ever recurse down from the root directory unless I
>> want to take a long coffee break waiting for results, and I don't
>> mind users screaming at me for slowing down the system.
>
>     ;)))))))))) I see you are not a BOFH ;))) Confess it: you like
> your users XDDD

Of course.  That's nothing to be ashamed of. :)


> [ ... ]
>
>>   find / -name specific-file -print   # 15 min 19 sec elapsed
>>   xlocate specific-file               # 28 min 40 sec elapsed
>
> [ ... ]
>
>     BTW, you seem to have a *really* big set of files...

Well, I manage a small system with around 150-160 users total, with
maybe 15-20 percent of them active at the moment.  Only a handful are
shell users; the rest are mostly email users; there are a small number
of web users, as well.  It doesn't take much to generate lots of files,
even in a modestly sized system such as this one.


>     Thanks for the code :))
>
>>   xlocate() {
>>     setopt nullglob extendedglob
>>     eval print -l ${argv[1]%/}'/**/'${^argv[2,-1]}'{,/**/*}'
>>   }
>
>     Nice! :)))

Well, I created a full-blown version with user help, meaningful error
messages, etc., and I put it into /etc/zshrc so it's available to all
the shell users on my system.  I call it 'zfind' ("zsh find"), and the
code is below.


> [ ... ]
>
> [ ... ] Thanks again for your help and suggestions :)
>
> [ ... ]
>
>     Raúl Núñez de Arenas Coronado

My pleasure.

Here's the code to my full-blown zfind function, right out of my
/etc/zshrc:

zfind() {

  local usage moreusage match oiplus1 verbose=1

  usage="\nusage: $prog [ -qhH ] dir pattern ...

  -h  =>  print a short version of help to stderr

  -H  =>  print a longer version of help to stdout (so you
          can easily pipe it through your favorite pager)

  -q  =>  suppress all error messages except for help and
          the message for illegal flags on the command line\n"

  moreusage="
  This command recursively searches under the directory tree 
  specified by 'dir' for any filesystem items whose names match 
  each the 'pattern' items that are specified.

  Its usage approximates that of the 'find' command with the '-name'
  option.

  Example:

    $0 ~ '(#i)*.{gif,png,jp{,e}g}'

    Recursively lists all items under your HOME directory whose
    names have the suffix '.gif', '.png', '.jpg', or '.jpeg'.  
    In this case (due to the '(#i)' prefix), matches are done in 
    a case-insensitve manner.

  This command makes use of zsh's extended pattern matching.
  To get more information about this, do a 'man zshexpn' and 
  look under the FILENAME GENERATION section.\n"

  while getopts qhH arg
  do
    case "${arg}" in
    q)
       verbose=
       ;;
    h)
       print -u2 "${usage}"
       return 1
       ;;
    H)
       print "${usage}${moreusage}"
       return 1
       ;;
    ?)
       print -u2 "for help, invoke \"$0 -h\" or \"$0 -H\""
       return 1
       ;;
    esac
  done

  (( $# <= $OPTIND )) && {
    print -u2 "${usage}"
    return 1
  }

  setopt nullglob extendedglob

  [[ -d ${argv[$OPTIND]} ]] || {
    [[ -n "${verbose}" ]] && {
      print -u2 "$0: directory not found: ${argv[$OPTIND]}"
    }
    return 1
  }

  (( oiplus1 = $OPTIND + 1 ))
  eval match='('${argv[$OPTIND]%/}'/**/'${^argv[$oiplus1,-1]}'{,/**/*})'

  if [[ -z "${match}" ]]
  then
    [[ -n "${verbose}" ]] && {
      print -u2 "$0: not found within \"${argv[$OPTIND]}\" tree: " \
                "${^argv[$oiplus1,-1]}"
    }
    return 1
  else
    print -l ${match}
  fi
}



--
 Lloyd Zusman
 ljz@asfast.com


  reply	other threads:[~2003-10-04 17:06 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20031001221753.GA23189@DervishD>
     [not found] ` <1031002023639.ZM22046@candle.brasslantern.com>
2003-10-02  8:03   ` DervishD
2003-10-02 14:29     ` Bart Schaefer
2003-10-02 15:53       ` DervishD
2003-10-02 17:08         ` Oliver Kiddle
2003-10-02 19:27           ` DervishD
2003-10-03 16:22     ` Lloyd Zusman
2003-10-04 10:48       ` DervishD
2003-10-04 13:48         ` Lloyd Zusman
2003-10-04 15:12           ` DervishD
2003-10-04 17:05             ` Lloyd Zusman [this message]
2003-10-04 21:35               ` DervishD
2003-10-04 16:37           ` Bart Schaefer
2003-10-04 19:33             ` Lloyd Zusman
2003-10-04 21:29               ` DervishD
2003-10-04 22:40               ` Bart Schaefer
2003-10-04 23:18                 ` Lloyd Zusman
2003-10-05 15:57                   ` Bart Schaefer
2003-10-06 13:37                     ` Lloyd Zusman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m3znghq6fi.fsf@asfast.com \
    --to=ljz@asfast.com \
    --cc=zsh-users@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).