Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question

3 Answers 3

up vote 21 down vote accepted

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.

share|improve this answer
2  
If <filename> contains wildcards, use quotes around it, e.g. find . -name '*.txt'. –  Gilles Feb 9 '11 at 23:03
    
@Gilles, updated my answer to say that, thanks. –  Mikel Feb 10 '11 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. –  user unknown Feb 10 '11 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 '11 at 10:48
1  
find -iname <filename> is better since it is case-insensitive like DOS –  Agnel Kurian Feb 16 at 17:18

You can do this with

find . | egrep filename
share|improve this answer
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 9 '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 9 '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 8:14

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

share|improve this answer
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 4:41
    
That one is really nice. Thanks! –  rozcietrzewiacz Aug 4 '11 at 8:02
    
This 'ls **/filename` is fine, but seems not go more that one directory level deep. –  Sopalajo de Arrierez Apr 12 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 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. –  Sopalajo de Arrierez Apr 13 at 12:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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