70

How can I search a wild card name in all subfolders? What would be the equivalent of DOS command: dir *pattern* /s in *nix?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Eduard Florinescu
  • 11,153
  • 18
  • 57
  • 67

2 Answers2

92

You can use find. If, for example, you wanted to find all files and directories that had abcd in the filename, you could run:

find . -name '*abcd*'
Ryan A.
  • 1,201
  • 9
  • 3
  • 3
    Just adding a comment in case someone else will encounter the same confusion as I did: `-name` will look for a pattern that matches the filename, i.e. not the full path. So if you are looking for a file containing somewhere in its path the string `string1` followed by `string2` somewhere in the path you should use `find . -wholename "*string1*string2*"`. – Kvothe Jul 28 '21 at 10:00
20

Zsh:

ls -ld -- **/*abcd*

Ksh93:

set -o globstar     # put this line in your ~/.kshrc
ls -ld -- **/*abcd*

Bash ≥4:

shopt -s globstar   # put this line in your ~/.bashrc
ls -ld -- **/*abcd*

Yash:

set -o extendedglob # put this line in your ~/.yashrc
ls -ld -- **/*abcd*

tcsh:

set globstar
ls -ld -- **/*abcd*

fish:

ls -ld -- **abcd*

(beware some of those shells will follow symlinks when descending the directory tree; some of those that don't like zsh, yash or tcsh have ***/*abcd* to do it).

Portable (except to very old systems; OpenBSD took a long time but finally supports exec … + since 5.1):

find . -name '*abcd*' -exec ls -ld {} +

Not POSIX but works on *BSD, Linux, Cygwin, BusyBox:

find . -name '*abcd*' -print0 | xargs -0 ls -ld

Note that except in some BSDs, if no matching file is found, ls -ld will be run without arguments, so will list .. With some xargs implementations, you can use the -r option to work around that.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175