UNIX Unleashed, System Administrator's Edition
- 5 -
General Commands
by Sanjiv Guha
UNIX has commands that are native to UNIX, and commands that you (or someone at
your installation) can write. Depending on the UNIX version you are running and the
shell you are in, the commands differ. In this chapter, commands that are available
in most UNIX versions are discussed. Each command can have several arguments and
several flags associated with it. The general form of a command is
command [flags] [argument1] [argument2] ...
Flags are preceded by a hyphen. Several flags can be specified together with only
one hyphen. For example, the following two commands are equivalent:
ls -a -l
ls -al
Depending on the command, the arguments can be optional or mandatory. All commands
accept inputs from the standard input, display output on standard output and display
error message on standard error. You can use UNIX redirection capabilities to redirect
one or more of these. Standard input is where UNIX gets the input for a command,
standard output is where UNIX displays output from a command, and standard error
is where UNIX displays any errors as a result of the execution of a command.
All commands, when executed successfully, return a zero return code. However,
if the commands are unsuccessful or partially successful, they return non-zero return
codes. The return codes can be used as part of control logic in shell scripts.
CAUTION: Though the commands described here work
on most of the UNIX systems in a general way, the behavior of the flags associated
with each command can differ. You should use the man command on your system
to learn about details of the command and its flags.
User Related Commands
The commands related to logging in and out of the system are discussed in the
following sections. User Related Commands are those that are used to log in and out
of your UNIX system. They might differ slightly from system to system, but the commands
discussed here are common to most systems.
login
Whenyou start working on a UNIX system, the first thing you need is a login ID.
Each user in a UNIX system has a unique login ID that identifies the user and any
characteristics associated with the user. When you first connect to a UNIX system,
you get the login prompt. The login prompt usually asks for the following information:
login:
password:
You will be allowed into a system, if, and only if, you enter the login ID and
password correctly. When the login is successful, you get information, such as last
unsuccessful login, last successful login, whether you have any mail, messages from
the system administrator, and more. Following is an example of a successful login:
*******************************************************************************
* *
* You are now logged on to the host1 computer *
* *
*******************************************************************************
Last unsuccessful login: Thu Nov 7 22:32:41 1996 on tty1
Last login: Fri Nov 8 01:17:04 1996 on tty1
/u/testuser:>>
While logging in, there are shell scripts that are executed. The script that gets
executed depends on the shell you are running. For example, in Bourne shell, the
.profile file in you home directory is execute, in Korn shell, the file
pointed to by the environment variable ENV is executed.
You should put in as part of the startup file all the commands that you want to
be executed before login. For example, you can set the search path, the terminal
type, and various environment variables or run special programs, depending on your
requirements. The following is an example of a Korn shell .profile file
for user testuser. Here the PATH variable is being set to include
directories that user testuser uses for daily work, and the mail is being
checked.
PATH=$PATH:/u/testuser:/u/testuser/exe:/u/testuser/script
PATH=/usr2/cdirect/local:$PATH
export PATH
You can invoke the login command from the command line also. The user
is optional. If user is not provided, the system prompts for login id
followed by the password prompt, if needed. If user is provided, the system
prompts for a password, if needed.
rlogin
UNIX provides you with the rlogin command to let you move between various
computers in a network. The rlogin command is similar to the telnet
command described in this section.
To be allowed access to a remote host, you must satisfy the following conditions:
- The local host is included in the /etc/hosts.equiv file of the remote
host, the local user is not the root user and the -l User
option is not specified.
- The local host and user name are included in the $HOME/.rhosts file
in the remote user account.
If the user on the remote host is set up without a password, rlogin will
allow you to log in without using any password, provided the above defined conditions
are satisfied. However, it is not advisable to set up users without passwords. When
you exit from the remote host, you are back on the local host.
Examples Here is an example of the rlogin command where the user
ID has not been specified. Executing the following command will prompt you for a
password (if one is required) and entering the correct password will let you into
the remote host:
rlogin box2
In this case, the user ID for the remote host is assumed to be the same as that
of the logged in user ID in the local host. For example, if testuser is
logged in currently on box1, UNIX will assume that you are trying rlogin
for testuser on box2.
If, however, your user ID is different on box2 than on box1,
you can use another option of the rlogin command:
rlogin box2 -l testusernew
In this option, you tell rlogin that the user ID to be used for the remote
host is testusernew.
telnet
If you are in an environment where you work with multiple UNIX computers networked
together, you will need to work on different machines from time to time. The telnet
command provides you with a facility to login to other computers from your current
system without logging out of your current environment. The telnet command
is similar to the rlogin command described earlier in this section.
The hostname argument of telnet is optional. If you do not use the host
computer name as part of the command, you will be placed at the telnet prompt,
usually, telnet>. There are a number of sub-commands available to you
when you are at the telnet> prompt. Some of these sub-commands are as
follows:
- exit to close the current connection and return to the telnet>
prompt if sub-command open was used to connect to the remote host. If, however,
telnet was issued with the host-name argument, the connection is
closed and you are returned to where you invoked the telnet command.
- display to display operating arguments.
- open to open a connection to a host. The argument can be a host computer
name or address. telnet will respond with an error message if you provide
an incorrect name or address.
- quit to exit telnet.
- set to set operating arguments.
- status to print status information.
- toggle to toggle operating arguments (toggle ? for more).
- ? to print help information.
Examples Assume that you work with two networked computers, box1
and box2. If you are currently logged in on box1, you can execute
the following command to login into box2:
telnet box2
As a response to this command, box2 will respond with the login screen
where you can enter your userid and password for box2 to login.
After completing your work on box2, you can come back to box1.
passwd
As you have seen, every time you try to log in to a computer system, you are asked
for your user ID and password. Although users can be set up without passwords, most
users will have a password, which they will must use when logging in to a computer
system.
When you first get you user ID set up in a computer, the system or security administrator
will assign you a temporary password using the root user ID. The first time
you try to log in, the system will ask you to change your password; you will use
the new password for all subsequent logins.
However, you can change you password if, for example, you think that somebody
else has come to know it. As a security precaution, it is a good practice to modify
your password frequently. You can use the passwd command to change your
password.
When you issue the passwd command, you are first prompted for your current
password, followed by two prompts to enter your new password. The new password must
match on both entries. This is a precaution to ensure that you do not type in something
you did not intend and are subsequently unable to login. Your new password cannot
be the same as you current password. When you are typing your password (both old
and new), UNIX does not display them.
Whenever you change your password, the new password must follow the rules set
up at your installation. Some of the rules that govern passwords are as follows:
- The minimum number of alphabetic characters.
- The maximum number of times a single character can be used in a password.
- The minimum number of weeks that must elapse before a password can be changed.
- The maximum number of weeks after which the password must be changed. The system
will prompt you to modify the password when this happens.
Some of the UNIX system have additional flags for the passwd command.
You should be careful about choosing passwords. If you choose passwords that other
people can make an educated guess about, your login will not be secure. Following
are some guidelines for choosing passwords:
- Avoid using proper nouns like name of spouse, children, city, place of work,
and so on.
- Avoid using strings of characters followed by numbers, like xyz01.
- Use both uppercase and lowercase letters mixed with numbers.
- The length should be at least 7 characters.
- You should never write down your password.
- You should be able to type it quickly.
Examples In Korn shell, you can execute the following command to get finger
related information:
passwd -f
may result in the following display
testuser's current gecos:
"Sanjiv Guha, UNIX Programmer, X 9999"
Change (yes) or (no)? >
Exit
When you log in to a UNIX system, you are always placed in a shell. The shell
may be the Bourne shell, C shell, Korn shell, or other. As we have seen, we can log
in to other system with rlogin or telnet commands. The exit
command allows you to exit the current shell.
You can also exit your current shell by typing CTRL-d. (Hold down the Ctrl key
and the d key together.) If you are on the command line and press Ctrl-d, you will
be logged off.
Locating Commands
When you try to execute a command, UNIX has to locate the command before it can
execute it. UNIX uses the concept of search path to locate the commands you
are trying to execute. The search path is a list of directories in the order to be
searched for locating commands.
The default search paths set by the installation usually have the standard directories
like /bin, /usr/bin, and other installation specific directories.
You can modify the search path for your environment as follows:
- Modify the PATH statement in the .profile file (Korn shell
and Bourne shell).
- Modify the set path=(....) in the .cshrc or .login
file (C shell)
Add the directory that contains your commands, or any commands you have modified,
to the beginning of the path. They will be found first and executed first. Make sure
you do not give your modified command a name that is the same as a UNIX native command.
which
This command can be used to find whether a particular command exists in you search
path. If it does exist, which tells you which directory contains that command.
Examples To find out where the which command resides, you can
execute the following command:
which which
The system responds with the following message, meaning that the command which
exists in the directory /usr/bin.
/usr/bin/which
whence
The whence command is a more verbose form of the which command
in Korn shell. It has a flag -v associated with it to produce output in
a verbose form. For a native command that has not been aliased, it generates output
similar to which.
Examples To get information about the which command, you can execute
the following command:
whence which
/usr/bin/which
If you use the -v flag, you get the verbose for
which is /usr/bin/which
However, if you have aliased commands, then the output is different. For example,
if you have aliased rm as rm -i (remove command with confirmation
flag set), the command
which rm
generates the output
rm -i
while with the flag -v, it generates the output
rm is an alias for rm -i
where
The where command is used to obtain the full pathname of one or more
files or directories. There are no flags associated with the where command.
When the pathname is displayed, it is prefixed by the hostname, and if there are
no arguments, the full pathname of the current directory is displayed.
Examples If you want to find the path name of the current directory, assuming
that you are in the directory /u/testuser, the following command:
where
will result in
box1:/u/testuser
Determining Command Usage
UNIX provides you with online help to learn about various commands and their options
and flags. You may be familiar with the most often used commands and their various
options, but to find out about less popular commands or command usage, you can use
the online help provided by UNIX.
man
The man command is used to display the online UNIX manual pages, which
include commands, files, sub-routines, and so on. You have to provide the man
command with the name of the object you are looking for. If you do not know the full
name, you can use the UNIX wildcard to specify the object name. You can even find
out more about the man command itself by using the man command.
The following are some of the flags and arguments that can be used for the man
command:
- -k keyword for a list of summary information of manual sections in the
keyword database for the specified keyword.
- -f command for details associated with the command. The root user must
set up the file /usr/man/whatis before you can use this option.
- -M path to specify the search path for the man command.
You can specify a section name as part of the command. This will allow you to
search for a title in the specified section. The following is a list of sections
that can be specified:
- 1--Commands
- 2--System calls
- 3--Subroutines
- 4--File formats
- 5--Miscellaneous
- 7--Special files
- 8--Maintenance
Examples If you want to find out about the find command, execute
the following command:
man find
To find out about filesystem related keywords, execute the following command:
man -k filesystem
Administration
There are some functions that only the UNIX system administrator at you installation
can perform. These include starting the system, shutting down the system, setting
up new user accounts, monitoring and maintaining various filesystems, installing
new software on the system, and more. In this section we will discuss the commands
necessary to fulfill these duties.
install
The install command is used to install new versions of current software
programs or brand new software programs. The basic function of the install
command is to copy the binary executable and any associated files to the appropriate
directories. In the case of new versions of existing software, the files are copied
over the existing files and retain their permissions. The install command
also prints a log of messages of what it has done. The system administrator can then
modify the permissions and ownership defined for that installation.
NOTE: For new software, the default permissions
are set to 755 with owner and group both set to bin. The default
directories searched to find whether the command is present are /usr/bin,
/etc, and /usr/lib in that order.
The following are some of the flags that can be used for the install
command:
- -c directory to install the files in the specified directory, if, and
only if, the command does not already exist. If it exists, the operation is aborted.
- -f directory to install the files in the specified directory, even if
the command already exists in that directory. If the file is new, the default owner,
group, and permissions are applied.
- -G group to set the group of installed files to the specified group
instead of the default group bin.
- -O owner to set the ownership of the installed files to the specified
user instead of the default owner bin.
- -o to save the current version with the prefix OLD;
that is, if the name of the file is sample, it is saved as OLDsample.
- -i if you do not want to search the default directories but want to
search the command line specified directories.
- -n directory if you want to install the files in the specified directory
if they are not found in the search directories.
Examples You can execute the following command to install the file sample_program
in the directory /usr/bin (default directory):
install sample_program
Assuming that sample_program already exist in the /u/testuser/exe
directory, you can install the new version by executing the following command:
install -f /u/testuser/exe sample_program
To save a copy of the old version of the sample_program as OLDsample_program
and install a new version in the directory /u/testuser/exe, you can execute
the following command:
install -f /u/testuser/exe -o sample_program
shutdown
The system administrator may need to shutdown the system at the end of the day
or to do hardware maintenance on the UNIX machine. To shutdown the machine in an
orderly fashion, use the shutdown command. This command can be executed
only with the root user authority. All users get a shutdown message when
this command is executed, and they receive a final completion message. This process
is not complete until all users receive the message. By default, this command brings
down the system to a single user mode from a multi-user mode.
The following is a list of some of the flags that can be used with the shutdown
command:
- -h halts the operating system completely.
- -i for interactive messages are displayed to guide the user through
the shutdown.
- -k to avoid shutting down the system. This only simulates a system shutdown.
- -m brings the system down to maintenance (single user) mode so that
the system administrator or hardware engineer can do system maintenance, which can
include installing new hardware or software or scheduled maintenance of existing
hardware or software.
It is possible also to specify the time when restart is to be done by specifying
a future date or relative time. In that case, the system sends messages to the users
periodically about the impending shutdown.
ulimit
The ulimit command is available in Korn shell and Bourne shell, and can
be used to set limits on certain resources for each process. The corresponding command
in C shell is limit. There are two types of limits:
- Hard limits are those defined on the resources on a system-wide basis and which
can be modified only by root authority.
- Soft limits are the default limits applied to a newly created process. Soft limits
can be increased to the system-wide hard limit.
Following are the flags that can be used with the ulimit command:
- -a to show the soft limits
- -Ha to show the hard limits
- -c size to set the coredumpsize in blocks
- -t size to set the CPU time in seconds
- -f size to set the maximum file size in blocks
- -d size to set the maximum size of the data block in kilobytes
- -s size to set the maximum size of the stack in kilobytes
- -m size to set the maximum size of the memory in kilobytes
Examples To obtain the current setting of the hard limits, execute the
following command:
ulimit -Ha
time(seconds) unlimited
file(blocks) 4097151
data(kbytes) unlimited
stack(kbytes) unlimited
memory(kbytes) unlimited
coredump(blocks) unlimited
To obtain the current setting of the soft limits, execute the following command:
ulimit -a
time(seconds) unlimited
file(blocks) 4097151
data(kbytes) 2048576
stack(kbytes) 82768
memory(kbytes) 909600
coredump(blocks) 102400
You should keep in mind that the soft limits set by the system administrator can
be modified by you. For example, if you want to change the above setting of CPU time
limit to only 60 seconds for a process, you can execute the following command:
ulimit -t 60
You should also be aware that these limits are imposed on a per process basis.
Usually, most of the jobs have multiple processes; therefore, this limit does not
impose real limits on the jobs.
umask
The umask command is used by the system administrator to set the default
value to be assigned to each file created by a user. You, as a user, can modify this
default setting.
Three groups of permissions are associated with a file or directory--owner, group,
and world (sometimes referred to as others). The permissions for these three groups
are assigned using octal numbers--one octal number for each group. The values for
each group depend on the following three bits:
- read bit (0 or 1)
- write bit (0 or 1)
- execute bit (0 or 1)
Using binary arithmetic, the value can vary from 0 (all bits having a value of
zero) to 7 (all bits having a value of 1).
You should be careful while interpreting the value associated with umask.
The value associated with umask can be used to derive the value of the default
permission by subtracting it from 777. That is, if the value of umask is
022, then the value of the permission is 777 - 022 = 755 (read, write, execute for
owner; read, execute for group; and read, execute for the world).
Examples To obtain the default value of the umask, execute the
following command:
umask
To set the value of the permission to, say, 751, you should set umask
to the value 026 (777 - 751). To achieve this, execute the following command:
umask 026
Process Related Commands
In UNIX, a process is a program that has its own address space. Usually, a command
or a script that you can execute consists of one or more processes. Simple commands
like umask have only one process associated with them, while a string of
commands connected by pipes has multiple processes associated.
The processes can be categorized into the following broad groups:
- Interactive processes, which are those executed at the terminal. The interactive
processes can execute either in foreground or in background. In a foreground process,
the input is accepted from standard input, output is displayed to standard output,
and error messages to standard error. While executing a process in the background,
the terminal is detached from the process so that it can be used for executing other
commands. It is possible to move a process from foreground to background and vice
versa.
- Batch processes are not submitted from terminals. They are submitted to job queues
to be executed sequentially.
- Deamons are never-ending processes that wait to service requests from other processes.
In UNIX, each process has a number of attributes associated with it. The following
is a list of some of these attributes:
- Process ID is a unique identifier assigned to each process by UNIX. You can identify
a process during its life cycle by using process ID.
- Real User ID is the user ID of the user who initiated the process.
- Effective User ID is the user ID associated with each process. It determines
the process's access to system resources. Under normal circumstances, the Real User
ID and Effective User ID are one and the same. But, it is possible for the effective
user ID to be different from the real user ID by setting the Set User ID
flag on the executable program. This can be done if you want a program to be executed
with special privilege without actually granting the user special privilege.
- Real Group ID is the group ID of the user who initiated the process.
- Effective Group ID is the group ID that determines the access rights. The effective
group ID is similar to the effective user ID.
- Priority (Nice Number) is the priority associated with a process relative to
the other processes executing in the system.
kill
The kill command is used to send signals to an executing process. The
process must be a nonforeground process for you to be able to send a signal to it
using this command.
The default action of the command is to terminate the process by sending it a
signal. It is possible, however, that the process may have been programmed for receiving
such a signal. In such a case, the process will process the signal as programmed.
You can kill only the processes initiated by you. However, the root user
can kill any process in the system.
The flags associated with the kill commands are as follows:
- -l to obtain a list of all the signal numbers and their names that are
supported by the system.
- -signal number is the signal number to be sent to the process.
You can also use a signal name in place of the number. The strongest signal you can
send to a process is 9 or kill.
The argument to kill command is the Process ID (PID). You can specify
more than one PID as arguments to kill more than one process. The value of the PID
can be one of the following:
- PID greater than zero to kill specified processes.
- PID equal to zero to kill all processes whose process group ID is the
same as the process group ID of the user initiating the kill command.
- PID equal to -1 to kill all processes owned by the effective user ID of the user
initiating the kill command.
- PID equal to a negative number but not -1 to kill processes whose PID is equal
to the absolute value of the number specified.
Examples If you are running a command in the background and think it has
gone into a loop, you will want to terminate it. If the process number is, for example,
2060, execute the following command:
kill 2060
If, for some reason, this does not kill it, use the stronger version
kill -kill 2060 (same as kill -9 2060)
If you want to kill all the processes owned by you, execute the following command:
kill -kill 0
You should be aware, however, that this command also logs you off, because specifying
0 means that all processes, including your current login, will be killed.
nice
As you have seen, a relative priority is associated with each process. The relative
priority governs the resources allocated to it by the operating system. The nice
command lets you modify the priority of one or more processes so that you can assign
them higher or lower priority. You can increase the priority only if you have root
privileges though.
A negative number signifies a higher priority than a positive number. The value
is usually in the range of -20 to 20.
If you do not specify an increment, the nice command takes a value equal to or
higher than the current process, provided you have appropriate privileges. If you
do not have sufficient privileges, the priority is not affected at all.
Usually, you should use the nice command to lower the priority of background
or batch processes for which you do not need fast turn around.
Examples If you want to find, at a lower priority in background, all the
C source files in the current directory or its sub-directories, execute the following
command:
nice find . -name *.c -print &
This will set the process to a default nice priority, which may be 10.
To run the process at an even lower priority, execute the following command:
nice 16 find . -name *.c -print &
The following command can also be executed to achieve the same result:
nice -n 16 find . -name *.c -print &
ps
The ps command is used to find out which processes are currently running.
Depending on the options, you can find all processes or only those initiated by you
user ID. This command provides you with details about the various background and
batch processes running in the system. It can provide information only about the
active processes.
When the ps command is executed without any flags or arguments, it lists
all processes (if any) initiated from the current terminal.
Following is a list of some of the flags that determine what processes are listed
by the ps command:
- -A to list details of all processes running in the system.
- -e to list details of all processes, except kernel processes.
- -k to list all the UNIX kernel processes.
- -p list to list details of all processes specified in the list.
- -t list to list details of all processes initiated from the terminals
specified in the list.
- -U list (-u list) to list details of all processes initiated
by the users specified in the list.
- a to list details of all processes that have terminals associated wit
them.
- g to list details of all processes.
- x to list details of all processes that do not have any terminal associated
with them.
The following is a list of some of the flags for the ps command that
determine which details are displayed for each process listed:
- -l to generate the listing in a long form
- -f to generate a full listing
- -F format to generate a formatted output
NOTE: Some of the flags used in the ps
command are not preceded by a hyphen (-).
The following details are displayed if formatting flags are not used:
- Process ID of the process.
- Terminal ID associated with the process. Hyphen (-), if no terminals are associated.
- CPU time consumed by the process.
- Command being executed as part of the process.
By using the formatting command, some of the details that can be obtained are
as follows:
- USER ID of the user who initiated the process.
- Process ID of the process.
- Parent Process ID of the process.
- CPU utilization of the process.
- Start time of the process. If the process has been started on the same day, it
shows the time; otherwise, it shows only the date.
- Terminal ID associated with the process. It display a hyphen (-) if there are
no terminals associated with the process.
- CPU time consumed by the process.
- Commands being processed as part of the process.
Examples To display the processes initiated by the current user at the
current terminal, execute the following command:
ps
The result is displayed as follows:
PID TTY TIME CMD
66874 2 0:00 -ksh
71438 2 0:00 ps
The two processes displayed here are the login shell running (Korn shell) and
the ps command itself.
If you want more details, execute the following command:
ps -f
This generates the following display:
USER PID PPID C STIME TTY TIME CMD
testuser 66874 1 1 22:52:26 2 0:00 -ksh
testuser 480076 66874 6 00:21:33 2 0:00 ps -f
If you want to know all the processes executing at terminal tty2, execute
the following command:
ps -f -t tty2
The result is
USER PID PPID C STIME TTY TIME CMD
testuser 66874 1 1 22:52:26 2 0:00 -ksh
testuser 703277 66874 6 00:24:17 2 0:00 ps -f -t tty2
If there are no terminals associated with a process, a hyphen (-) is displayed.
Therefore, you can use a hyphen as a terminal name to get a list of processes that
are not associated with any terminals. For example, you can execute the following
command:
ps -t -
To find out all the processes being executed by your friend with the user ID friend,
execute the following command:
ps -f -ufriend
jobs
In UNIX, there is a subtle difference between processes and jobs. A job
typically is one command line of commands, which can a single command, a shell script,
or a chain of piped commands. In a chain of piped commands, each command has a unique
process ID, but all have the same job ID.
The C shell and some versions of Korn and Bourne shell offer the jobs
command. You can use the jobs command to find out the details about active
jobs. Once you have the job ID, you can start using it to do primitive job controls.
You can use % (percent sign) in front of the job number to indicate that the number
is a job number rather than a process ID.
Examples If you want to bring job number 5 from background to foreground,
execute the following command:
fg %5
If you had used 5 instead of %5, UNIX would have interpreted
it as the process ID 5.
If you have one job called sample_job running in the background, execute
the following command:
jobs
to get the details of the job. The display looks like:
[1] + Running nohup sample_job > sample_log &
If you use the -l option, the process number will also be displayed as
follows:
[1] + 270384 Running nohup sample_job > sample_log &
You need not know the job number to take any action on it. You can use the name
of the job preceded by a % (percent sign) to identify the job. You can use
regular UNIX wildcards to achieve this.
In the preceding example, you can use the name sample_job to identify
the job. UNIX also allows you to use %\? to identify the unique part of
the job name. In this example, it may be %\?job (Please note that the character
\ has been used as ? has special meaning in UNIX. Specifying a
\ in front of a character tells UNIX not to interpret the character as a
control character.) To kill the job sample_job, use the following command:
kill %sample_job
or, you cause the following, if sample_job is the only one you are currently
executing. Otherwise, it will kill all jobs with job name ending in job:
kill %\?job
wait
You can use the wait command to wait for completion of jobs. This command
takes one or more process IDs as arguments. This is useful while doing shell programming
when you want a process to be finished before the next process is invoked. If you
do not specify a process ID, UNIX will find out all the processes running for the
current environment and wait for termination of all of them.
Examples If you want to find out whether all the processes you have started
have completed, execute the following command:
wait
If you want to find out whether the process ID 15060 has completed, execute
the following command:
wait 15060
The return code from the wait command is zero if you invoked the wait
command without any arguments. If you invoked the wait command with multiple
process IDs, the return code depends on the return code from the last process ID
specified.
nohup
When you are executing processes under UNIX, they can be running in foreground
or background. In a foreground process, you are waiting at the terminal for the process
to finish. Under such circumstances, you cannot use the terminal until the process
is finished. You can put the foreground process into background as follows:
Ctrl-z
bg
The processes in UNIX will be terminated when you logout of the system or exit
the current shell whether they are running in foreground or background. The only
way to ensure that the process currently running is not terminated when you exit
is to use the nohup command.
The nohup command has default redirection for the standard output. It
redirects the messages to a file called nohup.out under the directory from
which the command was executed. That is, if you want to execute a script called sample_script
in background from the current directory, use the following command:
nohup sample_script &
The & (ampersand) tells UNIX to execute the command in background.
If you omit the &, the command is executed in foreground. In this case,
all the messages will be redirected to nohup.out under the current directory.
If the nohup.out file already exists, the output will be appended to it.
If the permissions of the nohup.out file is set up so that you cannot write to that
file, UNIX will create or append to the nohup.out file in your home directory.
There may be instances where neither of these files can be accessed. For example,
you may have run out of disk space. In such circumstances, UNIX will not initiate
the nohup command.
The nohup command allows you to redirect the output into any file you
want instead of the default file nohup.out by using standard UNIX redirection
capabilities.
When you initiate the nohup command in the background (by using &
at the end of the command line), UNIX displays the process ID associated with it,
which can later be used with commands like ps to find out the status of
the execution.
Examples If you want to find the string sample_string in all the
files in the current directory, and you know that the command will execute for quite
a while, execute the following command:
nohup grep sample_string * &
UNIX responds with the following message:
[2] 160788
Sending output to nohup.out
The first line of the display is the process ID, and the second line is the informational
message about the output being directed to the default nohup.out file. You
can later go into the file nohup.out to find out the result of the grep
command.
In the preceding example, if you would like to redirect the output to a file called
mygrep.out, execute the following command:
nohup grep sample_string * > mygrep.out &
In this case, UNIX displays only the process ID as follows:
[2] 160788
You can also execute shell scripts, which can contain multiple commands, using
the nohup command. For example, if you want to execute a script called my_script
in the current directory and redirect the output to a file called my_script.out,
execute the following command:
nohup my_script > my_script.out &
CAUTION:: You should be careful
not to redirect the output of a script you want to execute to the script itself.
sleep
If you want to wait for a certain period of time between execution of commands,
use the sleep command. This can be used in cases where you want to check
for, say, the presence of a file, every 15 minutes. The argument is specified in
seconds.
Examples If you want to wait for 5 minutes between commands, use:
sleep 300
Here is part of a small shell script that reminds you twice to go home, with a
5-minute wait between reminders. (The script is incomplete.)
echo "Time to go home"
sleep 300
echo "Final call to go home ....."
Communication
UNIX has several commands that are used to communicate with host computers for
purposes of connecting to another host, transferring files between host computers,
and more. You may need these commands, for example, to transfer a file from another
host to the local host you are working on (provided you have sufficient access to
do so). These commands are different from the commands used to log in to other computers
because these allow you to communicate with other hosts without logging in.
cu
The cu command allows you to connect to another host computer, either
directly or indirectly. That is, if you are currently on host1 and you use
cu to connect to host2, you can connect to host3 from
host2, so that you are connected directly to host2 and indirectly
to host3.
Following is a list of some of the flags that can be used with cu command:
- -d to print diagnostic messages.
- -lLine to specify the device to be used for communication. By default,
UNIX uses the first available appropriate device. You can use this flag to override
the default.
- -n to confirm the telephone number. If this flag is used, cu
prompts you for the telephone number rather than accepting it as part of the command
line.
- -sSpeed to specify the speed of the line to be used for communication
between hosts. The line speed is specified is in bauds and can be 300, 1200, 2400,
and so on. UNIX in most cases should be able to default to the right line speed.
- -Tseconds to specify the time-out parameter. This is the time up to
which UNIX will try to connect to the remote host.
The following arguments can be specified with the cu command:
- System Name is the name of the system to which you want to connect.
This name must be defined in the /etc/uucp/Systems file. Also defined in
this file are parameters, such as telephone number, line speed, and more, which are
used to connect to the remote host. If you are using System Name, you should
not use the -l and -s option.
- Telephone Number is the telephone number to be used to connect to the
remote host. The number can be a local or long-distance.
After making the connection, cu runs as two processes: The transmit process
reads data from the standard input and, except for lines beginning with ~
(tilde), passes the data to the remote system; the receive process accepts data from
the remote system and, except for lines beginning with ~ (tilde), passes
it to the standard output.
Once you are able to successfully log in to the remote host, you will be able
to use several sub-commands provided by cu. These sub-commands allow you
to operate on files, directories, and so on that are on the remote host.
The following is a list of some of these sub-commands (You should prefix the ~
with a \ so that UNIX does not apply special meaning to ~):
- ~. to disconnect from the remote host.
- ~! to disconnect from the remote host.
- to activate an interactive shell on the local host. You can toggle between interactive
shells of local and remote hosts using ~! and Ctrl-d. That is, ~! puts you into a
shell at the local host, while Ctrl-d returns you to the remote host.
- ~!Command to execute Command at the local host
- ~%cd directory to change the directory on the local host to the
specified directory.
- ~%put From [ To ] to copy a file on the local system to a file
on the remote system. If the To variable is not specified, the local
file is copied to the remote system under the same filename. The progress of the
file transfer is displayed as consecutive single digits for each block transferred
between the hosts. Only files containing text (ASCII)files can be transferred using
this subcommand
- ~%take From [ To ] to copy a file from the remote system to a
file on the local system. If the To variable is not specified, the remote
file is copied to the local system under the same filename. The progress of the file
transfer is displayed as consecutive single digits for each block transferred between
the hosts. Only files containing text (ASCII)files can be transferred using this
subcommand.
- ~$Command to run, on the local system, the command denoted by
the Command variable, and send the command's output to the remote
system for execution.
Examples If you know the remote host name and the name is defined in the
/etc/uucp/Systems file, you can use it to cu to the remote host.
For example, if the remote host name is remote2, you can execute the following
command:
cu remote2
If you want to connect to a specific device, tty1, on a remote host at
a specified line speed, 2400, execute the following command:
cu -s 2400 -l tty1
Once you are in the remote host, you can execute any of the cu sub-commands.
If you want to change to the directory /u/testuser on the local host,
execute:
\~%cd /u/testuser
If you have a file called local_file from directory /u/testuser
on the local host, to copy it to the current directory in the remote host with the
same name, execute the following command:
\~%put /u/testuser/local_file
If you want to modify the name of the file to local_file in the directory
/u/testuser while copying it to remote_file under the /u/testuser/testdir
on the remote host, execute the following command:
\~%put /u/testuser/local_file /u/testuser/testdir/remote_file
You can do the opposite using the take sub-command. For example, if you
want to copy a file /u/testuser/remote_file from the remote host to a file
called /u/testuser/local_file on the local host, execute the following command:
\~%take /u/testuser/remote_file /u/testuser/local_file
To execute the ls command on the local system, execute the following
command:
\~!ls
ftp
You can use the ftp command to transfer files between two host computers.
This command allows you to move files from a remote host to a local host, or to move
files from the local host to a remote host. The ftp command allows only
simple forms of file transfer. That is, you will not be able to copy files from directories
recursively using a single ftp command. Instead you must transfer each file
or directory individually. You should also be aware that ftp can be used
between different types of systems, which means that system-dependent file attributes
might not be preserved when files are transferred.
While using the ftp command, you need to use a login and password to
login to the remote host. However, it is possible to login to the remote host without
a password if the home directory has a .netrc file and that file contains
the macro necessary for login to the remote host.
If a login ID and password are needed, the ftp command prompts for that
information. In such a case, you will not be able to proceed until you are able to
successfully login using the correct user ID and password.
Once you are able to login successfully, the ftp command displays an
ftp> prompt. In the ftp> prompt, you can use a number of
sub-commands to perform file transfers.
You can specify the hostname as part of the ftp command or open a connection
to a host in the ftp> prompt. If you specify the hostname as part of
the command line, you are prompted for login and password before you are put into
the ftp> prompt. For example, if you execute the following command:
ftp box2
you will be prompted for user ID and password.
Some of the flags that can be used with the ftp command are as follows:
- -i if you do not want to be prompted with filenames in case more than
one file is being transferred using a single command.
- -d for debug information.
- -n to prevent automatic login in case a .netrc file is present.
- -v to display the messages from the remote host on the terminal.
Once you are at the ftp> prompt, you can use several sub-commands.
These sub-commands are
- ! to invoke the interactive shell on the local host. Optionally, you
can invoke a command with arguments by specifying the command and arguments after
the ! sign.
- ? to display a list of sub-commands available. Optionally, a sub-command
can be specified after the ? to get information specific to that sub-command.
- type to set the file transfer mode. Valid modes are: ASCII
for text files, binary for files which may contain special files so that
the data is transferred without any translation, and EBCDIC to transfer
files in EBCDIC code.
- cd to change to the home directory on the remote host. If a directory
is specified, the current directory is changed to the specified directory on the
remote host.
- pwd to print the current working directory on the remote host.
- ls to print a list of the files on the remote host. If no directory
is specified, files and directories in the specified directory are printed. You can
direct the list of files to a file on the local host by specifying a local filename.
- mkdir directory to make a new directory under the current directory
on the remote host.
- dir to generate a list of files. Similar to the ls command
but produces a detailed list.
- rmdir directory to remove the specified directory on the remote
host, provided the directory is empty.
- rename oldname newname to rename a file from oldname
to newname on the remote host.
- delete filename to delete the specified file on the remote host.
- get filename to transfer a file from the remote host to the local
host. The name of the file is not altered. Optionally, you can specify a local
filename to which the file from the remote host will be copied.
- mget to transfer multiple files from the remote host to the local host.
You can specify a filename using a wildcard, which is expanded by UNIX. If the prompt
option is set, you will be prompted for each filename for confirmation. If a prompt
option is not set, all files are copied without any confirmation. .
- put filename to transfer a file from the local host to the remote
host. The name of the file is not altered. Optionally, you can specify a local
filename to which the file from the remote host will be copied.
- mput to transfer multiple files from the local host to the remote host.
You can specify a filename using a wildcard, which is expanded by UNIX. If the prompt
option is set, you will be prompted for each filename for confirmation. If the prompt
option is not set, all files are copied without confirmation.
- mdelete to delete multiple files by specifying wildcard filenames.
- append filename to append a local file to the end of a file on the remote
host. Optionally, you can specify a filename on the remote host to append at the
end of the specified file. If the remote filename is not specified, the local filename
is used by default.
- open to open a connection to a remote host by specifying a remote host
name.
- close to close the existing connection to the remote host.
- bye or quit to quit the ftp session.
- lcd to change to the home directory on the local host. If a directory
is specified, the current directory is changed to the specified directory on the
local host.
Examples To open an ftp session on a remote host named otherhost,
execute the following command:
ftp otherhost
or
ftp
ftp> open otherhost
In both cases, you will be prompted with user ID and password prompts, if you
are not set up in the .netrc file with the user ID and password. The prompts
and messages will appear as follows:
Connected to otherhost.
220 otherhost FTP server (Version 4.14 Fri Oct 10 13:39:22 CDT 1994) ready.
Name (otherhost:testuser): testuser
331 Password required for testuser.
Password:
230 User testuser logged in.
Once you are at the ftp> prompt, you can execute the ftp sub-commands.
If you want to find out which directory you are in on the remote host, execute the
following sub-command:
ftp> pwd
257 "/home/testuser" is current directory.
To copy a file called testfile from otherhost to the local host,
execute the get sub-command. Following is the sub-command and its response:
ftp> get testfile
200 PORT command successful.
150 Opening data connection for testfile (73 bytes).
226 Transfer complete.
80 bytes received in 0.02583 seconds (3.025 Kbytes/s)
Similarly, to copy a file called testfile from the local host to the
remote host, otherhost, use the put sub-command. Following is the
sub-command and its response:
ftp> put testfile
200 PORT command successful.
150 Opening data connection for testfile.
226 Transfer complete.
142 bytes sent in 0.02954 seconds (4.695 Kbytes/s)
Following is a series of commands and responses. Here, we are trying to copy a
file called testfile in binary mode from the /u/testuser directory
on the local host to a directory called /u/testuser/testdir on the remote
host otherhost.
ftp otherhost
Connected to otherhost.
220 otherhost FTP server (Version 4.14 Fri Aug 5 13:39:22 CDT 1994) ready.
Name (otherhost:testuser): testuser
331 Password required for testuser.
Password:
230 User testuser logged in.
ftp> lcd /u/testuser
Local directory now /u/testuser
ftp> cd /u/testuser/testdir
250 CWD command successful.
ftp> binary
200 Type set to I.
ftp> put testfile
200 PORT command successful.
150 Opening data connection for testfile.
226 Transfer complete.
46197 bytes sent in 0.03237 seconds (1394 Kbytes/s)
ftp> quit
221 Goodbye.
Here are some more examples of transferring multiple files, listing files, and
deleting multiple file:
ftp otherhost
Connected to otherhost.
220 otherhost FTP server (Version 4.14 Fri Aug 5 13:39:22 CDT 1994) ready.
Name (otherhost:testuser): testuser
331 Password required for testuser.
Password:
230 User testuser logged in.
ftp> mput file*
mput file1? y
200 PORT command successful.
150 Opening data connection for file1.
226 Transfer complete.
46197 bytes sent in 0.03323 seconds (1358 Kbytes/s)
mput file2? y
200 PORT command successful.
150 Opening data connection for file2.
226 Transfer complete.
44045 bytes sent in 0.01257 seconds (3422 Kbytes/s)
mput file3? y
200 PORT command successful.
150 Opening data connection for file3.
226 Transfer complete.
41817 bytes sent in 0.01172 seconds (3485 Kbytes/s)
ls -l
200 PORT command successful.
150 Opening data connection for /bin/ls.
total 176
-rw-r----- 1 testuser author 1115 Dec 15 11:34 file1
-rw-r----- 1 testuser author 43018 Dec 15 11:34 file2
-rw-r----- 1 testuser author 40840 Dec 15 11:34 file3
226 Transfer complete.
mdel file*
mdel file1? y
250 DELE command successful.
mdel file2? y
250 DELE command successful.
mdel file3? y
250 DELE command successful.
mailx
In UNIX, you can send mail to other users in the system and receive mail from
them by using the mailx commands. The mailx commands provide sub-commands
to facilitate saving, deleting, and responding to messages. This command also provides
facilities to compose and edit messages before finally sending it to one or more
users.
The mail system on UNIX uses mailboxes to receive mail for a user. Each user has
a system mailbox in which all mail for that user is received pending action by the
user. The user can read, save, and delete the mail once the mail is received. Once
the user has read mail, it can be moved to a secondary or personal mailbox. The default
secondary mailbox is called the mbox. The mbox is usually present
in the home directory of the user. However, the user can specify the name of a file
as a secondary mailbox. All messages saved in the mailbox mbox are save
indefinitely until moved to other secondary mailboxes, which are sometimes known
as folders. You can use the folders to organize your mail. For example, you can organize
folders by subject matter and save all mail pertaining to a subject in a particular
mailbox.
You can send messages to one or more users using the mailx command. This
command allows you to send mail to users on the same host or other hosts in the network
to which the local host is connected. You will not get a positive acknowledgment
if the mail delivery is successful, but if the mail cannot be delivered, you will
get notification.
Following is a list of some of the flags that can be used with the mail command:
- -d to display debug information.
- -f to display a list of messages in the default mailbox mbox.
Optionally, you can specify the name of a folder in which you have saved your mail
previously.
- -s subject to associate a subject for the mail to be created.
- -v to display detailed information by the mailx command.
Each mail has information associated with it. The following is a list of this
information:
- status indicates the status of a mail. Following is a list of the various
statuses of a mail item:
- M indicates that the message will be stored in your personal
mailbox.
- > indicates the current message.
- N indicates that the message is a new message.
- P indicates that the message is to be preserved in the system mailbox.
- R indicates that you have read the message.
- U indicates an unread message. An unread message is one that was a new
message at the last invocation of mailx but was not read.
- * to indicate that the message has been saved or written to a file or
folder.
- A message without a status indicates that the message has been read but has not
been deleted or saved.
- number indicates a numerical ID of the message by which it can
be referred to.
- sender indicates the user who sent the message.
- date indicates when the mail was received in the mailbox.
- size indicates the size of the message in number of lines and number
of bytes.
- subject indicates the subject matter of the mail if the sender has associated
a subject with the mail. This may or may not be present depending on whether the
mail has an associated subject.
Following is a list of sub-commands you can use while in mail> prompt:
- q to apply mailbox commands entered this session.
- x to quit.
- !command to start a shell, run a command, and return to mailbox.
- cd to place you in the home directory. Optionally, you can specify a
directory name to place you in the specified directory.
- t to display current message. Optionally, you can specify a message
list to display messages in that list.
- n to display the next message.
- f to display headings of the current message. Optionally, you can specify
a message list and display all headings in that message list.
- e to edit the current message. Optionally, you can specify a message
number to modify that message.
- d to delete messages the current message. Optionally, you can specify
a message list to delete the message in the message list.
- u to restore deleted messages.
- s file to append the current message, including its heading, to a file.
Optionally, you can specify a message list between s and file to
append the specified messages to the file.
- w file to append the current message, excluding its heading, to a file.
Optionally, you can specify a message list between w and file to
append the specified messages to the file.
- pre to keep messages in the system mailbox. Optionally, you can specify
a list of messages to keep them in system mailbox.
- m addresslist to create or send a new message to addresses in the addresslist.
- r to send a reply to senders and recipients of messages. Optionally,
you can specify a list of messages to send a reply to senders and recipients of all
messages in the list
- R to send reply only to senders of messages for the current message.
Optionally you can specify a list of messages to send reply to senders of the messages.
- a to display a list of aliases and their addresses.
Examples You can invoke the mailx command by itself to put you
into the mail> prompt where you can use the sub-commands. You will be
able to get into the mail> prompt only if you have mail. Otherwise, you
will get a message similar to "you have no mail". If you have
mail, prompts similar to the following will be displayed:
mailx
Mail [5.2 UCB] Type ? for help.
"/usr/spool/mail/testuser": 1 message 1 new
>N 1 testuser Sat Nov 16 22:49 285/9644
&
If you now quit the mailx command using the quit sub-command
(can be abbreviated as q), the mail is saved in your personal mailbox (mbox
file in your home directory).
Now to see the mail, use the mailx -f command, which results in the following
dialog:
mailx -f
Mail [5.2 UCB] Type ? for help.
"/u/testuser/mbox": 1 message
> 1 testuser Sat Nov 23 00:11 162/5175
&
To save mail in a folder while in the mailx command, execute the following
sub-command:
& save 1 /u/testuser/folder1/file1
This will create file1 from the first message in the directory /u/testuser/folder1.
Now, if you invoke the mailx command to read the file /u/testuser/folder1,
it results in the following dialog:
mailx -f /u/testuser/folder1
Mail [5.2 UCB] Type ? for help.
"/u/testuser/folder1": 1 message
> 1 testuser Sat Nov 23 00:11 162/5175
&
Once you are in mailx, you can execute the sub-command m to
create and send mail to other users as follows:
& m friend1
Subject: Testing mailx command
This is test of the mailx command
Here we are trying to send a mail to user friend1 with cc to friend2
Cc: friend2
&
The body of the mail is terminated by Ctrl-d. You can send mail to multiple users
using the m sub-command.
talk
You can converse with another user in real time using the talk command
if the other user is logged on. Using the talk command, you can converse
with users on local host or remote host.
The talk command takes one mandatory argument--user name or user and
host name. You can optionally provide a second argument specifying the TTY onto which
the user is logged.
The user on the remote host can be specified in one of the following formats:
- username@host
- host!username
- host.username
- host:username
When you execute the talk command, it opens two windows--one for sending
messages and one for receiving messages, and it will wait for the other user to respond.
Examples If you execute the following command to converse with the user
friend:
talk friend
you will get the following screen:
[Waiting for your party to respond]
-----------------------------------------------------------------------------
with the one half for sending message and other half for receiving messages. If
the specified user is not logged on, you get a message similar to "Your
party is not logged on".
while the user friend will get the following message:
Message from Talk_Daemon@host1 at 0:46 ...
talk: connection requested by testuser@host1.
talk: respond with: talk testuser@host1
[Waiting for your party to respond]
The user friend has to respond with the following command:
talk testuser@host1
to start the conversation.
To quit the talk session, use Ctrl-c.
vacation
If you want to notify a mail sender that you are on vacation, you can use the
vacation command. The message sent can be a customized message if you create
the message in a file called .vacation.msg in your home directory. If this
file does not exist, a system default message is sent. By default, the system message
is sent only once a week to a user who sends mail to you.
The vacation command can be used to forward messages you receive during
vacation to other users by using the .forward file in your home directory.
Include the user names of all the users to whom you want the messages to be forwarded.
The entry in the .forward file is of the following format:
testuser, "|/usr/bin/vacation testuser"
The vacation command also lets you store the names of users who sent
messages to you while you were on vacation. These user names are stored in the .vacation.dir
and .vacation.pag files in your home directory.
The vacation command has one option flag, which is used as follows:
- -i to initialize the .vacation.dir and .vacation.pag
files before the start of your vacation.
The presence of the .forward file in your home directory is used by the
system to identify that you are on vacation. So, once you are back, you should delete
or rename the .forward file.
Before you go on vacation, use the following command to initialize the .vacation.dir
and .vacation.pag files in your home directory:
vacation -I
This should be followed by the creation of the .forward and .vacation.msg
file.
write
The write command can be used to hold a conversation with another user
in the local host or remote host just like the talk command. To hold a conversation
with another user, the following must be true:
The user must be logged on.
The user must not have denied permission by using the mesg command.
A message consists of all the characters you have typed until you hit the Enter
key. Both you and the other user can send messages this way. To end the conversation,
use Ctrl-d.
The following is a list of some of the flags that can be used with the write
command:
- -h Handle,Reply to reply to a message sent by a utility or shell script
using write with the reply option. The handle is a number
that is generated by the system. The reply can be ok, cancel,
or query.
- -nHost to specify a remote host if you want to hold conversation with
a user on a remote host. It is also possible to specify user at a remote host as
username@host.
- -q to find out about messages awaiting replies from users on a host
and display them with their handles.
Examples If you want to hold a conversation with a user called friend,
execute the following command:
write friend
If user friend is not logged on, you will get a message similar to the
following:
friend is not logged on.
If the user friend has used the mesg command to turn the permission
off for conversations, then you will get a message similar to the following:
write: permission denied
If, however, the write command succeeds, the user will get a message
similar to the following:
Message from testuser on mainhost(pts/3) [Fri Nov 22 19:48:30 1996] ...
You can use the UNIX input redirection operator to send long messages from a file
called, for example, long_message:
write friend < long_message
To start the conversation, the other user also has to use the write command
the same way as follows:
write testuser
If you want to hold a conversation with user friend on the remote host
otherhost, execute either of the following two commands:
write friend@otherhost
or
write -n otherhost friend
File Comparison
Here some of the commands that can be used for comparing the contents of the file
are presented. These commands compare the contents and, depending on various options,
generate outputs of what the differences are between the various files. There are
also commands that can be used to compare the contents of directories.
cmp
The cmp command compares the contents of two files and generates output
into standard output. It is possible to have one of the files be standard input but
not both. You can use a - (hyphen) to indicate that the file is standard
input, which is terminated using Ctrl-d. The cmp command should be used
for nontext files to find out whether they are identical. For text files, use the
diff command, discussed later.
The following are the outputs generated by the cmp command:
- No output if the files are exactly identical.
- Displays the byte number and line number of the first position where the files
are different.
The flags that can be used with cmp command are
- -l to display, for each difference, the byte number in decimal and the
differing bytes in octal.
- -s to return only an exit value without generating any output. The values
of return code are: 0 for identical files, 1 if the files are different, or 2 if
the cmp file is not successful in comparing the files.
Examples If you want to compare the new version of an executable file with
the old version of the executable file, execute the following command:
cmp new_prog1 old_prog1
If the files are identical, no output will be generated, but if they are different,
output similar to the following will be generated:
new_prog1 old_prog1 differ: byte 6, line 1
You can use the -s flag, if you are using the cmp command in
a shell script to determine whether two files are identical. Following is part of
a shell script that uses the -s command:
ret_code='cmp -s new_prog1 old_prog1'
if [[ $ret_code -eq 0 ]] then
echo "Files are identical ..."
else
echo "Files are different ..."
fi
If the files are identical except that one file has extra bytes at the end, use
of the -l flag will generate the following output:
cmp -l new_prog1 old_prog1
18 12 63
cmp: EOF on new_prog1
comm
If you have files that are sorted and you want to compare them, use the comm
command. The comm command can be used to either exclude or include the common
lines between the two files. You can use a - (hyphen) to indicate that one
of the files is to be accepted from standard input.
The default output is generated on the standard output in three columns, which
are as follows:
- Lines that exist only in the first file.
- Lines that exist only in the second file.
- Lines that exist in both files.
The following is a list of flags that can be used with the comm command:
- -1 to suppress the display of the first column.
- -2 to suppress the display of the second column.
- -3 to suppress the display of third column.
Examples Let us assume that the there are two files named file1
and file2. The content of the files has been displayed using the more
command as follows:
more file1
line 1
line 2
line 3
line 5
line 6
line 7
line 8
line 9
more file2
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 9
If you compare the two files, file1 and file2, you will get
the following output:
comm file1 file2
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
The output shows that file1 has one line that is not there in file2
(in column 1); file2 has one line that is not there in file1 (in
column 2); and there are seven lines that exist in both file1 and file2.
If you are interested only in the differences, you can drop column 3 as follows:
comm -3 file1 file2
line 4
line 8
If you are interested only in finding out which lines are identical in file1
and file2, you can drop columns 1 and 2 as follows:
comm -12 file1 file2
line 1
line 2
line 3
line 5
line 6
line 7
line 9
diff
You can compare text files with the diff command but cannot compare nontext
files. You can use diff to compare individual files or multiple files with
identical names in different directories. For comparing individual files, use a -
(hyphen) to indicate that one of the files is to be accepted from the standard input.
The group of output lines of the diff command is preceded by an information
line containing the following information:
- Lines that are different in the first file. This may a single line number or
two line numbers separated by a comma, meaning the lines within that range.
- Action code, valid values of which are a (for lines added), c
(for lines modified), and d (for lines deleted).
- Lines that are different in the second file. This may a single line number or
two line numbers separated by a comma, meaning the lines within that range.
The diff command generates ed commands. The ed commands
can be applied to the first file, and doing so will make the second file identical
to the first file.
Three forms of output are generated by the diff command. These are as
follows:
- Number1aNumber2,Number3, which means that the lines Number2
through Number3 in the second file must be added to the first file after
line Number1 to make them identical. This form is followed by the actual
lines from the second file preceded by a > (greater than sign).
- Number1dNumber2, which means that the lines Number1 through
Number2 must be deleted from the first file to make them identical. This
form is followed by the actual lines from the first file preceded by a <
(less than sign).
- Number1,Number2cNumber3,Number4, which
means that the lines Number1 through Number2 in the first file
must be modified by the lines Number3 through Number4 in the second
file to make them identical. This form is followed by the actual lines from the first
file, each preceded by < (less than sign), and then actual lines from
the second file each preceded by > (greater than sign)
The following is a list of some of the flags that can be used with the diff
command:
- -b to ensure that more than one space or tab character is considered
as one. However, leading space or tab characters are processed as is.
- -C LINE to produce the output is a format different from the default
format. The output first identifies the files being compared along with their creation
date and time. Each modified group of lines is separated by a line with an *(asterisk)
followed by lines from the first file and lines from the second file. The lines removed
from the first file are designated with a - (hyphen). The lines added to
the second file are designated with a + (plus sign). Lines that exist in
both files but differ are designated with an ! (exclamation point). Changes
that lie within the specified context lines of each other are grouped together as
output.
- -c LINE to produce the output is a format different from the default
format. The LINE parameter is optional. The output first identifies the
files being compared along with their creation date and time. Each modified group
of lines is separated by a line with an * (asterisk) followed by lines from
the first file and lines from the second file. The lines removed from the first file
are designated with a - (hyphen). The lines added to the second file
are designated with a + (plus sign). Lines that exist in both files but
differ are designated with an ! (exclamation point). Changes that lie within
the specified context lines of each other are grouped together as output.
- -D String to create a merged version of the first and second files on
the standard output. The C preprocessor controls are included so that a compilation
of the results, without defining String, is equivalent to compiling the
first file, while defining String compiles the second file.
- -e to generate output that can be input to ed to produce the
second file from the first file.
- -f to create output that is the reverse of what is produced by the -e
flag.
- -i to compare the two files ignoring case of letters.
- -l to generate formatted output. It also generates a summary at the
end.
- -n to generate output in the reverse order of the -e flag.
Additionally, it generates the count of lines added and deleted.
- -r to execute the diff command on all identically named sub-directories
of the specified directory.
- -s to generate output to obtain a list of identical files along with
differences between identically named files. This will also generate a list of files
that are only in one of the directories.
- -S FILE to ignore files. This option can be used while comparing directories
to ignore filenames that collate less than that of the specified FILE.
- -t to preserve the original indentation of the lines. The indentation
of lines in the output can be modified by the use of > (greater than
sign) or < (less than sign in the output).
- -w to ignore all space and tab characters. That is, treat them identically
for comparison purposes.
Examples Let us assume that we have two files, file1 and file2.
The contents of the files are shown using the more command:
more file1
This is the first line
This is the second line
This is the fourth line
This is the fifth line
This is the sixth line
This is the seventh line
This is the eighth line
This is the NINTH line
more file2
This is the first line
This is the second line
This is the third line
This is the fourth line
This is the sixth line
This is the seventh line
This is the eighth line
This is the ninth line
The plain vanilla diff command on file1 and file2 will
generate output as follows:
diff file1 file2
2a3
> This is the third line
4d4
< This is the fifth line
6,8c6,8
< This is the seventh line
< This is the eighth line
< This is the NINTH line
---
> This is the seventh line
> This is the eighth line
> This is the ninth line
which means add This is the third line to file1, delete This
is the fifth line from file1 and modify line 6 and 7 to This is
the seventh line and This is the eighth line in file1 to make
file1 identical to file2.
If you do not care about space and tab characters, use the -b flag in
the following command:
diff -b file1 file2
2a3
> This is the third line
4d4
< This is the fifth line
8c8
< This is the NINTH line
---
> This is the ninth line
As you can see, lines 6 and 7 are not displayed because the only reason these
lines are different is the existence of extra space and tab characters.
You can display the output in special format using the -C or -c
flag in the following command:
diff -C 0 file1 file2
*** file1 Thu Nov 28 22:15:23 1996
--- file2 Thu Nov 28 18:05:59 1996
***************
*** 2 ****
--- 3 ----
+ This is the third line
***************
*** 4 ****
- This is the fifth line
--- 4 ----
***************
*** 6,8 ****
! This is the seventh line
! This is the eighth line
! This is the NINTH line
--- 6,8 ----
! This is the seventh line
! This is the eighth line
! This is the ninth line
The output contains the filenames being compared and their creation date and time,
and the output has a format different from the default diff format.
If you want to generate output in a format that can be input to ed, use
the -e flag as in the following command:
diff -e file1 file2
6,8c
This is the seventh line
This is the eighth line
This is the ninth line
.
4d
2a
This is the third line
.
This output can then be input to ed to be applied to file1 to
make it identical to file2. This can be used to maintain a base version
and incremental changes so that any version can be created by applying the changes
to the base version. Let us see an example of how we can make file1 identical
to file2 using the -e command.
Let us redirect the output of the diff command to a file called diffout
as in the following command:
diff -e file1 file2 > diffout
If you execute the more command on diffout, you will get the
following output:
more diffout
6,8c
This is the seventh line
This is the eighth line
This is the ninth line
.
4d
2a
This is the third line
.
Let us now add an extra command line at the end of the file diffout.
The command is w, which will ensure that when it is input to ed,
the output is written back to the file on which it is applied. If we execute more
on the file diffout, you can see the extra line at the end:
more diffout
6,8c
This is the seventh line
This is the eighth line
This is the ninth line
.
4d
2a
This is the third line
.
w
To update file1 and see the result, you now use the following command:
ed - file1 < diffout
more file11
This is the first line
This is the second line
This is the third line
This is the fourth line
This is the sixth line
This is the seventh line
This is the eighth line
This is the ninth line
If you do not want to update the original file, use the command 1,$p
instead of w. This generates the output to the standard output instead.
You can also redirect the output to a file as follows:
ed - file1 < diffout > file1.new
To generate output in reverse order to that specified by -e, execute
the following command with the -f flag:
a2
This is the third line
.
d4
c6 8
This is the seventh line
This is the eighth line
This is the ninth line
.
If you do not care about whether the files have lowercase or uppercase letters,
use the -i flag to execute case-insensitive diff as in the following
command:
diff -i file1 file2
2a3
> This is the third line
4d4
<Ti s the fifth line
6,7c6,7
< This is the seventh line
< This is the eighth line
---
> This is the seventh line
> This is the eighth
Notice that the lines This is the ninth line in file1 and This
is the ninth line in file2 are evaluated to be equal due to use of
the -i flag.
If you want to know the number of lines affected by each insertion or deletion,
use the -n flag as in the following command:
diff -n file1 file2
a2 1
This is the third line
d4 1
d6 3
a8 3
This is the seventh line
This is the eighth line
This is the ninth line
The information in the above lines is with respect to file1. It tells
you that one line is to be inserted after line 2, followed by the lines to be inserted,
one line is to be deleted at line 4, three lines are deleted at line 6, and 3 lines
are to be inserted after line 8, followed by lines to be inserted.
To ignore all tab and space characters, use the -w flag. The difference
between the -b and -w flags is that -b ignores all space
and tab characters except leading ones, while -w ignores all. Following
is an example of the -w flag:
diff -w file1 file2
2a3
> This is the third line
4d4
< This is the fifth line
8c8
< This is the NINTH line
---
> This is the ninth line
So far, we have seen the actions of the diff command for comparing two
files. Now let us see some examples of comparing two directories. Let us assume that
the two following sub-directories exist under the current directory:
testdir1 and testdir2
Further, let us see what files exist under these directories:
ls -R test*
testdir1:
file1 file2 file3 file4 file5 file6 testdir3
testdir1/testdir3:
filea fileb filec filed filee
testdir2:
file2 file4 file5 file7 file8 testdir3
testdir2/testdir3:
fileb filed filee filef fileg
The simplest form of the diff command without any flags to compare two
directories will result in the following output:
diff testdir1 testdir2
Only in testdir1: file1
Only in testdir1: file3
Only in testdir1: file6
Only in testdir2: file7
Only in testdir2: file8
Common subdirectories: testdir1/testdir3 and testdir2/testdir3
In the above example, the diff command does not go through the sub-directory
testdir3 under the directory testdir1 and testdir2. If
you want the diff command to traverse the sub-directory under the directories,
use the -r flag as in the following command:
diff -r testdir1 testdir2
Only in testdir1: file1
Only in testdir1: file3
Only in testdir1: file6
Only in testdir2: file7
Only in testdir2: file8
Only in testdir1/testdir3: filea
Only in testdir1/testdir3: filec
Only in testdir2/testdir3: filef
Only in testdir2/testdir3: fileg
If you want to know a list of all files in the directories that are identical,
use the -s command as in the following command:
diff -rs testdir1 testdir2
Only in testdir1: file1
Files testdir1/file2 and testdir2/file2 are identical
Only in testdir1: file3
Files testdir1/file4 and testdir2/file4 are identical
Files testdir1/file5 and testdir2/file5 are identical
Only in testdir1: file6
Only in testdir2: file7
Only in testdir2: file8
Only in testdir1/testdir3: filea
Files testdir1/testdir3/fileb and testdir2/testdir3/fileb are identical
Only in testdir1/testdir3: filec
Files testdir1/testdir3/filed and testdir2/testdir3/filed are identical
Files testdir1/testdir3/filee and testdir2/testdir3/filee are identical
Only in testdir2/testdir3: filef
Only in testdir2/testdir3: fileg
If you do not want to process all files whose names collate before the specified
filename (in this case file2), use the -S flag as in the following
command:
diff -r -S file2 testdir1 testdir2
Only in testdir1: file3
Only in testdir1: file6
Only in testdir2: file7
Only in testdir2: file8
Only in testdir1/testdir3: filea
Only in testdir1/testdir3: filec
Only in testdir2/testdir3: filef
Only in testdir2/testdir3: fileg
diff3
The diff command compares two files. If you want to compare three files
at the same time, use the diff3 command. The diff3 command writes
output to the standard output that contains the following notations to identify the
differences:
- ==== means all three files differ.
- ====1 means the first file differs.
- ====2 means the second file differs.
- ====3 means the third file differs.
The following is a list of flags that can be used with the diff3 command:
- -3 to produce an edit script that contains only lines containing the
differences from the third file.
- -E, -X to produce an edit script where the overlapping lines from both
files are inserted by the edit script, bracketed by <<<<<<
and >>>>>> lines.
- -e to create an edit script that can be input to the ed command
to update the first file with differences that exist between the second and third
(that is, the changes that normally would be flagged ==== and ====3).
- -x to produce an edit script to incorporate only changes flagged ====.
The format of the generated output is as follows:
- File Id:Number1 a means that lines are to be added after line Number1
in the file File Id. The File Id can be 1, 2, or 3, depending
on the file it is referring to. This is followed by the lines to be added.
- File Id:Number1[,Number2]c means that lines in the range Number1
through Number2 are to be modified. This is followed by the lines to be
modified.
Examples Let us assume that we have three files: file1, file2
and file3. The contents of these three files are shown below using the more
command:
more file1
This is the first line in first file
This is the second line
This is the third line
This is the fourth line
This is the fifth line
This is the sixth line
This is the seventh line
This is the eighth line
This is the ninth line
more file2
This is the first line
This is the second line
This is the third line
This is the 3.5th line
This is the fourth line
This is the sixth line in second file
This is the seventh line
This is the eighth line
This is the ninth line
more file3
This is the first line
This is the second line
This is the third line
This is the fourth line
This is the sixth line in third file
This is the seventh line
This is the eighth line
This is the ninth line
This is the tenth line
This is the eleventh line
Now execute diff3 on these three files without using any flag, as in
the command below:
diff3 file1 file2 file3
====1
1:1c
This is the first line in first file
2:1c
3:1c
This is the first line
====2
1:3a
2:4c
This is the 3.5th line
3:3a
====
1:5,6c
This is the fifth line
This is the sixth line
2:6c
This is the sixth line in second file
3:5c
This is the sixth line in third file
====3
1:9a
2:9a
3:9,10c
This is the tenth line
This is the eleventh line
The first group of lines starting with ====1 show that line 1
of file1 is different from the line 1 of file2 and file3.
The lines starting with ====2 show that line 4 in file2
should be inserted after line 3 of file1 and file3 to
make them identical. The lines starting with ==== show that line 5,
6 of file1, line 6 of file2 and line 5 of
file3 are all different. The lines starting with ====3 show that
line 9, 10 of file3 should be inserted after line 9 of
file1 and file2 to make them identical.
If you are interested in only finding out the differences in file3, use
the -3 flag as in the following command:
diff3 -3 file1 file2 file3
9a
This is the tenth line
This is the eleventh line
.
w
q
This tells that there are two lines lines 9 and 10 that are
present in file3 but not in file1 or file2.
If you want to apply changes between file2 and file3 to file1,
use the -e flag to create an edit script as in the following command:
diff3 -e file1 file2 file3
9a
This is the tenth line
This is the eleventh line
.
5,6c
This is the sixth line in third file
.
w
q
This output means that file3 has two extra lines at line 9 and
line 6 of file2 has been replaced by line 5 of file3.
If, however, you are interested in changes, use the -x flag as in the following
command:
diff3 -x file1 file3 file2
5,6c
This is the sixth line in second file
.
w
q
dircmp
If you want to compare the contents of two directories, use the dircmp
command. This command compares the names of the files in each directory and generates
a list of filenames that exist only in one of the directories followed by filenames
that exist in both and whether they are identical or not.
The following is a list of flags that can be used with dircmp command:
- -d to generate a list of files that exist in either of the directories
followed by a list of files that exist in both and whether they are identical or
different. This is further followed by output of diff command on pairs of
files that are different
- -s to generate a list of files that exist in either of the directories
followed by list of files that are different
Examples Let us assume that the there are two directories testdir1
and testdir2 in the current directory. The list of files in these directories
are as follows:
ls testdir1
file1 file2 file3 file4 file5 file6
ls testdir2
file2 file3 file5 file6 file7 file8
If you want to do a plain vanilla dircmp between these two directories,
execute the following command:
dircmp testdir1 testdir2
Fri Nov 29 22:51:34 1996 testdir1 only and testdir2 only Page 1
./file1 ./file7
./file4 ./file8
Fri Nov 29 22:51:34 1996 Comparison of testdir1 and testdir2 Page 1
directory .
different ./file2
same ./file3
same ./file5
same ./file6
The first part of the above report shows the files present only in testdir1
on the left and only in testdir2 on the right. The second part shows a comparison
of the directories and also shows which files are identical and which are different.
If you want further information on what are the differences between the files file2
in these directories, use the -d flag as in the following command:
testdir -d testdir1 testdir2
Fri Nov 29 22:56:01 1996 testdir1 only and testdir2 only Page 1
./file1 ./file7
./file4 ./file8
Fri Nov 29 22:56:01 1996 Comparison of testdir1 and testdir2 Page 1
directory .
different ./file2
same ./file3
same ./file5
same ./file6
Fri Nov 29 22:56:01 1996 diff of ./file2 in testdir1 and testdir2 Page 1
1c1
< This file is in testdir1
---
> This file is in testdir2
If you want only a list of files that are unique to each directory and files that
are different, use the -s flag as in the following command:
dircmp -s testdir1 testdir2
Fri Nov 29 23:39:59 1996 testdir1 only and testdir2 only Page 1
./file1 ./file7
./file4 ./file8
Fri Nov 29 23:39:59 1996 Comparison of testdir1 and testdir2 Page 1
different ./file2
If you want to suppress the display of identical files, but you want list of the
files that are different and the difference between these files, execute the dircmp
command with both -s and -d flags.
sdiff
The command sdiff compares two files and displays output on the standard
output in a side-by-side format. Following is the detail of the display format:
- if the two lines are identical then displays each line of the two files with
a series of spaces between them
- if the line only exists in the first file, then a < (less than sign) is displayed
at the end of the line
- if the line only exists in the second file, then a > (greater than sign) is
displayed at the beginning of the line
- if the lines from the two files are different, then a | (vertical bar) is displayed
between the lines
The flags that can be used with sdiff command are as follows:
- -s if you do not want to display the identical lines
- -w number to set the width of the display to number
- -l to display only the line from the first file if the lines from the
two
- files are identical
- -o file to create a merged file from the first and second file depending
on a number of sub-commands you can specify
Examples Let us assume that we have two files file1 and file2
whose contents are displayed below using the more command:
more file1
This is the first line in first file
This is the second line
This is the third line
This is the fourth line
This is the fifth line
This is the sixth line
This is the seventh line
This is the eighth line
This is the ninth line
more file2
This is the first line
This is the second line
This is the third line
This is the 3.5th line
This is the fourth line
This is the sixth line in second file
This is the seventh line
This is the eighth line
This is the ninth line
If you execute sdiff command on the two files file1 and file2,
we get the following result:
sdiff file1 file2
This is the first line in first file | This is the f
irst line
This is the second line This is the s
econd line
This is the third line This is the t
hird line
> This is the 3
.5th line
This is the fourth line This is the f
ourth line
This is the fifth line | This is the s
ixth line in second file
This is the sixth line <
This is the seventh line This is the s
eventh line
This is the eighth line This is the e
ighth line
This is the ninth line This is the n
inth line
If, however, you do not want to display the identical lines, use the -s
flag as in the following command:
sdiff -s file1 file2
This is the first line in first file | This is the f
irst line
> This is the 3
.5th line
This is the fifth line | This is the s
ixth line in second file
This is the sixth line <
You can use the -l to display only the line from the first file if the
lines are identical so that the other lines stand out as in the following command:
sdiff -l file1 file2
This is the first line in first file | This is the f
irst line
This is the second line
This is the third line
> This is the 3
.5th line
This is the fourth line
This is the fifth line | This is the s
ixth line in second file
This is the sixth line <
This is the seventh line
This is the eighth line
This is the ninth line
File Manipulation Commands
Here we will discuss several commands that can be used to manipulate various attributes
of one or more files, as well as to copy and move files from one location to another.
The various attributes that can be manipulated include modification time, permission,
and more.
touch
The touch command can be used for a number of purposes depending on whether
a file already exists. If a file does not exist, the touch command will
create it if you have write access to the directory. If a file is already present,
the touch command modifies the last modification time of the file.
Examples To create a file called testfile in the current directory,
execute the following command:
touch testfile
To create testfile in the /u/testuser/testdir, execute the following
command:
touch /u/testuser/testdir/testfile
chmod
You may need to modify the permission of a directory or files either to secure
them or to make them accessible to others. You can use the chmod command
to modify the permission or files and directories. The permission in UNIX is specified
in octal number (0 thorough 7). Permission for a file or directory can be specified
for the following:
- Owner--The user who created the file
- Group--The group to which the owner belongs
- World or others--Users other than the owner and users in the group to which the
owner belongs
For each of these, one octal number is specified to designate the permission.
The permission for the owner, group, and world is derived on the basis of three
bits associated with read, write, and execute authority for the file. That is, the
bit for read will have a value of one if read permission is to be granted, the bit
for write will have a value of one if write permission is to be granted, and the
bit for execute will have a value of one if execute permission is to be granted.
You should be aware that the execute bit functions differently for directories.
The execute permission for a directory is used to designate whether you are able
to access that directory.
The combination of these three bits is expressed as an octal number and is used
to designate the permission. The weight associated with the read bit is 4, the weight
associated with write is 2, and the weight associated with execute is 1. The value
of the permission is derived as follows:
(4 * value of read bit) + (2 * value of write bit) + (1 * value of execute bit)
The value of the permission can vary from 0 (no read, write, or execute permission)
to 7 (read, write, and execute permission).
For example, if you want to provide read and write permission but no execute permission,
then the value to be used will be:
(4 * 1) + (2 * 1) + (1 * 0) = 6
You should be aware that execute permission on a directory means that the directory
can be accessed. That is, operations can be performed on files residing in that directory.
If you provide write permissions to a directory, the user will be able to read, write,
delete, and execute all files in that directory, irrespective of the permissions
of the individual files.
With the chmod command, you specify the new permissions you want on the
file or directory. The new permission can be specified in one the following two ways:
- As a three-digit numeric octal code
- As symbolic mode
Examples If you want testfile to have the permission: Owner with
read, write, execute; group with read only; and others with execute only, you must
execute the following command:
chmod 741 testfile
While using the symbolic mode, the following will need to be specified:
- Whose (owner, group, or others) permission you want to change.
- What operation (+ (add), - (subtract), or = (equals)) you want to perform on
the permission.
- The permission (r, w, x, and so on).
If you want to setup the permission for testfile owned by you in the
current directory so that only you and users in your group can read and write the
file, execute the following command using absolute permissions:
chmod 660 testfile
If you want to add write permission for the group for testfile in the
current directory (assuming that currently testfile has 741 permission),
execute the following command:
chmod g+w testfile
Similarly, if you want to revoke the read permission for others for testfile
in the current directory, execute the following command:
chmod o-r testfile
If you want to grant the same permissions to the world (other) as the group for
testfile in the current directory, execute the following command:
chmod o=g testfile
NOTE: Modifying the permissions will
not have any effect on the root user. The root user has access
to all files and directories irrespective of the permissions you may have granted.
chgrp
If you want to change the group to which the file belongs, use the chgrp
command. The group must be one of the groups to which the owner belongs. That is,
the group must be either the primary group or one of the secondary groups of the
owner.
Examples Assume user testuser owns the file testfile,
and the group of the file is staff. Also assume that testuser belongs
to the groups staff and devt. To change the owner of testfile
from staff to devt, execute the following command:
chgrp devt testfile
chown
In case you want to change the owner of a file or directory, use the chown
command.
CAUTION:: On UNIX systems with
disk quotas, only the root user can change the owner of a file or directory.
Examples If the file testfile is owned by the user called testuser,
to change ownership of the file to user friend, you must execute the following
command:
chown friend testfile
rm
Once you are done using a file and you do not want to use it any more, then you
would like to remove the file so as to regain the space utilized by the file. The
rm command lets you do this by removing files permanently from the disk.
If an entry is the last link to a file, the file is deleted. To remove a file from
a directory, you do not need either read or write permission to the file, but you
do need write permission to the direct |