linux - recursively - unix command to find a file in a directory and subdirectory




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

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


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


find will find all files that match a pattern:

find . -name "*foo"

However, if you want a picture:

tree -P "*foo"

Hope this helps!


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.


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.


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

Use find for that:

find . -name "foo*"

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


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"

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)





shell