john
john

Reputation: 35410

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

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

Upvotes: 3090

Views: 4898969

Answers (19)

tux21b
tux21b

Reputation: 94779

Use find:

find . -name "foo*"

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

If you need case insensitive search use :

find . -iname "foo*"

Upvotes: 4393

XYZ_Linux
XYZ_Linux

Reputation: 3607

Use

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).

Upvotes: 77

Jay Yang
Jay Yang

Reputation: 422

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

Upvotes: 10

Aleksandar Pavić
Aleksandar Pavić

Reputation: 3430

The default way to search for files recursively, and available in most cases is

find . -name "filepattern"

It starts recursively traversing for filename or pattern from within the current directory where you are positioned. With the find command, you can use wildcards, and various switches. To see the 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 than find, which are traversing your whole filesystem and indexing your files. One such common tool is locate or slocate/mlocate. You should check the manual of your OS on how to install it, and once it's installed, it needs to initiate the database. If the install script doesn'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 a filename or pattern from within the 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 the pattern that you have typed. To see the full list of locate's options, type: locate --help or man locate

Additionally, you can configure locate to update its database on scheduled times via a cron job, so a sample cron which updates the database at 1 AM would look like:

0 1 * * * updatedb

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

Upvotes: 20

MukeshKoshyM
MukeshKoshyM

Reputation: 554

The 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 <FromDirectory> -iname '<FileName wild char allowed>'   2>&1 | grep -v "Permission denied"

Example

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

Upvotes: 10

kenorb
kenorb

Reputation: 166795

fd

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

Syntax:

fd PATTERN

Demo:

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

Upvotes: 113

user8848899
user8848899

Reputation: 311

You can use:

find . -type f  -name 'text_for_search'

If you want use a regular expression, use -iname:

find . -type f  -iname 'text_for_search'

Upvotes: 21

Alberto
Alberto

Reputation: 195

For file search

find / -xdev -name settings.xml → the whole computer
find ./ -xdev -name settings.xml → the current directory and its subdirectories

For files with an extension type:

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

Upvotes: 15

Paul Whipp
Paul Whipp

Reputation: 16541

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

Upvotes: 320

Saman Bayat
Saman Bayat

Reputation: 256

If you want to search special files with a wildcard, you can use the 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.

Upvotes: 8

Anshul Singhal
Anshul Singhal

Reputation: 2201

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

find ./ -name "pattern"

Upvotes: 13

Robert Ranjan
Robert Ranjan

Reputation: 1886

Try with the fd command if installed. Install instructions.

Find all files that start with 'name':

fd "name*"

This command ignores all .hidden and .gitignoreed files.

To include .gitignoreed files, add the -I option as below:

fd -I "name*"

To include hidden files, add the -H option as below:

fd -H "name*"

Upvotes: 8

jian
jian

Reputation: 4877

You can also use ripgrep.

Example:

Search all the filenames contain substring psql,

rg --files | rg psql

Upvotes: 1

IslandCow
IslandCow

Reputation: 3542

find will find all files that match a pattern:

find . -name "*foo"

However, if you want a picture:

tree -P "*foo"

Upvotes: 224

Katu
Katu

Reputation: 1564

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

Upvotes: -4

Shital Shah
Shital Shah

Reputation: 68858

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.

Upvotes: 46

melchi
melchi

Reputation: 667

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.

Upvotes: 14

kenorb
kenorb

Reputation: 166795

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.

Upvotes: 64

toddcscar
toddcscar

Reputation: 1225

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

Upvotes: 83

Related Questions