66

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

2 Answers 2

89

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*'
1
  • 1
    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, 2021 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.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.