List all files/dirs in or below the current directory that match 'filename'.
Asked
Active
Viewed 3.8k times
4 Answers
30
The direct equivalent is
find . -iname <filename>
which will list all files and directories called <filename> in the current directory and any subdirectories, ignoring case.
If your version of find doesn't support -iname, you can use -name instead. Note that unlike -iname, -name is case sensitive.
If you only want to list files called <filename>, and not directories, add -type f
find . -iname <filename> -type f
If you want to use wildcards, you need to put quotes around it, e.g.
find . -iname "*.txt" -type f
otherwise the shell will expand it.
As others have pointed out, you can also do:
find . | grep "\.txt$"
grep will print lines based on regular expressions, which are more powerful than wildcards, but have a different syntax.
See man find and man grep for more details.
Mikel
- 57,299
- 15
- 134
- 153
8
Some shells allow ls **/filename, which is quite convenient.
Shawn J. Goff
- 46,081
-
1Good point. In recent versions of bash, running
shopt -s globstar; echo **/filenameis equivalent tofind . -name "filename". It also works in zsh. – Mikel Feb 10 '11 at 04:41 -
This 'ls **/filename` is fine, but seems not go more that one directory level deep. – Sopalajo de Arrierez Apr 12 '14 at 23:14
-
@sopalajo-de-arrierez If you do
shopt -s globstar, it will probably work for you. Recursive globbing is a feature that is available only in some shells, and sometimes, it is not on by default. – Shawn J. Goff Apr 13 '14 at 03:39 -
Ops... I understand now, @ShawnJ.Goff: the
shoptcommand enables the optionglobstar on. Now it works like a charm. Thanks a lot. – Sopalajo de Arrierez Apr 13 '14 at 12:05
4
You can do this with
find . | egrep filename
Matten
- 141
-
1You could also do it in one with
find . -regextype posix-egrep -regex '.*filename.*'(I don't know if theegreppart is important, but you usedegrepin your answer so I included it) – Michael Mrozek Feb 09 '11 at 20:29 -
You can, but grep is different than the equivalent DOS command.
grepuses regular expressions, while the DOS command uses shell wildcards. – Mikel Feb 09 '11 at 20:36 -
1Come to think of it, shell globs are also different than DOS wildcards. For instance,
find . -name "*.*"won't do what you'd expect from a DOS background. Globs are close enough to be recognizable, though, while regexes are an entirely new beast. – Jander Feb 10 '11 at 08:14 -
What does
*.*.*do in a modern dos i.e. windows cmd? What about*.*.*.*? – ctrl-alt-delor Jun 30 '16 at 08:30
<filename>contains wildcards, use quotes around it, e.g.find . -name '*.txt'. – Gilles 'SO- stop being evil' Feb 09 '11 at 23:03find .will do.<filename>as marker for userinput is a bad habit in command-line environment, where< and >most of the time have specific meaning. I suggest just usingfilename, maybe FILENAME to emphasize it. Most people will understand, and those, who won't, might cause harm when not understanding that they aren't supposed to hit less-than or greater-than sign. – user unknown Feb 10 '11 at 08:25<filename>is a convention in a lot of UNIX documentation, so I think it's useful for people to be aware of it, but I agreeFILENAMEmight be easier to understand. – Mikel Feb 10 '11 at 10:48find -iname <filename>is better since it is case-insensitive like DOS – Agnel Kurian Feb 16 '14 at 17:18