Operating System - HP-UX
1833887 Members
2067 Online
110063 Solutions
New Discussion

find command and wildcard in search path

 
SOLVED
Go to solution
dev44
Regular Advisor

find command and wildcard in search path

Hi,

I have a bunch of directories:

/dira/au01
/dirb/au02
/dirc/au03
/var
/usr
etc

I want to use the find command to only search in *u0* directories. I can't seem to find the correct syntax.

find *u0* -type f -name xxx -print

and it doesn't like the *u0* , are wild cards even possible in the search path?
whatever
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: find command and wildcard in search path

Hi:

Try specifying an absolute path:

# find *u0* -type f -name xxx -print

It is useful to expose what the shell has done to your command by doing prefacing the command with 'echo', like:

# echo find *u0* -type f -name xxx -print

Regards!

...JRF...
dev44
Regular Advisor

Re: find command and wildcard in search path

#find *u0* -type f -name "*.dbf" -print
find: cannot stat *u0*
whatever
James R. Ferguson
Acclaimed Contributor

Re: find command and wildcard in search path

Hi (again):

> find: cannot stat *u0*

...means that 'find' can't find '*u0*' (literally).

This may reflect that you have turned off wildcard expansion. Note the difference in the following:

# set -f
# cd /
# echo v*
v*
# set +f
# echo v*
var

Regards!

...JRF...
TTr
Honored Contributor

Re: find command and wildcard in search path

The *u0* will match a filename pattern only in the current directory and will not take care of the /dira/ part of the path. You need something like /dir?/au0?
Also you can list out multiple paths in the find command as in
find /dira/au01 /dirb/au02 /dirc/au03 /var /usr -type f -name xxx -print

kobylka
Valued Contributor
Solution

Re: find command and wildcard in search path

Hi dev44!

I would use a nested find like this:


find / -name '*u0*' -type d -exec find {} -name '*xxx*' \; -print


The first find loops only through directories matching the pattern *u0* and for each loops again to look for files matching *xxx* (or whatever).

Regards,

Kobylka
Bill Hassell
Honored Contributor

Re: find command and wildcard in search path

*u0* doesn't make any sense because it will return different results depending on the directory that you are in. Instead, don't use the find command until you use echo to find your directories, like this:

echo /dir*/*u0*

This should produce all the directories you listed in the above example. It will /dira as well as /dirabcdef, and will also find /dira/abcdu0xyz. If you have a more restricted naming convention, by all means use the appropriate masking characters. For instance, if all the directories are /dira through /dirw, then use a character class to pick an exact match:

echo /dir[a-w]

Now, /dirabcd will not match, only /dira.

Similarly with the u0 directories. If the directories always start with au0, then don't use *. The * is far too inclusive. If the u0 directories are au0 through zu0, then again, use a character class. And if there is only a single number following, do this:

echo /dir[a-w]/[a-z]u0[0-9]

Note that find does not translate a simple file matching string *u0*. Instead, it will see how the shell will translate *u0*. You are supplying all the names from shell expansion -- that's why echo is your friend.


Bill Hassell, sysadmin