Previous Table of Contents Next


Finding a File

To find a file by searching from the home directory, type find $HOME -name filename -print and press Return. The $HOME variable starts the search with the home directory. The -name option looks for the name specified in the filename variable. The -print option displays the results of the find. If the named file is not found, the prompt is redisplayed.

This example shows the results of a find looking for core files:

oak% find $HOME -name core -print
/home/ignatz/core
oak%

Table 1-7 shows some of the options to the find command that you can use to focus your searches.

Table 1-7 Options to the find Command

Option Description
-fstype type Finds files of the file system type you specify (typically ufs or nfs).
-prune Limits the search to the specified directory.
-nouser Finds files that belong to a user not in the /etc/passwd database.
-nogroup Finds files that belong to a group not in the /etc/group database.
-atime n Finds files that have been accessed within the last n days.
-mtime n Finds files that have been modified within the last n days.
-ctime n Finds files that have been changed within the last n days. Changes can include changing its attribute such as the number of links, its owner, or its group.
-Xdev Restrict search to one file system.

See the find(1) manual page for a complete list of options.

Finding the Type of a File

Sometimes you need to determine the type of a file. To find the type of a file, type file filename and press Return. The output of the command makes an educated guess about the type of the file.

For example, if a user is trying to execute an ASCII file that does not have execute permissions, or execute an empty file, displaying the file type will tell you whether the system recognizes the file as a command.

In this example, the file is empty:

anastasia% file junk
junk: empty file
anastasia%

In this example, the file is an ASCII text file:

anastasia% file junk
junk: ascii text
anastasia%

In this example, the file is a text file with executable permissions, so the file command reports that the file contains commands and is text:

anastasia% chmod 777 junk
anastasia% file junk
junk: commands text
anastasia%


NOTE:  You can, of course, determine if the command has execute permissions using the ls -l command.

To show the file type for all files in a directory, type file * and press Return. The files are listed in alphabetical order followed by the file type:

$ file *

coterie:     directory
course:      ascii text
dead.letter  ascii text
ksyms        English text
people:      directory
personal:    directory
showrev:     ascii text
status:      directory
text:        directory
todo:        ascii text
$

Finding Information in Files

You can use the grep and egrep commands to search files and command output for specific information.

Searching Files for Text Strings To search files for a specific text string, type grep search-string filenames and press Return. Lines in the files containing the string are displayed.

In this example, the passwd file is searched for lines containing csh:

oak% grep csh /etc/passwd
ignatz::6693:1Ø:Iggy Ignatz 646Ø7:/home/ignatz:/bin/csh
fred::14Ø72:1Ø:Fred Lux:/home/fred:/bin/csh
oak%

You can search more than one file by specifying a series of file names separated by spaces, or by using metacharacters such as the asterisk (*) or question mark (?) together with (or in place of ) the file name.

To print out lines that do not contain the specified string, type grep -v search-string filename and press Return. Lines in the file that do not contain the string are displayed.

Searching Input for Lines with a Given Pattern You can use the grep command with pipes in combination with many administrative commands. For example, if you want to find all of a user's current processes, pipe the output of the ps command to grep and search for the user name, type ps -e | grep name, and press Return. The listing for the name you specify is displayed.

For example, to find the OpenWindows process:

oak% ps -e | grep openwin
PID TTY      TIME COMD
2212 pts/Ø    Ø:ØØ openwin
oak%

Looking at Files

You will undoubtedly spend lots of time looking at the content of files. When you need to look at the entire file, use the more command. When the information you need is at the end of the file (for example, in a log file), use the tail command to display the last 10 lines of the file. When important information is at the beginning of the file, use the head command to display the first 10 lines of the file.


Previous Table of Contents Next