[X-Unix] Wildcard * in Pathnames

David Ledger dledger at ivdcs.demon.co.uk
Thu May 12 22:16:08 PDT 2005


>From: Jerry Krinock <jerry at ieee.org>
>
>In the arguments of various commands, for example "find" and "rm", I would
>often like to match any path containing a given word with a wildcard
>expression like:
>
>     *Fruit*
>
>And I expect this to match _any_ pathname containing "Fruit", including:
>
>     ~/Fruit/Apple/Seeds
>     /Applications/Fruit/Lemon.app
>     /Volumes/MyHD/Fruit
>
>but it never works.  The problem seems to be that the path separator
>character "/" is beyond the scope of the wildcard "*".  I can get the
>desired result if I search a set of wildcard expressions, such as:
>
>     /*/Fruit/*
>     /*/*Fruit/*
>     /Fruit/*/*/*/
>     etc.
>     etc.
>     etc.
>
>Does anyone know a more elegant solution than this kludge?
>
>Thank you,
>
>Jerry Krinock

Your expectations need adjusting.

First thing to realise is that 'find' or 'rm' never sees the '*'. 
The moment you hit return, the shell analyses the command line you've 
just given it.  Where it sees a word with wildcards, unless quoting 
indicates otherwise, it replaces it with words that match the pattern 
as pathnames from where you are.  If there is no match, the 
wildcarded word is unchanged.

This means that *Fruit* will be replaced by a list of all names in 
the current directory that contain 'Fruit' (except any that start 
with a '.'). If nothing does match it is left as *Fruit*.

/*/Fruit/* will be replaced with all the names in any directory 
called Fruit that lives in any directory directly under /.

/*/*Fruit/* will be replaced with all the names in any directory 
whose name ends in Fruit (but does not start with a '.') that lives 
in any directory directly under /. If nothing does match it is left 
as is.

etc.

All this, and a lot more checking for possible replacements, takes 
place before the shell executes the (now) first word on the line as a 
command and passes it the rest of the line as arguments.

If you want the command to reliably see a '*', '?' or '[...]' 
sequence they must be quoted or escaped as in
find / -name '.*Fruit.*'
Without the quotes, entered with the current directory being one 
containing files
theFruit theWholeFruit nothingButTheFruit
the above would actually execute the command
find / -name theFruit theWholeFruit nothingButTheFruit
which is unlikely to give the expected results.


This is what gives working through a shell such flexibility, but you 
do need to understand how it works to do all but the simplest things 
reliably.

David


-- 
David Ledger - Freelance Unix Sysadmin in the UK.
Chair of HPUX SysAdmin SIG of hpUG technical user group (www.hpug.org.uk)
david.ledger at ivdcs.co.uk
www.ivdcs.co.uk


More information about the X-Unix mailing list