[Next] [Previous] [Up] [Top] [Contents] [Index]

Chapter 6: The UNIX File System

6.4 Information About Files

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.

6.4.1 Find a File: find

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:

% find . -name lostfile -print
% find wwwork -name '*.html' -print
% find . -name 'd*' -ok more {} \; 
% find . ! -name 'm*' -print
% find . -exec grep -l "hello" {} \; 
% 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:

% find / \( -name /afs -prune \) -o \( -name /nfs -prune \) -o -name filename -print

6.4.2 Search for a Pattern: grep

The grep utility searches the contents of one or more files for a pattern.

The format is:

% 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 * 
% grep 'fr*og' abc 
% grep '^T' myfile 

or

% less myfile | grep '^T'
% grep 'file[0-9]' /usr/jones/junk 
% who | grep smith 
% ps -ef | grep smith
% env | grep string
% alias | grep string

6.4.3 Count a File: wc

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 lines, words, and characters 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 

6.4.4 Dump a File: od

The od (octal dump) 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!

6.4.5 Determine File Type: file

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... 

UNIX at Fermilab - 10 Apr 1998

[Next] [Previous] [Up] [Top] [Contents] [Index]