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
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:
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*" {}
链接地址: http://www.djcxy.com/p/78004.html
上一篇: UNIX命令中的隐式系统调用
下一篇: Unix命令查找所有文件