Sino February 2016

Unix command to find all files

I need a unix command to list all files that contains 'foo' in their name ? We have two commands that do that : grep command and find command !! what's the best?

Thanks

Answers


obi1 February 2016

Use find to list all files on the system.

Pair that up with grep to search for a specific file name.

like this:

find / -type f -exec grep -i -r "*foo*" {}


Thomas Dickey February 2016

The find command by itself suffices (unless you want to include files in directories whose name includes "foo"):

find / -type f -name '*foo*'

That checks the leaf name (last part) of the pathnames. If you piped the result of find through grep in a similar way:

find / -type f | grep foo

it would match those files, as well as all files (and directories) inside directories whose name includes "foo".

To filter the list in a more interesting way, you can use grep, which supports regular expressions and other features. For example, you could do

find / -type f | grep -i foo

to match "foo" ignoring case.

But if you want to look at the contents of files, that is grep-specific:

find / -type f -exec grep foo {} +

Further reading:

Post Status

Asked in February 2016
Viewed 1,808 times
Voted 6
Answered 2 times

Search




Leave an answer