With the Linux find utility, you can perform powerful searches on just about any criterion you can think of, and then, from the same command-line entry, invoke another utility to do whatever you need to do with the results.
In order to perform the most basic of searches, you’ll need to specify three things:
directory or search path
You can perform a search in either a specific path, or the entire filesystem. Since find is inherently recursive, the search will automatically extend to all of the subdirectories beneath of the directory that you specify. Of course, you can also add command switches that limit the depth of the search.
option or if you dereference symbolic links
Dereferencing means that that if find locates a symbolic link it uses the file the symbolic link points to. However, the default option for find is to not derefence links, in other words it acts on the symbolic link. Here are the options that can be used.
-H if a symbolic link is specified it works with the file not the symbolic link, only effects specified symbolic links
-L acts on the file not the symbolic link for all found
-P acts on the symbolic link not the file for all found
expression or what you’re searching for
There are a lot of ways that you can specify this. You can search for files of a specific name, and decide whether to make the search case-sensitive. You can also use wildcards, or search for files with certain characteristics or that are of a certain age. Or, you can combine multiple criteria for even more specific searches. The main thing that limits you is your own imagination.
Here the simplest use of find searches for all files with the ending “.conf”. The “*” acts as a wild card. When used in the /etc directory this will search recursively through sub-directories as this is the typical function of find.
find -iname '*.conf'
You can also specify the directory that you would like to search.
find /etc -iname '*.conf'
The find command can be used with other commands by piping the output of find into another command. Here the output of find is sent to the wc command with the “l” option that counts the number of lines.
find /etc -iname '*.conf' | wc -l
Use find to locate specific files. This example is using the present workign directory.
find /etc -iname 'syslog.conf'
The disadvantage of using find when searching the entire file system is that hundreds of file fly by the screen befor it finds the match. It does report the result at the end.
find . -iname 'syslog.conf'
One way to limit the screen ouput is to use tail. This provides the last line, which is your solution.
find / -iname 'syslog.conf' | tail -n 1