19

List all files/dirs in or below the current directory that match 'filename'.

mackenir
  • 293

4 Answers4

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
  • 3
    If <filename> contains wildcards, use quotes around it, e.g. find . -name '*.txt'. – Gilles 'SO- stop being evil' Feb 09 '11 at 23:03
  • @Gilles, updated my answer to say that, thanks. – Mikel Feb 10 '11 at 04:32
  • And if you don't want a , just find . will do. – Jander Feb 10 '11 at 08:15
  • 2
    Using <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 using filename, 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
  • 2
    <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 agree FILENAME might be easier to understand. – Mikel Feb 10 '11 at 10:48
  • 1
    find -iname <filename> is better since it is case-insensitive like DOS – Agnel Kurian Feb 16 '14 at 17:18
8

Some shells allow ls **/filename, which is quite convenient.

Shawn J. Goff
  • 46,081
  • 1
    Good point. In recent versions of bash, running shopt -s globstar; echo **/filename is equivalent to find . -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 shopt command enables the option globstar 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
  • 1
    You could also do it in one with find . -regextype posix-egrep -regex '.*filename.*' (I don't know if the egrep part is important, but you used egrep in 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. grep uses regular expressions, while the DOS command uses shell wildcards. – Mikel Feb 09 '11 at 20:36
  • 1
    Come 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
0

It can be also: tree -if </your/path> or 'pwd' as a path