18

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

4 Answers 4

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.

6
  • 3
    If <filename> contains wildcards, use quotes around it, e.g. find . -name '*.txt'. Feb 9, 2011 at 23:03
  • @Gilles, updated my answer to say that, thanks.
    – Mikel
    Feb 10, 2011 at 4:32
  • 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. Feb 10, 2011 at 8: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, 2011 at 10:48
  • 1
    find -iname <filename> is better since it is case-insensitive like DOS Feb 16, 2014 at 17:18
8

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

4
  • 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, 2011 at 4:41
  • This 'ls **/filename` is fine, but seems not go more that one directory level deep. Apr 12, 2014 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. Apr 13, 2014 at 3:39
  • Ops... I understand now, @ShawnJ.Goff: the shopt command enables the option globstar on. Now it works like a charm. Thanks a lot. Apr 13, 2014 at 12:05
4

You can do this with

find . | egrep filename
4
  • 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) Feb 9, 2011 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 9, 2011 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, 2011 at 8:14
  • What does *.*.* do in a modern dos i.e. windows cmd? What about *.*.*.*? Jun 30, 2016 at 8:30
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .