Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


The sleep Command

The sleep command pauses for a given number of seconds. The basic syntax is

sleep n

where n is the number of seconds to sleep or pause. Some types of UNIX enable other time units to be specified. It is usually recommended that n not exceed 65,534.

sleep can be used to give a user time to read an output message before clearing the screen. It can also be used when you want to give a series of beeps:

echo -e "A value must be input!\a"
sleep 1
echo -ne "\a"
sleep 1
echo -ne "\a"

\a causes echo to output an audible beep. -e is required on some UNIX systems for \a to sound a beep. -n suppresses the newline that echo normally prints. The sleep command is used in the previous example to give a sequence of beeps, spaced one second apart.

sleep can be used in a loop to repeat a job periodically:

while :
do
   date
   who
   sleep 300
done >> logfile

This code enables a list of who is logged into the system to be appended to logfile every 5 minutes (300 seconds).


Note:  
If you want to leave this code running all the time, you must clear logfile periodically so that it does not eat up all your disk space.

The find Command

The find command is a very powerful, very flexible way to create a list of files that match given criteria. The basic syntax is

find start-dir options actions

Here is a simple find example:

find / -name alpha -print

This example looks for all files named alpha and displays the full pathname to the screen (standard output). It is a useful command to know about when you are sure you have a file named alpha but can’t remember what directory it is in or want to know whether it exists in more than one directory. Here is some possible output from that command:

/reports/1998/alpha
/reports/1998/region2/alpha
/tmp/alpha

I will shortly cover the elements of the find command in detail. Files can be selected not only by name but also by size, last modification date, last access date, and so on. First let me give you a more complex example with a brief explanation of each part of the example, so you get a sense of what options and actions look like:

find /reports/1998 -name alpha -type f -print -exec lp {} \;

Table 18.1 provides a breakdown of these elements.

Table 18.1 A Sample find Command

Command Element Description

/reports/1998 The starting directory. find looks only in this directory and its subdirectories for files that match the following criteria.
-name alpha An option that says you are looking only for files whose name is alpha/reports/1998/region2/alpha, for example. find does not check any words in the directory portion of a filename for alpha. It checks only the filename itself, which is also called the file basename.
-type f An option that says you are looking only for files of type f, which means regular or normal files, and not directories, device files, and so on. Any files selected must match both conditions: they must have the name alpha and they must be a regular file.
-print An action that says to display to standard output the pathname for any files that match the criteria given by the options.
-exec lp {} \; An action that says to use the lp command to print a hard copy of any files that match the criteria. Multiple actions can be specified.

find: Starting Directory

A UNIX system can contain a huge number of files, often so many that find can take several minutes or more to complete. For this reason, find enables specifying a starting directory to narrow down the number of files it has to search. Only files in this directory and all its subdirectories are checked. find enables either an absolute or relative pathname for the starting directory. If you specify an absolute pathname such as /reports,

find /reports -name alpha -print

then all the files found are specified as absolute pathnames, as in this sample output:

/reports/1998/alpha
/reports/1998/region2/alpha

If you specify a relative pathname to find,

cd /reports
find ./1998 -name alpha -print

all the files are displayed relative to the starting directory. For example,

./1998/alpha
./1998/region2/alpha

To search the whole system, specify / as the starting directory. This indicates the system root directory that includes all other files and directories:

find / -name alpha -print

To search the entire system and still display all found files as relative pathnames, use the following:

cd /
find . -name alpha -print

Sample output:

./reports/1998/alpha
./reports/1998/region2/alpha

This point about relative versus absolute pathnames is important if you are using find to generate a list of files to be backed up. It is better to back up using relative pathnames that enable the files to be restored to a temporary directory.

Some versions of UNIX let you search multiple directories with one find command:

find dir1 dir2 -name alpha -print

Refer to the man page about find on your UNIX system to see whether it enables multiple directories.


Previous Table of Contents Next