2226

How can I recursively find all files in current and subfolders based on wildcard matching?

hyde
  • 50,653
  • 19
  • 110
  • 158
john
  • 26,310
  • 11
  • 41
  • 57
  • Does this answer your question? [Find all files with a filename beginning with a specified string?](https://stackoverflow.com/questions/4034896/find-all-files-with-a-filename-beginning-with-a-specified-string) – Josh Correia Oct 20 '20 at 20:49

17 Answers17

3236

Use find for that:

find . -name "foo*"

find needs a starting point, and the . (dot) points to the current directory.

tux21b
  • 75,045
  • 15
  • 108
  • 99
  • 25
    I know this is tagged as linux but this is worth mentioning: the path is required for on other *nix variants that aren't linux. On linux, the path is optional if you want to use dot. – IslandCow Nov 16 '13 at 00:14
  • Could somebody please explain the "foo*" part thank you. – Seatter Mar 30 '14 at 15:53
  • 4
    @Seatter "foo*" tells find to look for all files that start with "foo". It is just his example. You could use "gpio*" to find all files who's names start with gpio, or just "gpio1" to find all files named gpio1. – schumacher574 Apr 02 '14 at 18:00
  • * is a wildcard - more wildcard possibilities here: http://www.tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm – Michael Apr 30 '14 at 10:29
  • 38
    note that the "foo*" is in quotes so the shell doesn't expand it before passing it to find. if you just did `find . foo*`, the `foo*` would be expanded AND THEN passed to find. – grinch May 19 '14 at 14:29
  • 3
    use -iname for case insensitive – Bharat Mar 20 '15 at 10:05
  • I have a file test.xml in the subdirectory of my current working directory and I have used the command find . -name "test*". It doesnt work. – Atom Nov 12 '15 at 00:46
  • 1
    For some reason it doesn't work when put in a sh script. Does it need bash or something? – Howie Feb 18 '16 at 13:54
  • 49
    Worth stressing that `" "` is very necessary for recursive searching. – WesternGun Oct 21 '16 at 11:43
  • 12
    Also useful: If you don't want to be notified about directories you don't have permission to (or other errors), you can do `find . -name "foo*" 2>/dev/null` – Jobbo Aug 15 '17 at 10:54
  • If you wish to ignore case (as in most cases when I use this) then you could do: find . -iname "foo*" – Orn Arnarson Mar 19 '18 at 18:10
  • In reponse to @IslandCow's comment: that's not true (at least not everywhere). I have to use '.' explicitly on my Fedora boxes. – Clearer Mar 29 '18 at 13:31
  • 2
    @WesternGun Can you explain why the `" "` is needed? What is "expansion" and why is that bad? – Kolob Canyon May 10 '18 at 17:49
  • 2
    @KolobCanyon you can check the man page of find, or [this link](https://linux.die.net/man/1/find) and search the word "non-bug" and then "quote", there you have more info. Also, check [this](http://linuxcommand.org/lc3_lts0080.php). Generally, `*` expansion is "iterate current dir and substitute the * with every file name". So, to avoid this, we use quotes. – WesternGun May 11 '18 at 06:44
  • @WesternGun Is that different than string interpolation? Because I thought quotes meant string interpolation in the shell – Kolob Canyon May 11 '18 at 15:11
  • 1
    @KolobCanyon something like that, but the links have more and I think expansion is broader. – WesternGun May 11 '18 at 15:31
  • Is it possible to group the results by directories? Instead of showing every file, I would like to see the locations of such files. – NurShomik Jun 22 '18 at 21:56
  • This is ridiculous... what if I only want to search in the current dir?? – Yan King Yin Feb 17 '19 at 04:08
  • @yan king yin - use `ls`? – KolonUK Apr 16 '19 at 10:12
  • weirdly enough this doesn't work me. What worked for me is the answer from Paul Whipp – metafa May 27 '19 at 14:26
  • If I leave out the quotes, it searches only in the current directory. The genius designer of this should explain the behavior. – Yan King Yin Aug 25 '19 at 12:25
  • dot does not work for me, I have to use `find /some/path/ -iname "foo*"` – andrej Aug 17 '20 at 13:07
249

Piping find into grep is often more convenient; it gives you the full power of regular expressions for arbitrary wildcard matching.

For example, to find all files with case insensitive string "foo" in the filename:

~$ find . -print | grep -i foo
Andy Lester
  • 81,480
  • 12
  • 93
  • 144
Paul Whipp
  • 13,812
  • 4
  • 37
  • 47
  • 76
    `find` also has the `-iname`, `-regex`, and `-iregex` flags for case-insensitive wildcard, regex, and case-insensitive regex matching, so piping to `grep` is unnecessary. – iobender Aug 04 '15 at 16:54
  • 9
    I don't think it is about being unnecessary, but being more convenient. – Elijah Lynn Feb 08 '17 at 16:57
  • 3
    However, piping to grep -v can allow you to use simple strings or regexes to remove entries you don't want. – door_number_three Apr 06 '17 at 03:15
  • 5
    @iobender - Sadly, I can tell you from experience that not all systems come with a `find` command that supports those options. Sometimes `grep` becomes the only option. – Mr. Llama Jul 05 '18 at 17:36
167

find will find all files that match a pattern:

find . -name "*foo"

However, if you want a picture:

tree -P "*foo"

Hope this helps!

the Tin Man
  • 150,910
  • 39
  • 198
  • 279
IslandCow
  • 2,828
  • 3
  • 16
  • 24
53

fd

In case, find is too slow, try fd utility - a simple and fast alternative to find written in Rust.

Syntax:

fd PATTERN

Demo:

Homepage: https://github.com/sharkdp/fd

kenorb
  • 118,428
  • 63
  • 588
  • 624
52
find -L . -name "foo*"

In a few cases, I have needed the -L parameter to handle symbolic directory links. By default symbolic links are ignored. In those cases it was quite confusing as I would change directory to a sub-directory and see the file matching the pattern but find would not return the filename. Using -L solves that issue. The symbolic link options for find are -P -L -H

toddcscar
  • 816
  • 8
  • 11
  • 2
    L switch is very helpful. Many times user do not have any idea about underlying directories, whether they are softlinked or are normal directories. So in case of doubt, it always good to use L option. At least, it has always helped me. – Ritesh Jan 03 '18 at 16:21
  • 2
    That was the answer for me. Thanks! – iedmrc Apr 07 '20 at 23:52
43

If your shell supports a new globbing option (can be enabled by: shopt -s globstar), you can use:

echo **/*foo*

to find any files or folders recursively. This is supported by Bash 4, zsh and similar shells.


Personally I've got this shell function defined:

f() { find . -name "*$1*"; }

Note: Above line can be pasted directly to shell or added into your user's ~/.bashrc file.

Then I can look for any files by typing:

f some_name

Alternatively you can use a fd utility with a simple syntax, e.g. fd pattern.

kenorb
  • 118,428
  • 63
  • 588
  • 624
32
find <directory_path>  -type f -name "<wildcard-match>"

In the wildcard-match you can provide the string you wish to match e.g. *.c (for all c files)

XYZ_Linux
  • 2,527
  • 5
  • 23
  • 32
  • 2
    Your answer is the first most correct here as it only searches files as specified. The others not specifying type will return directories. – wilsotc Nov 23 '17 at 01:15
  • if you wish to search for a directory "-type f" could be changed to "-type d " – XYZ_Linux Sep 11 '19 at 07:02
13

You can use:

# find . -type f  -name 'text_for_search'

If you want use REGX use -iname

# find . -type f  -iname 'text_for_search'
Benjamin W.
  • 33,075
  • 16
  • 78
  • 86
user8848899
  • 131
  • 1
  • 3
11

Following command will list down all the files having exact name "pattern" (for example) in current and its sub folders.

find ./ -name "pattern"

Anshul Singhal
  • 989
  • 11
  • 19
9

for file search
find / -xdev -name settings.xml --> whole computer
find ./ -xdev -name settings.xml --> current directory & its sub directory

for files with extension type

find . -type f -name "*.iso"
Alberto
  • 143
  • 2
  • 6
9

Default way to search for recursive file, and available in most cases is

find . -name "filepattern"

It starts recursive traversing for filename or pattern from within current directory where you are positioned. With find command, you can use wildcards, and various switches, to see full list of options, type

man find

or if man pages aren't available at your system

find --help

However, there are more modern and faster tools then find, which are traversing your whole filesystem and indexing your files, one such common tool is locate or slocate/mlocate, you should check manual of your OS on how to install it, and once it's installed it needs to initiate database, if install script don't do it for you, it can be done manually by typing

sudo updatedb

And, to use it to look for some particular file type

locate filename

Or, to look for filename or patter from within current directory, you can type:

 pwd | xargs -n 1 -I {} locate "filepattern"

It will look through its database of files and quickly print out path names that match pattern that you have typed. To see full list of locate's options, type: locate --help or man locate

Additionally you can configure locate to update it's database on scheduled times via cron job, so sample cron which updates db at 1AM would look like:

0 1 * * * updatedb

These cron jobs need to be configured by root, since updatedb needs root privilege to traverse whole filesystem.

Aleksandar Pavić
  • 2,463
  • 1
  • 25
  • 30
8

Use

find path/to/dir -name "*.ext1" -o -name "*.ext2"

Explanation

  1. The first parameter is the directory you want to search.
  2. By default find does recursion.
  3. The -o stands for -or. So above means search for this wildcard OR this one. If you have only one pattern then no need for -o.
  4. The quotes around the wildcard pattern are required.
Shital Shah
  • 47,549
  • 10
  • 193
  • 157
7

I am surprised to see that locate is not used heavily when we are to go recursively.

I would first do a locate "$PWD" to get the list of files in the current folder of interest, and then run greps on them as I please.

locate "$PWD" | grep -P <pattern>

Of course, this is assuming that the updatedb is done and the index is updated periodically. This is much faster way to find files than to run a find and asking it go down the tree. Mentioning this for completeness. Nothing against using find, if the tree is not very heavy.

melchi
  • 489
  • 5
  • 8
6

If you want to search special file with wildcard, you can used following code:

find . -type f -name "*.conf"

Suppose, you want to search every .conf files from here:

. means search started from here (current place)
-type means type of search item that here is file (f).
-name means you want to search files with *.conf names.

Saman Bayat
  • 218
  • 2
  • 11
6

Below command helps to search for any files

1) Irrespective of case
2) Result Excluding folders without permission
3) Searching from the root or from the path you like. Change / with the path you prefer.

Syntax :
find -iname '' 2>&1 | grep -v "Permission denied"

Example

find / -iname 'C*.xml' 2>&1 | grep -v "Permission denied"

find / -iname '*C*.xml'   2>&1 | grep -v "Permission denied"
MukeshKoshyM
  • 404
  • 1
  • 7
  • 16
  • 1
    why on earth do you use grep for that? Just redirect stderr to null `find / -iname '*C*.xml' 2>/dev/null` – phuclv Mar 23 '19 at 05:51
3

This will search all the related files in current and sub directories, calculating their line count separately as well as totally:

find . -name "*.wanted" | xargs wc -l
Jay Yang
  • 131
  • 1
  • 3
-1

With Python>3.5, using glob, . pointing to your current folder and looking for .txt files:

 python -c "import glob;[print(x) for x in glob.glob('./**/*txt', recursive=True)]"

For older versions of Python, you can install glob2

Katu
  • 739
  • 1
  • 17
  • 27