zsh-workers
 help / color / mirror / code / Atom feed
* Emulating 'locate'
@ 2003-10-01 22:17 DervishD
  2003-10-02  2:36 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: DervishD @ 2003-10-01 22:17 UTC (permalink / raw)
  To: Zsh

    Hi all :))

    Assuming that 'print /**/*' will print all filenames from the
root directory, recursive globbing could be used, for example, as a
poor-man substitute of 'locate'. But there is a problem. Let's assume
this hierarchy:

    /dir1/dir2/dir3/file1
    /dir1/dir2/dir3/file2
    ...
    /dir1/dir2/dir3/filen

    If we issue the command 'locate ir3', then locate will find all
the files above, and will show all paths. But if we issue what I
thought that was equivalente: 'print -l /**/*ir3*', then the output
is just '/dir1/dir2/dir3'.

    I'm obviously wrong, but don't know where: the above matches any
number of directories (well /(*/)#...), followed by any character,
ir3 and again any character. Well, I suppose that slashes must be
matched explicitly (that is what ** is for...), so I do 'print -l
/**/*ir3*/**'. Voilá, now it matches ok :))) and the result is the
same as locate. Problem solved? Not at all.

    Let do 'locate ir2'. The list of output files is the same as
'locate ir3' (well, it outputs a couple of dirs more). But if I do
'print -l /**/*ir2*/**' I only get '/dir1/dir2/dir3' :((( In fact,
adding more ** doesn't work: 'print /**/*ir2*/**/**' just outputs
'/dir1/dir2/ /dir1/dir2/dir3/ /dir1/dir2/dir3'

    Resuming: I'm again clueless and in addition to this I don't
understand correctly the filename generation and recursive globbing.
Anyone can explain a bit what I'm doing wrong so I can use zsh
instead of 'locate'?.

    For those interested: yes, locate may be faster than zsh
(although using a database, like locate does, may change things), but
it reveals files not accesible, nor even listable, by the current
user if the database was build with root privileges. Zsh won't, being
a bit more secure. There are a slocate out there, too, but... well,
it's zsh ;)))

    Thanks a lot again :)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Emulating 'locate'
  2003-10-01 22:17 Emulating 'locate' DervishD
@ 2003-10-02  2:36 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2003-10-02  2:36 UTC (permalink / raw)
  To: Zsh

[We should just go off and have our own little mailing list.]

On Oct 2, 12:17am, DervishD wrote:
}
} Well, I suppose that slashes must be
} matched explicitly (that is what ** is for...)

No, that is what **/ is for.  Each **/ (but not /**) triplet is replaced
by zero or more levels of hierarchy.  It's the trailing slash that makes
** magical; without it ** is just like *.

Actually to be entirely accurate, not _every_ **/ triplit is so replaced;
the **/ has to be the only thing in one level of hierarchy, i.e., it can't
be foo**/ either.

}     Let do 'locate ir2'. The list of output files is the same as
} 'locate ir3' (well, it outputs a couple of dirs more). But if I do
} 'print -l /**/*ir2*/**' I only get '/dir1/dir2/dir3' :((( In fact,
} adding more ** doesn't work: 'print /**/*ir2*/**/**' just outputs
} '/dir1/dir2/ /dir1/dir2/dir3/ /dir1/dir2/dir3'

That last should have done almost what you wanted, except the final *
is redundant.  I suspect you really did

    print /**/*ir2*/*/**
                   ^^^ Note only one star here
when you meant

    print /**/*ir2*/**/*

But that's still not sufficient, because it requires that *ir2* be only
an intervening directory and not the last file or directory in the path.
For that you have to use brace expansion, because you can't mix **/ and
any other form of alternation:

    print -l /**/*ir2*{,/**/*}

So "locate" would be e.g.

    locate() { print -l /**/*${^*}*{,/**/*} }

(You really ought to be sending these questions to -users, not -workers.)

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2003-10-02  2:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-10-01 22:17 Emulating 'locate' DervishD
2003-10-02  2:36 ` Bart Schaefer

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).