Intro to the UNIX OS.

Commands E-mail Glossary Index


On this page:


line.


Section 10 - Special-use Commands

Comparing Two Files

Using the diff Command

The diff command is used to display line-by-line differences between pairs of text files. The format for the diff command is:

diff [-bitw][-c[#]|-e|-f|-n|-h] filename1 filename2

diff is a differential file-comparing command. When run on regular files, diff tells what lines must be changed in the files to bring them into agreement. Except in rare circumstances, diff finds the smallest sufficient set of differences. If neither filename1 nor filename2 is a directory, either may be given as "-", in which case the standard input is used. If filename1 is a directory, a file in that directory, whose filename is the same as the filename of filename2, is used (and vice versa).

There are several options for output format; the default output format contains lines of these forms:

n1 a n3,n4
n1,n2 d n3
n1,n2 c n3,n4

Following each of these lines come all the lines that are affected in the first file flagged by "<", then all the lines that are affected in the second file are flagged by ">".

If both arguments are directories, diff sorts the contents of the directories by name and then runs the regular file diff program as described above on text files that are different. Binary files that differ, common subdirectories, and files that appear in only one directory are listed.

diff Options

Options for diff Command
Option Meaning
-b Ignores trailing blanks (space and tab characters) and treats all other strings of blanks as equivalent.
-i Ignores the case of letters; for example, "A" will compare equal to "a".
-e Produces a script of a, c, and d commands for the editor ed, which will recreate filename2 from filename1.

Searching for a Text String, Counting Words and Lines

Using the grep Command

The grep, egrep, and fgrep commands are used to search a file for a string or regular expression. The formats for the commands are:

grep [-bchilnsvw][-e expression][filename... ]

egrep [-bchilnsv][-e expression][-f filename][expression][filename... ]

fgrep [-bchilnsvx][-e string][-f filename][string][filename... ]

Commands of the grep family search the input filenames (the standard input default) for lines matching a pattern. Normally, each line found is copied to the standard output. grep patterns are limited, regular expressions in the style of ed. egrep patterns are full, regular expressions including alternation. fgrep patterns are fixed strings--no regular expression metacharacters are supported.

In general, egrep is the fastest of these programs.

Take care when using the characters $, *, [, ^, |, (, ), and \ in the expression, since these characters are also meaningful to the shell. It is safest to enclose the entire expression argument in single quotes ('...').

When any one of the grep utilities is applied to more than one input file, the name of the file is displayed preceding each line that matches the pattern. The filename is not displayed when processing a single file, so if you actually want the filename to appear, use /dev/null as a second file in the list.

grep Family Options

Options for grep Command
Option Meaning
-c Displays a count of matching lines, rather than displaying the lines that match.
-i Ignores the case of letters in making comparisons; that is, upper and lowercase are considered identical.

Using the wc Command

The wc command is used to display a count of lines, words, and characters. The format of the wc command is:

wc [-lwc][filename ... ]

wc counts lines, words, and characters in filenames or in the standard input, if no filename appears. It also keeps a total count for all named files. A word is a string of characters delimited by space, tab, or newline characters.

wc Options

When filenames are specified on the command line, their names will be printed along with the counts.

The default is -lwc (count lines, words, and characters).

Options for wc Command
Option Meaning
l Counts lines.
w Counts words.
c Counts characters.

Searching for a File

Using the find Command

The find command is used to find files by name or by other characteristics. The format of the findcommand is:

find pathname-list expression

or

find component

find recursively descends the directory hierarchy for each pathname in the pathname-list, seeking files that match a logical expression written using the operators listed under the next topic.

find does not follow symbolic links to other files or directories; it applies the selection criteria to the symbolic links themselves as if they were ordinary files (see ln for a description of symbolic links).

Using find Operators

In the descriptions, the argument n is used as a decimal integer where +n means more than n, -n means less than n, and n means exactly n.

find Operators
Command Meaning
-name filename True if the filename argument matches the current filename. Shell argument syntax can be used if escaped (watch out for [, ?, and *).
-prune Always yields true. Has the side effect of pruning the search tree at the file. That is, if the current pathname is a directory, find will not descend into that directory.
-type c True if the type of the file is c, where c is one of the following:

Type Meaning
b for block special file c
c for character special file
d for directory
f for plain file
p for named pipe (FIFO)
l for symbolic link
s for socket
-links n True if the file has n links.
-user uname True if the file belongs to the user uname. If uname is numeric and does not appear as a log-in name in the /etc/passwd database, it is taken as a user ID.
-nouser True if the file belongs to a user not in the /etc/passwd database.
-group gname True if the file belongs to group gname. If gname is numeric and does not appear as a log-in name in the /etc/group database, it is taken as a group ID.
-nogroup True if the file belongs to a group not in the /etc/group database.
-atime n True if the file has been accessed in n days. NOTE: The access time of directories in pathname-list is changed by find itself.
-mtime n True if the file has been modified in n days.
-ctime n True if the file has been changed in n days. "Changed" means either that the file has been modified or some attribute of the file (its owner, its group, the number of links to it, etc.) has been changed.
-exec command True if the executed command returns a zero value as exit status. The end of command must be punctuated by an escaped semicolon. A command argument { } is replaced by the current pathname.
-print Always true; the current pathname is printed.
-ls Always true; prints current pathname together with its associated statistics.
-depth Always true; find descends the directory hierarchy, acting on the entries in a directory before acting on the directory itself. This can be useful when find is used with cpio to transfer files that are contained in directories without write permission.

Searching for a Command

UNIX which Command

The which command is used to locate a command and display its pathname or alias. The format of the which command is:

which [commandname]

The which command takes a list of names and looks for the files that would be executed had these names been given as commands. Each argument is expanded if it is aliased and searched for along the user's path. Both aliases and the path are taken from the user's .cshrc file.

Making a Tape Archive

Using the tar Command

The tar command is used to make a tape (or other media) archive file. The format of the tar command is:

tar -[BcdDhiklmopRstvxzZ][-b N][-f F][-T F][filename or regular expression]

tar provides a way to store many files into a single archive. This can be kept in another UNIX file; stored on an I/O device, such as tape, floppy, cartridge, or disk; sent over a network; or piped to another program. It is useful for making backup copies or for packaging up a set of files to move them to another system.

tar has existed since Version 7 UNIX with very little change. It has been proposed as the standard format for interchange of files among systems that conform to the IEEE P1003 "Portable Operating System" standard.

tar Options

Options for tar Command
Option Meaning
-b N Specifies a blocking factor for the archive. The block size will be Nx512 bytes. Larger blocks typically run faster and let you fit more data on a tape. The default blocking factor is set when tar is compiled and is typically 20. There is no limit to the maximum block size, as long as enough memory can be allocated for it and as long as the device containing the archive can read or write that block size.
-B When reading an archive, reblocks it as it is read. Normally, tar reads each block with a single read system call. This does not work when reading from a pipe or a network socket under Berkeley UNIX; read only gives as much data as has arrived at the moment. With this option, it will do multiple reads to fill out to a record boundary, rather than reporting an error. This option is default when reading an archive from standard input or over a network.
-c Creates an archive from a list of files.
-f F Specifies the filename of the archive. If the specified filename is "-", the archive is read from the standard input or written to the standard output. If the -f option is not used, and the environment variable TAPE exists, its value will be used; otherwise, a default archive name (which was picked when tar was compiled) is used. The default is normally set to the "first'' tape drive or other transportable I/O medium on the system. If the filename contains a colon before a slash, it is interpreted as a hostname:/file/name pair. tar will invoke the commands rsh and dd to access the specified file or device on the system hostname. If you need to do something unusual like rsh with a different username, use -f - and pipe it to rsh manually.
-t Lists a table of contents of an existing archive. If filenames are specified, just list files matching the specified names. The listing appears on the standard output.
-v Be verbose about the files that are being processed or listed. Normally, archive creation, file extraction, and differencing are silent, and archive listing just gives filenames. The -v option causes an ls -l similar listing to be produced. The output from v appears on the standard output except when creating an archive (since the new archive might be on standard output), where it goes to the standard error output.
-x Extract files from an existing archive. If filenames are specified, just extract files matching the specified names, otherwise extract all the files in the archive.

Execution of a Command Prior to Each Prompt

T-shell supports a special alias, precmd, which, if set, holds a command that will be executed before printing each prompt. For example, if the user has done

> alias precmd date

then the program date will be run just before the shell prompts for each command. There are no limitations on what precmd can be set to do, although discretion should be used.

Periodic Command Execution

T-shell is now capable of providing periodic command execution through the use of the shell variable tperiod and the alias periodic. When these items are set, the alias periodic will be executed every tperiod minutes. This provides a convenient means for checking on common but infrequent changes, such as new messages. For example:

> set tperiod = 30
> alias periodic checknews

This will cause the checknews program to be run every 30 minutes. Having the alias periodic set, but with an unset tperiod (or a value of 0 for tperiod), will cause periodic to degenerate to another form of precmd.

T-shell Prompt Format

The format for the prompt shell variable includes many things, such as the current time of day, and current working directory. The new format uses %<char> to signal an expansion, much like printf. The available sequences are:

Sequences
Symbol Meaning
%/ Current working directory.
%~ cwd. If it starts with $HOME, that part is replaced by a ~. In addition, if a directory name prefix matches a user's home directory, that part of the directory will be substituted with ~user. NOTE: The ~user substitution will only happen if the shell has performed a ~ expansion for that username in this session.
%c or %. Trailing component of cwd; may be followed by a digit to get more than one component. If it starts with $HOME, that part is replaced with a ~.
%C Trailing component of cwd, may be followed by a digit to get more than one component, no ~ substitution.
%h, %!, ! Current history event number.
%M The full machine hostname.
%m The hostname up to the first ".".
%S (%s) Start (stop) standout mode.
%B (%b) Start (stop) boldfacing mode. (Only if T-shell was compiled to be 8-bit clean.)
%U (%u) Start (stop) underline mode. (Only if T-shell was compiled to be 8-bit clean.)
%t or %@ Current time of day in 12-hour, am/pm format.
%T Current time of day in 24-hour format. (But see the ampm shell variable below.)
ESC-c "c" is parsed the same way as in bindkey.
Ctrl-c "c" is parsed the same way as in bindkey.
%% A single %.
%n The username, contents of $user.
%d The weekday in <Day> format.
%D The day in dd format.
%w The month in <Mon> format.
%W The month in mm format.
%y The year in yy format.
%Y The year in yyyy format.
%l The line (tty) the user is logged on.
%L Clear from prompt to end of display or end of line.
%# A "#" if T-shell is run as a root shell, a ">" if not.
%{..%} Include string as a literal escape sequence. Note that the enclosed escape sequence should only be used to change terminal attributes and should not move the cursor location. Also, this cannot be the last character in the prompt string. (Available only if T-shell was compiled to be 8-bit clean.)
%? Return code of the last command executed just before the prompt.
%R In prompt3, this is the corrected string; in prompt2, it is the status of the parser.

The sequences for standout are often used to indicate that this is an enabled (running as root) shell. An example:

> set prompt="%m [%h] %U[%@]%u [%/] you rang? "
tut [37] [2:54pm] [/usr/accts/sys] you rang?

In addition, there is a variable, prompt2, which is used to prompt for the body of while and for loops (wherever normal, C-shell prompts with a question mark). The default for prompt2 is %R?--the status of the parser followed by a question mark. This alternate prompt is also used when the parser is waiting for more input; i.e., when the previous line ended in a \. The prompt3 variable is used when displaying the corrected command line when automatic spelling correction is in effect; it defaults to CORRECT>%R (y|n|e)?.

T-shell Timed

Event List

T-shell supports a scheduled-event list through the use of the command sched. This command gives the user a mechanism by which to arrange for other commands to be executed at given times. An event is added to the scheduled-event list by saying

> sched [+]hh:mm <command>

as in

> sched 11:00 echo It\'s eleven o\'clock.

This will make an entry in the list at 11:00 am for the echo command to be run with the given arguments. The time may be specified in either absolute or relative time, and absolute times may have a morning/afternoon specification as well using "am" or "pm."

For example,

     > sched +2:15 /usr/lib/uucp/uucico -r1 -sother

     > sched 5pm set prompt='[%h] It\'s after 5; go home: >'

     > sched +3am echo This syntax doesn\'t work.

     Relative time inconsistent with am/pm.

     >

NOTE: T-shell will complain, if you try to make faulty time specifications.

Printing the current time-event list is accomplished by giving the sched command with no arguments:

     > sched

        1 Wed Apr 4 15:42 /usr/lib/uucp/uucico -r1 -sother

        2 Wed Apr 4 17:00 set prompt=[%h] It's after 5; go home: >

     >

There is also a mechanism by which the user can remove an item from the list:

     > sched --3

     Usage for delete: sched -<item#>.

     > sched -3

     Not that many scheduled events.

     > sched -2

     > sched

        1 Wed Apr 4 15:42 /usr/lib/uucp/uucico -r1 -sother

     >

All commands specified on the scheduled-event list will be executed just prior to printing the first prompt immediately following the time when the command is to be run. Hence, it is possible to miss the exact time when the command is to be run, but T-shell will definitely get around to all commands that are overdue at its next prompt. Scheduled-event list items that come due while T-shell is waiting for user input will be executed immediately. In no case, however, will normal operation of already-running commands be interrupted so that a scheduled-event list element may be run.

This mechanism is similar to, but not the same as, the at command on some UNIX systems. Its major disadvantage is that it does not necessarily run a command at exactly the specified time (but only if another command is already being run). Its major advantage is that commands that run directly from T-shell, as sched commands are, have access to shell variables and other structures. This provides a mechanism for changing one's working environment based on the time of day.

line.

Site Search Options

[© 2/2001 Documentation Home] [Laws and Policies] [TTU Home Page]
Updated July 29, 2003