Finding with ls
: first things first, ls | grep cisco
is a bit verbose, since cisco
isn't a regular expression. Try:
ls *cisco*
Using find
: along the same lines, -regex
is overkill with a simple, static pattern. How about:
find -name '*cisco*'
The quotes are required so the glob is interpreted by find
, not the shell. Also, -print
is required for many versions of find
, but is optional (and the default predicate) for others (e.g. GNU find
). Feel free to add it if you need it.
If you need to search for ‘cisco’ in the full pathname, you could try this:
find -path '*cisco*'
which is equivalent to find | fgrep cisco
.
Using find
with regular expressions: let's do that anyway, since this is what you want. Shamelessly copying from the GNU find
manpage:
-regex pattern
File name matches regular expression pattern. This is a match
on the whole path, not a search. For example, to match a file named
`./fubar3', you can use the regular expression `.*bar.' or `.*b.*3',
but not `f.*r3'.
What this means is that your regular expression is wrapped in an invisible ^...$
, so it must match every character in the full pathname of the file. So, as nwildner and otokan said in the comments, you should use something like:
find -regex '.*cisco.*'
And you don't even need the -regextype
for something this simple.
It looks like you want to avoid looking for files in *cache*
directories more than finding files with *pillar*
and not *cache*
in their name. Then, just tell find
not to bother descending into *cache*
directories:
find . -iname '*cache*' -prune -o -iname '*pillar*' -print
Or with zsh -o extendedglob
:
ls -ld -- (#i)(^*cache*/)#*pillar*
(not strictly equivalent as that would report a foo/pillar-cache
file)
Or (less efficient as it descends the whole tree like in @apaul's solution):
ls -ld -- (#i)**/*pillar*~*cache*
Details on the zsh
specific globs:
(#i)
: turn on case insensitive matching
^
: negation glob operator
(...)
: grouping (like @(...)
in ksh
).
<something>#
: zero or more of <something>
(like *
in regexps).
~
: and-not operator (matches on the whole path)
**/
: 0 or more directory levels (short for (*/)#
).
Add the (D)
glob qualifier if you want to descend into hidden dires and match hidden files like in the find
solution.
Best Answer
You're confusing regular expressions with shell search patterns.
? in shell means any single character.
? in regexp means the previous character (or sub-pattern) is optional.
Try:
find . -regex '.*ooks?' -type f
From the find man page: