find all files in current folder & subfolders

We all know Linux is all about files and folders and there are times when we just want to recursively find files in Linux based on wildcard match in the current folder and its subfolders.

A simple answer is to use the find command like below

find . -name "foo*"

But as sysadmins we will not limit ourselves to just the find command, so let’s jump in a little deeper and see various ways to achieve something trivial.

A much faster way is to use locate command, but this command is dependent on updatedb, assuming we have our updatedb updated, simple run

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

to find any file quickly.

Now if you have a shell with new globbing enabled(you can read more about globbing here). YOu can enable globbing using “shopt -s globstar” you can simply use below to find any files or folders recursively. This is supported by Bash 4, zsh and similar shells.

echo **/*foo*

or you can have a function defined in your shell

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

$ f some_filename

There is also this neat utility called as fd, this is a simple and fast alternative to find, and is available at GitHub for free to download and test syntax to follow:

fd PATTERN
Stackoverflow User: kenorb

If you want to look for a filename or pattern from within the current directory, you can type:

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

However, if you want a picture:

tree -P "*foo"

Well, that’s it for find, please keep following my blog for more such articles.

%d bloggers like this: