Navigating the Linux File System
◾
95
find /etc -name “*.conf”
Notice that the instruction
ls /etc/*.conf
is similar but in ls, you are only examining files in /etc, not its subdirectories. You could use
-R in ls to recursively search all subdirectories so it performs similarly to find.
A variation
of the option -name is -iname, which does the same thing but is case insensitive.
The find command provides numerous options which increase its usefulness over the ls
command. Options are divided into three categories. First are options similar to most Linux
commands which cause the program to function differently. Second are options that the
user specifies to tailor the search. These are
search criteria
. Table 3.6 illustrates some of the
more useful expressions. Third are options that control what find will do when it has located
an item that matches the expression(s). We will consider these later in this subsection.
The notation
[
+
-]n
from Table 3.6 needs a bit of explanation. These expressions can
either be followed by an integer number, a
+
integer or a –integer. The integer by itself, n,
means an exact match.
The values
+
n and –n indicate “greater than n” and “less than n,”
respectively. For
instance,
-size 1000c
means “exactly 1000 bytes in size” while
–size
-1000c
means “less than 1000 bytes in size.”
The various expressions listed in Table 3.6 can be combined using Boolean connectors.
To specify that two (or more) expressions must be true, combine them with
–and
or
–a
(or
just list them). For instance, the following
three are all equivalent, which will seek out all
files under the user’s home directory that are between 100 and 200 bytes.
• find ~ -size
+
100c -size -200c
• find ~ -size
+
100c -a -size -200c
• find ~ -size
+
100c -and -size -200c
To specify that any of the expressions should be true, combine them with
–o
or
–or
.
The following expression looks for any item owned by user 500 or group 500.
• find ~ -uid 500 -o -gid 500
To specify that the given expression must not be true, use either
!
expression or
–not
.
For instance, the following two both search for noncharacter-based files.
• find ~ ! -type c
• find ~ -not -type c
If your expression contains numerous parts and you need to specify operator prece-
dence, place them within parentheses. Without parentheses, not is applied first, followed
by and, followed by or.
96
◾
Linux with
Operating System Concepts
There are five options available for find. The first three deal with the treatment of sym-
bolic links. The option -P specifies that symbolic links should never be followed. This is
the default behavior and so can be omitted. The option -L forces symbolic links to be fol-
lowed. What this means is that if an item in a directory is actually a symbolic link, then the
expression is applied not to the link but to the item being pointed at.
So for instance, if a
symbolic link points to a character-type file and the expression is
-type c
, then the file
will match. If instead, option -P (or the default) is used, the file does not match because the
symbolic link is not a character-type file.
The option -H is a little trickier to understand. Like -P, it prevents find from following
symbolic links when searching for files. However, as we will see below,
find can also have
an action to perform on any item that matches the expression. With -H, while find does not
follow symbolic links to identify a file, symbolic links can be followed when performing an
action. As this is a complex concept, we will not go into it in any more detail.
The final two options are to receive debugging information and ordering of expressions.
Debugging information is useful if you suspect that your find operation has some errors in
its logic or is not efficiently organized. To specify debugging use
the option -D and follow
TABLE 3.6
Expressions Used in Find
Do'stlaringiz bilan baham: