Advertisement
Advertisement


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


Question

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

2016/08/11
1
1973
8/11/2016 1:32:12 PM

Accepted Answer

Use find for that:

find . -name "foo*"

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

2016/07/13
2869
7/13/2016 3:45:12 PM

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
2014/02/03

find will find all files that match a pattern:

find . -name "*foo"

However, if you want a picture:

tree -P "*foo"

Hope this helps!

2014/10/16

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

2017/01/14

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

2018/06/13

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.

2018/08/20

Source: https://stackoverflow.com/questions/5905054
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]