[Next] [Previous] [Up] [Top] [Contents] [Index]
Chapter 6: The UNIX File System
This section gives a cursory overview of simple uses for two very powerful commands for dealing with files: find
for searching for files and grep
for searching for strings within files. We also describe wc
which displays the size of a file, od
which creates a dump of a file, and file
which can determine file type.
The find
utility tests each file in the given pathname list to see if it meets the criteria specified by the expression supplied. It does this by recursively descending the directory hierarchy for each path name. The format is:
% find path-name-list expression
path-name-list can contain file expansion metacharacters. Each element in expression is a separate boolean criterion. A space separating two criteria is a logical AND operator, a -o
separating the criteria is a logical OR operator. A criterion can be negated by preceding it with an exclamation point (!
). Criteria are evaluated from left to right unless parentheses are used to override this. Special characters must be quoted (use \
) and there must be spaces on each side of the special character pair.
Some of the criteria that can be used within expression are:
-name filename
True if filename matches the name of the file being evaluated. Ambiguous file references can be used if enclosed in quotes.
-type filetype
True if the type of the file is filetype, where filetype is either d
(directory) and f
(ordinary file).
-atime n
True if the file has been accessed in n days.
-mtime n
True if the file has been modified in n days.
-newer filename
True if the file has been modified more recently than filename has.
-print
Causes the matching path names to be displayed on the screen.
-exec command
\;
True if command returns a zero exit status. command must be terminated with a quoted semicolon (note the \
). An empty pair of braces ({}) within the command represents the filename of the file being evaluated.
-ok command
\;
Same as -exec
except the generated command line is displayed and executed only if the user responds by typing y
.
In the previous list, +n means more than n, -n
means less than n, n means exactly n.
[Missing image]Note that find
doesn't do anything with the found files, it doesn't even display the names, unless instructed to.
Examples:
lostfile
:
% find . -name lostfile -print
.html
in your /wwwork
subdirectory:
% find wwwork -name '*.html' -print
more
on each file that begins with the letter d
in the current directory and all subdirectories (Enter y
if you want the file displayed.):
% find . -name 'd*' -ok more {} \;
m
:
% find . ! -name 'm*' -print
% find . -exec grep -l "hello" {} \;
a.t
or have the extension of .o
and haven't been accessed in a week:
% find ~\(-name a.t -o -name '*.o') -atime +7 -exec rm {} \;
[Missing image]Note that using the find command uses many system resources.
[Missing image]In particular on AFS systems, you may accidentally end up searching servers all over the world if the top of the search is at the root directory (/
). Generally you should be careful to only search the part of the UNIX tree that interests you. Here is an example:
/
), and exclude searches in the /afs
and /nfs
branches:
% find / \( -name /afs -prune \) -o \( -name /nfs -prune \) -o -name filename -print
The grep
utility searches the contents of one or more files for a pattern.
% grep [options] pattern [file ...]
Some of the options are:
-c
Display only a count of lines that contain the pattern.
-i
Ignore upper/lower case distinctions during comparisons.
-l
Display only the name of each file that contains one or more matches.
The pattern can be a simple string or a regular expression (see section 5.4.5). You must quote regular expressions that contain special characters, spaces, or tabs (this can be done by enclosing the entire expression within single quotation marks).
Examples:
% grep -i smith *
abc
for a string beginning with f, followed by 0 or more r's, and ending in og (e.g., frog, fog, frrog):
% grep 'fr*og' abc
myfile
for a line beginning with a T
:
% grep '^T' myfile
or
% less myfile | grep '^T'
/usr/jones/junk
for the characters file followed by a number (e.g., file1, file3):
% grep 'file[0-9]' /usr/jones/junk
% who | grep smith
% ps -ef | grep smith
% env | grep string
% alias | grep string
The wc
command, which stands for word count, counts the number of lines, words, and characters there are in the named files, or in the standard input if the argument is absent. If there is more than one file, wc
totals the count as well.
% wc [-lwc] [names]
The options l
, w
, and c
may be used in any combination to specify that a subset of l
ines, w
ords, and c
haracters be reported. The default is -lwc.
UNIX users frequently count things by piping them into wc
. For example, to display the number of users logged into the system, you can execute:
% who | wc -l
The od
(o
ctal d
ump)
command can be used to examine the contents of a file in various formats: octal, decimal, hexadecimal, and ASCII. The default is octal.
The format is:
% od [options] [file] [offset] [|less]
If file is not included, standard input is assumed. The options are:
-c
Produces a character dump.
-d
Produces a decimal dump.
-o
Produces an octal dump.
-x
Produces a hexadecimal dump.
The -c
option prints non-printable characters as a printable character preceded by a backslash: \0
is null, \b
is backspace, \f
is form-feed, \n
is new-line, \r
is return, and \t
is tab.
The offset specifies where in the file the dump is to begin, if different than the beginning of the file. It is of the form [+]n[.][b]
. The +
is only necessary if you have no file specified so that the command interpreter knows this is the offset not the file. Without .
or b
, n indicates the dump starts at (octal) byte n of the file. A .
displays n in decimal, a b
in 512-byte blocks.
[Missing image]We recommend that you always pipe the output of od
to less
(or more
) so that you can manipulate it. Large files can be unwieldy, and you may not be able to stop the output once it's going!
The file
utility can be used to determine the file type of a file according to its contents. It bases its guesses on a list of "magic numbers" recorded in a "magic file", /etc/magic
. Some of the file types are:
file
determines the filetype by looking at the beginning of the file and comparing it to entries in the magic file. The command format is:
% file filename...