Navigating the Linux File System
◾
97
it by one or more debugging options of
help
,
tree
,
stat
,
opt
, and
rates
.
*
Ordering of
expressions allows find to attempt to optimize the expressions provided in order to reduce
search time. Ordering is specified by -O followed by an optimization number with no
space between them, as in
-O3
. The optimization number is between 0 and 3 with 3 being
the most optimized. Note that while optimization will reduce search time, it takes longer
to set up the search query. Therefore, optimization has a tradeoff
and the higher the opti-
mization level, the slower find will be to start. Depending upon the size of the file space
being searched, optimization may or may not be useful.
Aside from these options, you can also specify options in the expression portion of the
command. These include
-daystart
so that -amin, -atime, -cmin, -ctime, -mmin, and
-mtime all start from the start of the day rather than 24 hours earlier. For instance, by say-
ing “search for files created 1 day ago,” if you add -daystart, then “1 day ago”
literally means
at any time of the day before midnight” rather than “exactly 24 hours ago.”
The option
-depth
indicates that item should be sought (or acted upon) before the
current directory itself is acted upon. This is useful when doing a recursive deletion. Next,
-maxdepth
, which is followed by a nonnegative integer, indicates how far down the
file system find should search from the given directory. Typically, find will search all
subdirectories from the directory specified to the leaf level of the file space. By indicating
-maxdepth 1
, aside from the current directory, find will only search subdirectories of the
current directory. There is also a
-mindepth
. Finally,
-mount
indicates that find should
not go down any directories that are mounted.
The default action for find is to list all matches found to the terminal window. Find can
also
perform other operations, as indicated in Table 3.7.
As the find program is far more complicated than the GUI Search for Files program, you
might question its use. However, keep in mind that the GUI program takes more resources
to run and would be unavailable if you are accessing a Linux machine through a text-only
interface. Additionally, the find command offers a greater degree of control. Finally, find
can be used in a shell script or in a pipe and obviously the GUI search program cannot. It
is worthwhile learning find, but be patient in learning to use it correctly.
3.4.3 Other Means of Locating Files
There are several other options to locate items in the file system. To locate an executable
program, use
which
name
where
name
is the program’s name. This works as long as
the program is located in a directory that is part of the user’s PATH variable and as long
as the program is accessible by the user. Try for instance
which useradd
and you
will
receive an error because you, the user, do not have access to useradd. The program
whereis
is similar in that it will locate a program’s location. But whereis does not rely
on the PATH variable. Additionally, if available, whereis will also report on all locations
relevant to the program: the executable program itself (binary), the source code (usually
stored in a /lib subdirectory) and the manual page (usually stored under /usr/share/man).
*
See find’s man page to explore debugging options in more detail.
98
◾
Linux with Operating System Concepts
Two other available programs are
locate
and
slocate
. Both of these commands
access a database storing file locations. To update (or create)
the database for the locate
command, use the command
updatedb
. The option -U allows you to specify a portion of
the filespace to update. For instance,
updatedb -U /home/foxr
would only update
the portion of the database corresponding to user foxr’s home directory. Once the database
is established, the locate instruction is simply
locate [
option
]
string
which responds with the location within the file system of all files that match the given
string
. The common options are -c to count the number of matches found and output the
count rather than the matched files, -d
file
to use
file
as a substitute database over the
default database, -L to follow symbolic links, -i to ignore case, -n # to exit after # matches
have
been found, and -r
regex
to use the regular expression (
regex
) instead of the string.
The slocate instruction is newer and is used with SELinux. Unlike locate, slocate can
operate without updatedb by using the -u or -U option. With -u, the database is updated
by examining the entire file system. With -U, as with updatedb, you can specify a portion
of the file system.
Keep in mind that updating the database can be time consuming. Users are not allowed
to update the database because they will not have write access to the database. Therefore,
updating is done by the system administrator. And to prevent the update from taking too
TABLE 3.7
Find Actions
Do'stlaringiz bilan baham: