Performing More Powerful Searches with find




Download 7,3 Mb.
Pdf ko'rish
bet33/115
Sana27.11.2023
Hajmi7,3 Mb.
#106243
1   ...   29   30   31   32   33   34   35   36   ...   115
Bog'liq
linuxbasicsforhackers

11
Performing More Powerful Searches with find
The 
find
command is the most powerful and flexible of the searching utili-
ties. It is capable of beginning your search in any designated directory and 
looking for a number of different parameters, including, of course, the file-
name but also the date of creation or modification, the owner, the group, 
permissions, and the size.
Here’s the basic syntax for 
find
:
find directory options expression
So, if I wanted to search for a file with the name apache2 (an open source 
web server) starting in the root directory, I would enter the following:
kali >find /

 -type f

 -name apache2
w
First I state the directory in which to start the search, in this case 
/
. 
Then I specify which type of file to search for, in this case 
f
for an ordi-
nary file . Last, I give the name of the file I’m searching for, in this case 
apache2
w.
My results for this search are shown here:
kali >find / -type f -name apache2
/usr/lib/apache2/mpm-itk/apache2
/usr/lib/apache2/mpm-event/apache2
/usr/lib/apache2/mpm-worker/apache2
/usr/lib/apache2/mpm-prefork/apache2
/etc/cron.daily/apache2
/etc/logrotate.d/apache2
/etc/init.d/apache2
/etc/default/apache2
The 
find
command started at the top of the filesystem (
/
), went through 
every directory looking for apache2 in the filename, and then listed all 
instances found.
As you might imagine, a search that looks in every directory can be 
slow. One way to speed it up is to look only in the directory where you would 
expect to find the file(s) you need. In this case, we are looking for a con-
figuration file, so we could start the search in the /etc directory, and Linux 
would only search as far as its subdirectories. Let’s try it:
kali >find /etc -type f -name apache2
/etc/init.d/apache2
/etc/logrotate.d/apache2
/etc/cron.daily/apache2
/etc/default/apache2
This much quicker search only found occurrences of apache2 in the
/etc directory and its subdirectories. It’s also important to note that unlike 
some other search commands, 
find
displays only exact name matches. If the 


12
Chapter 1
file apache2 has an extension, such as apache2.conf, the search will not find a 
match. We can remedy this limitation by using wildcards, which enable us to 
match multiple characters. Wildcards come in a few different forms: 
*
.
,
?
and 
[]
.
Let’s look in the /etc directory for all files that begin with apache2 and 
have any extension. For this, we could write a 
find
command using the fol-
lowing wildcard:
kali >find /etc -type f -name apache2.\*
/etc/apache2/apache2.conf
When we run this command, we find that there is one file in the /etc
directory that fits the 
apache2.*
pattern. When we use a period followed 
by the 
*
wildcard, the terminal looks for any extension after the filename 
apache2. This can be a very useful technique for finding files where you 
don’t know the file extension.
When I run this command, I find two files that start with apache2 in the 
/etc directory, including the apache2.conf file.

Download 7,3 Mb.
1   ...   29   30   31   32   33   34   35   36   ...   115




Download 7,3 Mb.
Pdf ko'rish

Bosh sahifa
Aloqalar

    Bosh sahifa



Performing More Powerful Searches with find

Download 7,3 Mb.
Pdf ko'rish