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 directory containing the file. The rm command is usually used to remove files, but it provides a special flag -r to remove files in a directory recursively, including the directory and its sub-directories.

Following is a list of some of the flags that can be used with the rm command:

  • -i to interactively remove the files.
  • -f to remove the files without any messages. This will not generate any messages for cases where a file does not exist or you do not have permission to remove one or more files.
  • -r to remove files within a directory and directories themselves recursively.

The native version of the rm command does not ask for confirmation while removing files. So you should be careful when using wildcards with the rm command.

Examples If you want to remove all files starting with test in the current directory, execute the following command:

rm test*

However, if you make a typing mistake and type the following:

rm test *

you will remove all the files because of the asterisk (*).


CAUTION:: Be careful when using wildcards with the rm command. Be sure before you remove a file, because once a file is removed, it cannot be recovered.


To avoid any such mistakes, use the -i flag to indicate that you want to execute the rm command in interactive mode. In this mode, the system will ask you for confirmation before removing the file. Only if you confirm it with a y will the system remove the file. Following is the dialog you can have with the system if you want to remove two files in the current directory: testfile1 and testfile2, using the -i flag with the rm command:

rm -i testfile*
Remove file testfile1? y
Remove file testfile2? y

You can use the flag -f with the rm command if you do not want to get any messages from the command. Usually rm will display a messages that a file is not present if you do not provide the correct name of the file. However, using the -f flag forces rm to not display any messages. If you execute the following command:

rm -f testfile

the file testfile will be deleted if present, and no action will be taken if testfile is not present. In either case, you will not get any message from the rm command. Also, the rm -f command always has a return code of 0 (zero).

You can use the -r flag to remove files in directories recursively including directories and sub-directories. If there is a directory called testdir under the current directory that, in turn, has the files testfile1 and testfile2, execute the following command to remove the files testfile1 and testfile2 as well as the directory testdir:

rm -r testdir

It is advisable that you should set up an alias for the rm command as rm -i in your environment so that you have to confirm before deleting the files.

The rm command processes the hard linked file in a different way. If you have a testfile1 in your current directory, execute the following command to create a file called testfile2 that is hard linked to the file testfile1:

ln testfile1 testfile2

This, in effect, creates two identical files: testfile1 and testfile2. If you now execute the following command:

ls -l testfile*

you will get the following result:

-rw-r--r--   2 testuser    staff         10 Nov  3 14:28 testfile1
-rw-r--r--   2 testuser    staff         10 Nov  3 14:28 testfile2

Here both testfile1 and testfile2 show the number of links as 2, because they are linked using hard link. Now if you remove the file testfile1 using the rm command as follows:

rm testfile1

there will be two actions--to remove the file testfile1 and to decrease the link count of the file testfile2 from 2 to 1. Now if you repeat the ls command, you will get the following display:

-rw-r--r--   1 testuser    staff         10 Nov  3 15:38 testfile2

where the number of links of testfile2 is now 1.

mv

If you are not satisfied with a filename, you may wish to name the file differently. The mv command will let you do that. In addition, it allows you to move files from one directory to another retaining the original filename, which is equivalent to copying the files from the source directory to the destination directory and then removing the file from the source directory. You may do that if you are reorganizing your files. While moving files or directories, the target file or directory gets the permission of the source file or directory, irrespective of whether the target file or directory already exists or not.

Following is a list of some of the flags that may be used with the mv command:

  • -i to move or rename files interactively.
  • -f to move or rename files without any messages. Use of this flag will suppress messages when you are trying to rename a nonexistent file or you do not have permission to rename a file.

The mv commands takes two arguments. The first argument is the source file or directory name, and the second argument is the destination file or directory. However, the behavior of the mv command depends on whether the destination file or directory name exists.

If you move files within the same filesystem, all links to other files are retained. But if you move the files across filesystems, the links are not retained.

Examples To rename a file in the current directory, use the following command:

mv source_file dest_file

If the file dest_file does not exist, a new dest_file is created by copying source_file into it, and source_file is removed. If dest_file exists and you have write permission to it, source_file is copied to dest_file and removed. On the other hand, if you do not have permission, then mv does not take any action.

To move source_file from the current directory to the /u/testuser/target_dir directory, retaining the name, execute one of the following command:

mv source_file /u/testuser/target_dir
mv source_file /u/testuser/target_dir/.

If the file already exists in /u/testuser/target_dir, the existing file is overwritten.

To move source_file from the current directory to the /u/testuser/target_dir directory with the name target_file, execute the following command:

mv source_file target_dir/target_file

If you are not sure whether the file target_file exists, use the -i flag as follows:

mv -i source_file target_dir/target_file

If the file target_file exists, the system will prompt you with a message to confirm whether you want to move the file.

The . (period) as the target filename indicates that the source filename is to be retained. This is especially useful if you want to move multiple files to another directory. If you want to move all files with names beginning with test to the /u/testuser/target_dir directory, execute the following command:

mv test* /u/testuser/target_dir/.

To rename a directory, source_dir to /u/testuser/target_dir directory, execute the following command:

mv /u/guahs/source_dir /u/testuser/dest_dir

If the directory dest_dir does not exist, the directory /u/testuser/source_dir is renamed to /u/testuser/dest_dir. If /u/testuser/dest_dir exists and you have write permissions to it, all the files and sub-directories under /u/testuser/source_dir are moved to /u/testuser/dest_dir.

cp

The cp command can be used to make a copy of the contents of one or more source files as specified target files. If the target file already exists, it is overwritten with the contents of the source file. The cp command behavior varies depending on whether the source and the target are files or directories.

The following is a list of some of the flags that can be used with the cp command:

  • -p to retain the modification date and time as well as permission modes of the source file.
  • -i to execute the copy command in an interactive mode so that it asks for confirmation if the target file exists.
  • -h to follow the symbolic links.
  • -r to copy files under the specified directories and their sub-directories. Treats special files, such as linked files, the same way as regular files.

Examples In its simplest form, you can execute the following command to copy source_file to target_file under the current directory:

cp source_file target_file

If you want to copy source_file to the /u/testuser/target_dir directory retaining the filename, execute the following command:

cp source_file /u/testuser/target_dir/.

To copy all files in /u/testuser/source_dir to the /u/testuser/target_dir directory while retaining the filenames and the last modification time and permissions, execute the following command:

cp -p /u/testuser/source_dir/* /u/testuser/target/dir/.

This will not copy any sub-directories or any files under those sub-directories. To copy all the files in a directory as well as sub-directories and files in those sub-directories while retaining the last modification date and time and permissions for all files and sub-directories, use the following command:

cp -r /u/testuser/source_dir /u/testuser/target_dir/.

If you are not sure whether the target file already exists, use the -i flag. Following is a dialog for copying testfile from the current directory to the /u/testuser/testdir directory assuming that testfile already exists in the /u/testuser/testdir directory:

cp -i testfile /u/testuser/testdir/.
overwrite /u/testuser/testdir/testfile? y

cat

You have seen that the cp command allows you to copy one file into another file. It does not allow you, however, to copy multiple files into the same file. To concatenate multiple files into a single file, use the cat command. By default, the cat command generates outputs into the standard output and accepts input from standard input. The cat command takes in one or more filenames as its arguments. The files are concatenated in the order they appear in the argument list.

The following is a list of some of the flags that can be used with the cat command:

  • -b to eliminate line numbers from blank lines when used with the -n flag.
  • -e to display a $(dollar sign) at the end of each line, when specified with the -v flag.
  • -n to display output lines preceded by line numbers, numbered sequentially from 1.
  • -q to suppress message if the cat command cannot find one or more of the input files.
  • -v to display nonprintable characters in the file as printable characters.

CAUTION:: If you are using the output redirection operator (>) to redirect the standard output of the cat command, be careful not to use one of the input filenames as the output filename. If you do that, the input filename will be overwritten. Some UNIX versions may give you an error message when you try to do that but overwrite the file anyway.


While accepting input from the standard input, you should use Ctrl-d to indicate the end of the input.

Examples In its most simple form, you can just type in the command cat, which should put you in the entry mode. In this mode you can enter multiple lines followed by Ctrl-d to signal the end. The cat command will display the lines you have just entered:

cat
This is test line 1
This is test line 1
This is test line 2
This is test line 2
Ctrl d

You should be aware that the cat command does not provide any prompt in the above case.

If you want display a file called testfile in the current directory on your terminal, execute the following command:

cat testfile

This will produce output as follows:

This is a test file
This does not contain anything meaningful
This is for demo only

You should be careful if the file is big. A large file will scroll by on your terminal, and you will only see the last few lines. You can get around this by piping the output to either the more or pg command as follows:

cat testfile | more

To concatenate multiple files for display on the terminal, use the following command:

cat testfile1 testfile2 testfile3

If you want to concatenate these files into a file called testfile, use the redirection operator > as follows:

cat testfile1 testfile2 testfile2 > testfile

If the file testfile already exists, it is overwritten with the concatenated files testfile1, testfile2 and testfile3. If testfile already exists and you want to concatenate at the end of the existing file, instead of using the redirection operator >, you must use the >> (two consecutive greater than sign) operator as follows:

cat testfile1 testfile2 testfile2 >> testfile

If you try to concatenate a file or a number of files so that one or more files do not exist, cat will concatenate all the available files and, at the end, generate a message about the nonexistent files. If you want to concatenate two files, testfile1 and testfile2, into the file testfile in the current directory and you mis-type testfile2 as testtile2 while executing the following command:

cat testfile1 testtile2 > testfile

you will get a message similar to the following, and testfile will only have the contents of testfile1:

cat: cannot open testtile2

If you use the -q flag, you will not get the error message.

If you have testfile in the current directory containing the following lines (note that the last line contains special characters), cat will show the following:

This is a test file
 
This file does not contain anything meaningful
This file is for demo only
^F^F^F^F^F

If you execute the cat command with the -n flag, cat will display lines with line numbers, but the last line with special characters will be displayed as a blank line:

cat -n testfile
     1  This is a test file
     2
     3  This file does not contain anything meaningful
     4  This is for demo only
     5

If you want to be sure that the blank lines displayed actually do not contain any characters other than nonprintable ones, use the -v flag with the cat command. This ensures that the nonprintable characters are displayed as printable characters as follows:

cat -v testfile
This is a test file
 
This file does not contain anything meaningful
This is for demo only
^F^F^F^F^F

rcp

So far we have seen a number of commands to move or copy files between directories within the local host. If you need to copy files from one host to another, the rcp command can be used to copy files between different or the same hosts. You can execute the rcp command on a local host to copy files between local host and a remote host or between two remote hosts.

The filename on the remote host is preceded by the remote host ID as hostname:/dirname/filename. The colon (:) is used as a delimiter between the host name and the filename.

It is also possible to specify the user name at the remote host as username@hostname:/dirname/filename. The at-sign (@) is used as a delimiter between the user name and the host name. The user name, however, is optional. If not specified, user name at the remote host is the same as user name at the local host.

If neither the source nor the target file specifies the host name, the rcp command behaves the same way as the cp command.

If the filename on the remote host is not qualified fully, starting with the root directory, the filename or directory name is assumed to start with the home directory of the remote user.

If the files do not already exist on the remote host, they are created with the default permission of the remote user. If the files already exist on the remote host, the permissions of the target files are preserved.

Like the cp command, you can use the rcp command to copy directories and files within directories.

The following is a list of some of the flags that can be used with the rcp command:

  • -p to create the target file with the modification date and time of the source file as well as permission of the source file.
  • -r to copy files recursively while copying directories.

NOTE: For you to use the rcp command to transfer files from or to a remote host, you must have the local host name defined in the /etc/hosts.equiv file at the remote host, or the local host name and the user name defined in the .rhosts file in the home directory of the user on the remote host.


Examples If you want to copy testfile from the current directory to testfile in the directory testdir under the home directory on the remote host called otherhost, execute the following command:

rcp testfile otherhost:testdir/testfile

If the user name on the local host is testuser, this command will assume that the user name on the remote host is testuser. If the user name testuser does not exist on the remote host and you must use the user name newtestuser on the remote host, execute the following command:

rcp testfile newtestuser@otherhost:testdir/testfile

If you must transfer testfile from a remote host otherhost1 to another remote host otherhost2, and you want to preserve the modification date and time as well as the permission, execute the following command:

rcp -p testuser1@otherhost1:testfile testuser2@otherhost2:testfile

This copies testfile from the home directory of user testuser1 on the remote host otherhost1 to testfile in the home directory of testuser2 on remote host otherhost2.

If you want to copy all the files in the directory /u/testuser/testdir from the remote host otherhost to the current directory on the local host, execute the following command:

rcp testuser@otherhost:/u/testuser/testdir/* .

This will not copy any sub-directories you may have in testdir or any files in those sub-directories. To copy all the sub-directories and files in those sub-directories, use the following command:

rcp -r testuser@otherhost:/u/testuser/testdir/* .

ln

Sometimes you need to provide alternate names for the same file. This can be achieved by linking a filename to another using the ln command. It is possible to link a file to another name in the same directory or the same name in another directory.

When linking a filename to another filename, you can specify only two arguments: the source filename and the target filename. When linking a filename to a directory, you can specify multiple filenames to be linked to the same directory.

If you are linking using hard links, you cannot link to a file in another filesystem, but using soft links, you can link filenames across filesystems.

The flags that can be used with the ln command are as follows:

  • -s to create a soft link to another file or directory. In a soft link, the linked file contains the name of the original file. When an operation on the linked filename is done, the name of the original file in the link is used to reference the original file.
  • -f to ensure that the destination filename is replaced by the linked filename if the file already exists.

By default, the ln command creates a hard link.

Examples If you want to link testfile1 to testfile2 in the current directory, execute the following command:

ln testfile1 testfile2

This creates a hard linked testfile2 linking it to tesftfile1. In this case, if one of the files is removed, the other will remain unaltered.

If testfile is in the current directory and is to be linked to testfile in the directory /u/testuser/testdir, execute the following command:

ln testfile /u/testuser/testdir

To create a symbolic link of testfile1 in the current directory, execute the following command:

ln -s testfile1 testfile2

This creates a linked testfile2, which will contain the name of testfile1. If you remove testfile1, you will be left with an orphan testfile2, which points to nowhere.

If you want to link all the files in the current directory to another directory, /u/testuser/testdir, execute the following command:

ln * /u/testuser/testdir/.

Directory Manipulation Commands

When you are set up as a user in a UNIX operating system, you usually are set up with the directory /u/username as your home directory. To organize your files, you must set up directories of your liking. Here we will present the commands to create and remove directories.

mkdir

To create a directory, use the mkdir command. The mkdir command accepts multiple directory names for creation at the same time. As you did with the files, use relative pathname or absolute pathname to create a directory. To create a directory, you must have write permission for its parent directory, and UNIX uses the current permission setting (refer to the umask command) to set the permission for the directory.

The following is a list of the flags that can be used with the mkdir command:

  • -p to create all the directories in the part name of the specified directory if they do not exist.
  • -m permission to specify permission for the directory to be created.

Examples If your current directory is /u/testuser

mkdir temp

will create a directory called temp under the directory /u/testuser, whose absolute pathname is /u/testuser/temp.

mkdir /u/testuser/temp

can also be used to have the same effect as the previous one.

mkdir ../temp

can be used to create the /u/temp directory. Here we have used .. (two consecutive periods) as part of the relative pathname to indicate that the directory temp will be created under the directory one level up, that is, /u.

To create testdir1 and testdir2 in the current directory, use the following command:

mkdir testdir1 /u/testuser/temp/testdir2

which will create testdir1 in the current directory and testdir2 in /u/testuser/temp (assuming it exists). In this example, testdir1 uses a relative pathname while /u/testuser/temp/testdir2 uses an absolute pathname.

If the directory testdir is already present and if you try to create the directory again, you will get a message similar to the following:

mkdir: cannot create testfir.
testdir: File exists

If you want to create the directory testdir under the current directory and grant the access 770 to it, execute the following command:

mkdir -m 770 testdir

If you want to create the directory testdir under the current directory and subdirectory temp under testdir, create both of them using a single command as follows:

mkdir -p testdir/temp

rmdir

Once you are done with a directory or you run out of space and want to remove a directory, use the rmdir command. You can remove a directory only if it is empty, that is, all the files and directories in it have been removed. You can specify multiple directory names as arguments to rmdir command. To remove a directory, you must have write permission to the parent directory.

The following is a flag that can be used with rmdir command:

  • -p to remove all the directories in the specified pathname.

Examples If your current directory is /u/testuser and it contains the temp subdirectory, to remove temp, use the command:

rmdir temp

If the directory temp is not empty, you will get a message similar to the following:

rmdir: Directory temp is not empty.

Assume you are in the directory /u/testuser and it contains a sub-directory testdir and the sub-directory testdir contains a sub-directory temp. To remove the directory testdir in the current directory and the sub-directory temp under testdir, execute the following command (assuming that all the files and directories under them have been removed):

rmdir -p testdir/temp

File Information Commands

Each file and directory in UNIX has several attributes associated with it. UNIX provides several commands to inquire about and process these attributes.

ls

The ls command can be used to inquire about the various attributes of one or more files or directories. You must have read permission to a directory to be able to use the ls command on that directory and the files under that directory. The ls command generates the output to standard output, which can be redirected, using the UNIX redirection operator >, to a file.

You can provide the names of one or more filenames or directories to the ls command. The file and directory names are optional. If you do not provide them, UNIX processes the current directory.

Be default, the list of files within a directory is sorted by filename. You can modify the sort order by using some of the flags discussed later.

You should also be aware that the files starting with . (period) will not be processed unless you use the -a flag with the ls command. This means that the entries . (single period) and .. (two consecutive periods) will not be processed by default.

Following is a list of some of the flags that can be used with the ls command:

  • -A to list all entries in a directory except . (single period) and .. (two consecutive periods).
  • -a to list all entries in a directory including hidden files (filenames starting with . (period)).
  • -b to display nonprintable characters. The characters are displayed in an octal (\nnn) notation.
  • -c to use the time of last modification of the i-node. When used with the -t flag, the output is sorted by the time of last modification of the i-node. When used with the -l flag, the time displayed is the last modification time of the i-node. This flag must be used with the -t or -l flag.
  • -C to sort output vertically in a multiple column format. This is the default method when output is to a terminal.
  • -d to restrict the information displayed only to that of the directory specified. By default, the information of the files or sub-directories under a directory is also displayed.
  • -e to display the following information for each specified file or directory:

1. permission associated with the files and directories
2. number of links
3. owner
4. group
5. size (in bytes)
6. time of last modification
7. name of each file. If a special file, the size field contains the major and minor device numbers. If the file is a symbolic link, the path name of the linked-to file is printed preceded by a -> (minus sign followed by greater than sign). The attributes of the symbolic link are displayed.

  • -f to list the name in each slot for each directory specified in the directory parameter. This flag turns off the -l, -t, -s, and -r flags, and turns on the -a flag. The order of the listing is the order in which entries appear in the directory.
  • -F to put special characters before different file types as follows:

1. / (slash) after each directory
2. * (asterisk) if the file is executable
3. = (equal sign) if the file is a socket
4. | (pipe sign) if the file is a FIFO
5. @ (at sign) for a symbolic link

  • -g displays the following information for files and directories:

1. permission
2. number of links
3. group
4. size (in bytes)
5. time of last modification

  • -i to display the i-node number in the first column of the report for each file.
  • -l to display the following information about specified files and directories:

1. permission
2. number of links
3. owner
4. group
5. size (in bytes)
6. time of last modification

  • -m to display the output in a comma-separated format.
  • -n to display the following information for specified files and directories:

1. permission
2. number of links
3. owner id
4. group id
5. size (in bytes)
6. time of last modification

  • -o to display the following information about specified files and directories:
    • permission
    • number of links
    • owner id
    • size (in bytes)
    • time of last modification
  • - to put a slash (/) after each directory name.
  • -q to display nondisplayable characters in filenames as a ? (question mark).
  • -r to reverse the order of the sort. If the list is to be displayed in name order, it will be displayed in reverse name order. If the list is to be displayed in descending time order (using the -t flag), that is, latest one first, the list will be displayed in ascending time order, oldest one first.
  • -R to list all subdirectories recursively under the specified directory.
  • -s to provide the size of files and directories in kilobytes.
  • -t to sort the list of entries by time of last modification (latest first) instead of by name.
  • -u to use time of last access instead of time of last modification. If used with -l, time of last access is displayed instead of time of last modification. If used with -t, the output is sorted by time of last access instead of last modification. This flag must be used with the -l and -t flags.
  • -x to sort the output horizontally in a multiple column format.
  • -1 to display the output as one entry per line.

The permission details displayed by the ls command when certain flags, such as -l, are used consists of 10 characters, details of which are as follows:

  • Byte 1: d designates a directory, b designates a block special file, c designates a character special file, l designates a symbolic link, p designates a first-in, first-out (FIFO) special file, s designates a local socket, designates an ordinary file (for example, one which contains text).
  • Byte 2: r if read permission for owner has been granted, - (hyphen) if read permission for owner has not been granted.
  • Byte 3: w if write permission for owner has been granted, - (hyphen) if write permission for owner has not been granted.
  • Byte 4: x if execute permission for owner has been granted, - (hyphen) if execute permission for owner has not been granted, s if the file has set-user-ID mode.
  • Byte 5: r if read permission for group has been granted. - (hyphen) if read permission for group has not been granted.
  • Byte 6: w if write permission for group has been granted, - (hyphen) if write permission for group has not been granted.
  • Byte 7: x if execute permission for group has been granted, - (hyphen) if execute permission for group has not been granted, s if the file has setgroup-ID mode.
  • Byte 8: r if read permission for others has been granted, - (hyphen) if read permission for others has not been granted.
  • Byte 9: w if write permission for others has been granted, - (hyphen) if write permission for others has not been granted.
  • Byte 10: x if execute permission for others has been granted, - (hyphen) if execute permission for others has not been granted.

The execute permission for a file means that the file is an executable file. But the execute permission for a directory means that you can execute searches on the specified directory to locate one or more files.

Examples Let us assume that the following files and directories are present in the current directory .dot1, test1, test2, test3, and test4. Also assume that test2 is a directory.

The simplest form of the ls command can be used to get the list of files and directories in the current directory as follows:

ls
test1  test2 test3  test4 test5

In this list the entry .dot1 is not displayed because the file .dot1 is a hidden file. To display all the entries including the hidden files, execute the following command:

ls -a
. .. .dot1 test1  test2 test3  test4 test5

From the above list, you cannot get details about the entry. To get a detailed list of all the files and directories, execute the following command with the -a flag:

ls -la
total 56
drwxrwx---   3 testuser    author    3072 Nov 24 17:35 .
drwxr-xr-x  36 root        system    2048 Nov 23 19:51 ..
-rw-r--r--   1 testuser    author       0 Nov 24 14:54 .dot1
-rw-r--r--   1 testuser    author      10 Nov 24 17:36 test1
drwxr-xr-x   2 testuser    author     512 Nov 24 17:32 test2
-rw-r--r--   1 testuser    author       0 Nov 24 14:58 test3
-rw-r--r--   1 testuser    author       0 Nov 24 17:33 test4
-rw-r--r--   1 testuser    author   11885 Nov 24 11:50 test5

Use of the -a flag displays the two special entries that are present in all directories: . (a single period) to identify the specified directory and .. (two consecutive periods) to identify the parent directory of the specified directory. In the above example, . (a single period) identifies current directory and .. (two consecutive periods) identifies the parent directory.

If you just want to have a list of directories, execute the following command with the -d flag:

ls -ald
drwxrwx---   3 testuser   author       3072 Nov 24 17:15 .

As you have seen in the above examples, the list of files and directories are ordered by name. If you want to get a list of the entries by time of last modification so that you know which you have worked on last, execute the following command with the -t flag:

ls -lat
total 56
drwxrwx---   3 testuser    author    3072 Nov 24 17:37 .
-rw-r--r--   1 testuser    author      10 Nov 24 17:36 test1
-rw-r--r--   1 testuser    author       0 Nov 24 17:33 test4
drwxr-xr-x   2 testuser    author     512 Nov 24 17:32 test2
-rw-r--r--   1 testuser    author       0 Nov 24 14:58 test3
-rw-r--r--   1 testuser    author       0 Nov 24 14:54 .dot1
-rw-r--r--   1 testuser    author   11885 Nov 24 11:50 test5
drwxr-xr-x  36 root        system    2048 Nov 23 19:51 ..

Until now, we have not specified any file or directory name in the ls command. If you want to search for all entries that start with test, specify test* as the entry name as follows:

ls -la test*
-rw-r--r--   1 testuser    author      10 Nov 24 17:36 test1
-rw-r--r--   1 testuser    author       0 Nov 24 14:58 test3
-rw-r--r--   1 testuser    author       0 Nov 24 17:33 test4
-rw-r--r--   1 testuser    author   11885 Nov 24 11:50 test5
 
test2:
total 16
drwxr-xr-x   2 testuser    author     512 Nov 24 17:32 .
drwxrwx---   3 testuser    author    3072 Nov 24 17:41 ..
-rw-r--r--   1 testuser    author       0 Nov 24 17:45 test21
-rw-r--r--   1 testuser    author       0 Nov 24 14:58 test22

Notice that the entries . (single period), .. (two consecutive periods) and .dot1 are not displayed above because the wildcard *(asterisk) does not match the . (period) character.

If you want to obtain a comma-separated list of file and directory names in the current directory, execute the following command with the -m flag:

ls -am
., .., .dot1, test1, test2, test3, test4, test5

If you want to obtain a list of entries while being able to identify the directories with / (slash), execute the following command with the -p flag:

ls -ap
./                    test1                 test4
../                   test2/                test5
.dot1                 test3

A similar output can be obtained using the -F flag, although -F is more versatile. That is, -F can also identify executable files, symbolic links, and so on.

If you want to get the list of entries in the reverse order of name, execute the following command with the -r flag:

ls -rla
total 56
-rw-r--r--   1 testuser    author   11885 Nov 24 11:50 test5
-rw-r--r--   1 testuser    author       0 Nov 24 17:33 test4
-rw-r--r--   1 testuser    author       0 Nov 24 14:58 test3
drwxr-xr-x   2 testuser    author     512 Nov 24 17:32 test2
-rw-r--r--   1 testuser    author      10 Nov 24 17:36 test1
-rw-r--r--   1 testuser    author       0 Nov 24 14:54 .dot1
drwxr-xr-x  36 root        system    2048 Nov 23 19:51 ..
drwxrwx---   3 testuser    author    3072 Nov 24 18:00 .

To obtain a list of all files in the current directory as well as all files under all the sub-directories, execute the following command with the -R flag:

ls -lR
total 40
-rw-r--r--   1 testuser    author      10 Nov 24 17:36 test1
drwxr-xr-x   2 testuser    author     512 Nov 24 17:32 test2
-rw-r--r--   1 testuser    author       0 Nov 24 14:58 test3
-rw-r--r--   1 testuser    author       0 Nov 24 17:33 test4
-rw-r--r--   1 testuser    author   11885 Nov 24 11:50 test5
 
./test2:
total 0
-rw-r--r--   1 testuser    author       0 Nov 24 17:45 test21
-rw-r--r--   1 testuser    author       0 Nov 24 14:58 test22

Following are examples of the ls command with and without the -u flag. The list without the -u flag displays the time of last modification while the one with the -u flag displays the time of last access:

ls -lu
total 40
-rw-r--r--   1 testuser    author      10 Nov 24 17:34 test1
drwxr-xr-x   2 testuser    author     512 Nov 24 18:19 test2
-rw-r--r--   1 testuser    author       0 Nov 24 14:58 test3
-rw-r--r--   1 testuser    author       0 Nov 24 17:33 test4
-rw-r--r--   1 testuser    author   11885 Nov 24 17:56 test5
ls -l
total 40
-rw-r--r--   1 testuser    author      10 Nov 24 17:36 test1
drwxr-xr-x   2 testuser    author     512 Nov 24 17:32 test2
-rw-r--r--   1 testuser    author       0 Nov 24 14:58 test3
-rw-r--r--   1 testuser    author       0 Nov 24 17:33 test4
-rw-r--r--   1 testuser    author   11885 Nov 24 11:50 test5

find

If you are not sure where a particular file exists, use the find command to search for the particular file. The find command gives you the flexibility to search for a file by various attributes, such as name, size, permission, and so on. Additionally, the find command allows you to execute commands on the files that are found as a result of the search.

The format of the find command is as follows:

find directory-name search-expression

The directory name can be a full path name or a .(single period) for the current directory.

The following is a list of terms that can be used with the find command:

  • -name filename to specify the name of the file (including wildcards) to be used for searching. You can also use range as part of the wildcards. If you want to use wildcard characters, you must specify them within quotes. For example, "test*" will find all files starting with test. If you specify, "test[1-2]", you will find files that start with test and have 1 or 2 as the last characters such as test1 and test2.
  • -size Number to specify the size of the file to be used for searching. The file size specified is in blocks. To specify that you want to match size of files less than the specified size, use a - (minus sign) in front of the size and if you want to match size of files greater than the specified size use a + (plus sign) in front of the size. For example, -size 5 will match files that have size of 5 blocks (the size of a file while matching is always rounded up to the next nearest block), -size -5 will match files that have size of less than or equal to 5 blocks and -size +5 will match files that have size of more than 5 blocks.
  • -size Numberc to specify the size of the file to be used for searching. That is, specify a c at the end of the number. The file size is then taken to be specified in number of bytes. To specify that you want to match size of files less than the specified size, use a - (minus sign) in front of the size and if you want to match size of files greater than the specified size use a + (plus sign) in front of the size. For example, -size 50c will match files that have size of 50 bytes blocks, -size -50c will match files that have size of less than or equal to 50 bytes and -size +50c will match files that have size of more than 50 bytes.
  • -prune to restrict the find command not to process directories recursively. By default find recursively processes all the directories and sub-directories under the specified directory.
  • -atime number to search for files that have been accessed in the specified number of 24-hour periods. The number of 24-hour periods is computed by adding 1 to the number specified. 0 means the last 24 hours.
  • -mtime number to search for files that have been modified in the specified number of 24-hour periods. The number of 24-hour periods is computed by adding 1 to the number specified. 0 means the last 24 hours.
  • -ctime number to search for files whose i-node has been modified in the specified number of 24-hour periods. The number of 24-hour periods is computed by adding 1 to the number specified. 0 means the last 24 hours.
  • -type filetype to search for a specific type of file. The following is a list of types that can be used:
    • b--Block special file
    • c--Character special file
    • d--Directory
    • f--Regular file
    • l--Symbolic link
    • p--FIFO (a named pipe)
    • s--Socket
  • -user is the username to search for files whose owner matches the specified username.
  • -perm permission to search for files with specified permission. The permission is specified as an octal number of up to 3 digits. If the permission is not preceded by a - (hyphen), an exact match of the permission specified is made with the file permissions. If permission is preceded by a - (hyphen), file permission is ANDed with the specified permission. For example, if you want to search for files with owner read permission, use -perm -400.
  • -newer filename to search for files that has time of modification later than that of the specified filename.
  • -group groupname to search for files that belong to the specified group.
  • -inum Number to search for files whose i-node number matches the specified i-node number
  • -links Number to search for files with a specified number of links. To specify that you want to match number of links less than the specified number of links, use a - (minus sign) in front of the number of links, and if you want to match a number of links greater than the specified number of links use a + (plus sign) in front of number of links.
  • -ls to print the current path name along with the following attributes:
    • i-node number
    • Size in kilobytes (1024 bytes)
    • Protection mode
    • Number of hard links
    • User
    • Group
    • Size in bytes
    • Modification time
  • -execcommand to execute the command. To execute the command on the list of the files found by the find command use {} followed by \; (backslash followed by semi-colon).
  • -ok command to execute the command. To execute the command on the list of the files found by the find command use {} followed by \; (backslash followed by semi-colon).
  • UNIX asks for confirmation before proceeding with executing the command.
  • -print to print the output generated as a result of the search.

These operators can be specified in conjunction with each other to form complex criteria for searches. You can combine several operators as follows:

operator -a operator to search for files that satisfy both the specified conditions.

operator -o operator to search for files that satisfy either of the specified conditions.

!operator to search for files that do not satisfy the specified condition.

Examples Let us assume the following files exist in the current directory:

ls -al
total 64
drwxrwx---   3 testuser    author      3072 Nov 25 00:41 .
drwxr-xr-x  36 root     system      2048 Nov 23 19:51 ..
-rw-r--r--   1 testuser    author       0 Nov 24 14:54 .dot1
-rw-------   1 testuser    author      10 Nov 24 17:36 test1
drwxr-xr-x   2 testuser    author     512 Nov 24 17:32 test2
-r-x------   1 testuser    author       0 Nov 24 14:58 test3
-rw-r--r--   1 testuser    author       0 Nov 24 17:33 test4
-rw-r--r--   1 testuser    author   15647 Nov 24 18:32 test5

In its simplest form, you can execute the following command to get a list of all the files in the current directory and its sub-directories.

find . -print
.
./test5
./test1
./test3
./test4
./test2
./test2/test21
./test2/test22
./.dot1

If you want to search for all the files in the current directory that have been modified in the last 24 hours, use the -mtime operator as follows:

find . -mtime 0 -print
.
./test5
./test1
./test3
./test4
./test2
./test2/test21
./test2/test22
./.dot1

To search for a file whose permission is 600 (only owner has read and write permissions), execute the following command using the -perm operator:

find . -perm 600  -print
./test1

In this case, only the file that has permission of exactly 600 is displayed. However, if you want to search for a file with owner read and write permission, execute the following command using a -(hyphen) in front of 600:

find . -perm -600  -print
.
./test5
./test1
./test4
./test2
./test2/test21
./test2/test22
./.dot1

If you are interested in searching for directories only, use the -type operator and execute the following command:

find . -type d -print
.
./test2

To get more information about the files that are found as a result of the search, use the -ls operator and execute the following command:

find . -ls
    2    4 drwxrwx---  3 settlea     eod          3072 Nov 25 01:11 .
   16   16 -rw-r--r--  1 testuser    author        647 Nov 24 18:32 ./test5
   18    4 -rw-------  1 testuser    author         10 Nov 24 17:36 ./test1
   19    0 -r-x------  1 testuser    author          0 Nov 24 14:58 ./test3
   20    0 -rw-r--r--  1 testuser    author          0 Nov 24 17:33 ./test4
67584    4 drwxr-xr-x  2 testuser    author        512 Nov 24 17:32 ./test2
67585    0 -rw-r--r--  1 testuser    author          0 Nov 24 17:45 ./test2/test21
67586    0 -rw-r--r--  1 testuser    author          0 Nov 24 14:58 ./test2/test22
   22    0 -rw-r--r--  1 testuser    author          0 Nov 24 14:54 ./.dot1

To search for all filenames that start with test, use the -name operator and execute the following command:

find . -name "test*" -print
./test5
./test1
./test3
./test4
./test2
./test2/test21
./test2/test22

As you can see, the find command traversed the sub-directory test2 to obtain the filenames under that also. If you want to restrict the search only to the current directory and leave out the sub-directories, use the operator -prune and execute the following command:

find . -name "test*" -prune -print
./test5
./test1
./test3
./test4
./test2

To find a list of files in the current directory that are newer than the file test1, use the operator -newer and execute the following command:

find . -newer test1  -print
./test5
./test2/test21

On the other hand, if you want to find a list of files older than the file test1, use the negation operator ! in conjunction with the operator -newer in the following command:

find . ! -newer test1  -print
.
./test3
./test4
./test2
./test2/test22
./.dot1

If you want a list of all files that are exactly 10 bytes in size, use the -size operator and execute the following command:

find . -size 10c  -print
./test1

If you want to create a list of all files that are less than 10 bytes in size, execute the following command (the command is exactly the same as preceding one except the hyphen in front of the 10):

find . -size -10c -print
./test3
./test4
./test2/test21
./test2/test22
./.dot1

If you want a list of all files that have zero size, execute the find command with the -exec parameter as follows:

find . -size 0c -exec ls -l {} \;
-r-x------  1 testuser    author          0 Nov 24 14:58 ./test3
-rw-r--r--  1 testuser    author          0 Nov 24 17:33 ./test4
-rw-r--r--  1 testuser    author          0 Nov 24 17:45 ./test2/test21
-rw-r--r--  1 testuser    author          0 Nov 24 14:58 ./test2/test22
-rw-r--r--  1 testuser    author          0 Nov 24 14:54 ./.dot1

If you want to remove all the files with zero size but want to confirm the delete before you actually removed them, execute the following command with the -ok operator:

find . -size 0c -ok rm {} \;
< rm ... ./test3 > (yes)?   y
< rm ... ./test4 > (yes)?   n
< rm ... ./test2/test21 > (yes)?   y
< rm ... ./test2/test22 > (yes)?   y
< rm ... ./.dot1 > (yes)?   y

Here you have decided not to remove the file test4.

All the examples we have seen so far use one operator at a time. It is possible to execute the find command with complex conditions with multiple operators combined with each other using or or and conditions. If you want to find out about all the files that start with test and have a size of zero, execute the following command:

find . -name 'test*' -size 0c -print
./test3
./test4
./test2/test21
./test2/test22

In this example we have combined two different operators. It is possible to use the same operator multiple times and combine it with and or or operators. If you want to search for all files in the current directory that have a size of more than zero bytes and less than 50 bytes and whose name starts with test, use the following command:

find . -size +0c -a -size -50c -name `test*' -exec ls -l {} \;
-rw-------  1 testuser    author         10 Nov 24 17:36 ./test1

file

The command file can be used to determine the type of the specified file. The file command actually reads through the file and performs a series of tests to determine the type of the file. The command then displays the output as standard output.

If a file appears to be ASCII, the file command examines the first 512 bytes and tries to determine its language. If a file does not appear to be ASCII, the file command further attempts to distinguish a binary data file from a text file that contains extended characters.

If the File argument specifies an executable or object module file and the version number is greater than 0, the file command displays the version stamp.

The file command uses the /etc/magic file to identify files that have some sort of a magic number; that is, any file containing a numeric or string constant that indicates type.

Examples If you have a file called letter in you current directory that contains a letter to your friend, then executing the command

file letter

will display the following result

letter:  commands text

If you have a file called prog that is an executable program and you are working on IBM RISC 6000 AIX version 3.1, then executing the command

file prog

will display the following result (if you are on a RISC 6000 system).

prog:        executable (RISC System/6000 V3.1)

If you are in /dev directory, which contains all the special files, then executing the command

file hd1

for a file called hd1 (which is a disk on which a filesystem has been defined) will display the following result

hd1:            block special

File Content Related Commands

Here we will discuss some of the commands that can be used to look at the contents of the file or parts of it. You can use these commands to look at the top or bottom of a file, search for strings in the file, and so on.

more

The more command can be used to display the contents of a file one screen at a time. By default, the more command displays one screen worth of data at a time. However, the number of lines displayed can be modified. The more command pauses at the end of display of each page. To continue, press a space bar so that the next page is displayed or press the Return or Enter key to display the next line. Mostly the more command is used where output from other commands are piped into the more command for display.

Following is a list of flags that can be used with the more command:

  • -d for more to prompt to quit, continue, or get help.
  • -f to count logical lines in the file.
  • -number to set the size of the display window to number.
  • -p to disable scrolling. This will result in the more command clearing the screen at the start of each page.
  • -s to display only one blank line when multiple contiguous blank lines are present in the file.
  • -u to display lines without special attributes if lines in the file have special attributes.
  • -v to prevent display of nondisplayable characters graphically.
  • -w to allow you to go back in a file after reaching end of file. Default for more is to exit when end of file is reached.
  • +number to start display at line number in the file.
  • +g to start at the end of the file and be able to go backwards.
  • +/pattern to start in the file at the line number where pattern occurs first.

As we have already stated the more command pauses at the end of each page of display. There are several sub-commands you can use, when more pauses, to control further behavior of the more command. These sub-commands are as follows:

  • number spacebar to page forward by number and by one screen if number is not specified.
  • number d to page forward by a default number of lines (usually 11) if number is not specified and by number of lines if number is specified.
  • number z to page forward by specified number of lines if number is specified; otherwise, by one screen page.
  • number s to skip specified number of lines and display one screen page. If number is not specified, the next line is displayed.
  • number f to skip forward the specified number of screens. If number is not specified, the next screen is displayed.
  • number b to skip backward the specified number of screens. If number is not specified, the previous screen is displayed.
  • number Ctrl-B or numberCtrl-b to skip backward the specified number of screens. If number is not specified, the previous screen is displayed.
  • q to quit more command.
  • v to invoke vi editor.
  • number/expression to search for the expression and its position the number of occurrences specified by number. If the file has less than the specified number of occurrences of the expression, the display remains unaltered.
  • number n to search forward for the specified occurrence of the last expression entered. If number is not specified, the next occurrence is searched for and the screen is displayed.
  • !command to start command with the filename as argument if command is specified. If command is not specified, you will be returned to the shell prompt. You can then use the exit command to get back to the more command.
  • number:n to skip to the specified file following the current file if you have invoked the more command with multiple files. If the specified relative file number is invalid, more skips to the last file.
  • number:p to skip to the specified file previous to the current file if you have invoked the more command with multiple files. If the specified relative file number does not exist, more skips to the first file.
  • :f (followed by return key) to display the filename of the current file being displayed and the current line number being displayed at the top of the screen.
  • :q or :Q (followed by return key) to quit the more command.
  • . (single period) to repeat the last command executed.

Examples Let us assume that we have a file called file1 in the current directory. The content of the file is shown below:

This is the line 1
This is the line 2
This is the line 3
This is the line 4
This is the line 5
This is the line 6
This is the line 7
This is the line 8
This is the line 9
This is the line 10
This is the line 11
This is the line 13
This is the line 14
This is the line 15
This is the line 16
This is the line 17
This is the line 18
This is the line 19
This is the line 20
This is the line 21
This is the line 22
This is the line 23
This is the line 24
This is the line 25

If you want to display file1, use the following command:

more file1
This is the line 1
This is the line 2
This is the line 3
This is the line 4
This is the line 5
This is the line 6
This is the line 7
This is the line 8
This is the line 9
This is the line 10
This is the line 11
This is the line 13
This is the line 14
This is the line 15
This is the line 16
This is the line 17
This is the line 18
This is the line 19
This is the line 20
This is the line 21
This is the line 22
This is the line 23
--More--(91%)

This has a disadvantage because once the end of file is reached, the more command is exited. If do not want to exit from the more command even when the end of file is reached, use the -w flag. This is especially useful if you are looking at a file that is in the process of being created. The following command shows the use of -w flag:

more -w file1

If you want to start from the bottom of the file rather than the top of the file and go backwards, use the +g flag as in the following command:

more +g file1
This is the line 3
This is the line 4
This is the line 5
This is the line 6
This is the line 7
This is the line 8
This is the line 9
This is the line 10
This is the line 11
This is the line 12
This is the line 13
This is the line 14
This is the line 15
This is the line 16
This is the line 17
This is the line 18
This is the line 19
This is the line 20
This is the line 21
This is the line 22
This is the line 23
This is the line 24
This is the line 25
--More--(EOF)

If you want to start the display of the file at line number 20 of file1, use the following command:

more +20 file1
This is the line 20
This is the line 21
This is the line 22
This is the line 23
This is the line 24
This is the line 25

If you want to display the five files: file1, file2, file3, file4, and file5, execute the following command:

more file1 file2 file3 file4 file5

less

The less command is one more in the family of commands to view the contents of a file. This may not be available by default on all UNIX systems. It behaves similarly to the more command. The less command allows you to go backward as well as forward in the file by default.

The following is a list of sub-commands that can be used once you are in the less command:

  • h to display a list of the sub-commands that can be used.
  • spacebar or Ctrl-v or Ctrl-f or f to go forward in the file one screen. If preceded by a number, moves forward by the specified number of lines.
  • return key or Ctrl-n or Ctrl-e or e or j to move forward by 1 line. If preceded by a number, moves forward by the specified number of lines.
  • Ctrl-b or b to go backward in the file by one screen. If preceded by a number, moves backward by the specified number of lines.
  • Ctrl-d or d to go forward by half screen (default of 11 lines). If preceded by a number, moves forward by the specified number of lines. The new number is then registered as the new default for all subsequent d or u commands.
  • g to go the top of the file by default. If preceded by a number, the file is positioned at the line specified by the number.
  • G to go the bottom of the file by default. If preceded by a number, the file is positioned at the line specified by the number.
  • p or % to go to the top of the file by default. If preceded by a number between 0 an 100, positions to the percentage of file specified by the number.
  • Ctrl-p or Ctrl-k or Ctrl-y or k or y to go backward by 1 line. If preceded by a number, moves backward by the specified number of lines.
  • Ctrl-u or u to go backward by half a screen (default of 11 lines). If preceded by a number, moves backward by the specified number of lines. The new number is registered as the new default for all subsequent d or u commands.
  • Ctrl-l or Ctrl-r or r to redraw the current screen.
  • mlowercaseletter to mark the position with the lowercaseletter marker for use later.
  • `lowercaseletter to go to the position marked with lowercaseletter using the m sub-command. If ` is followed by ^ (caret), less displays the top of the file. If ` is followed by $ (dollar sign), less displays the bottom of the file.
  • number/pattern to position the file in the specified occurrence of the pattern in the file. You can use the following special characters to indicate special actions:
    • ! to search for lines that do not contain the specified pattern.
    • * to search multiple files if invoked with multiple files.
    • @ to start search at the top of the first file if invoked with multiple files.
    • n to repeat the last search executed. If the sub-command is preceded by a number, the file is positioned to the specified occurrence of the pattern previously specified. You can use the N sub-command to search in the reverse direction.
  • :e filename or :E filename to execute the less command for a specified filename and add it to the list of files for subsequent use.
  • :n to execute the less command on the next file on the list of files. The list of files can be specified by invoking less with multiple files, or added to the list by using the :e sub-command. You can use the :p sub-command similarly for previous file in the list.
  • :numberx to execute less on the first file in the list if number is not specified. If a number is specified, the less command on the file in that position in the list is executed.
  • = or :f to get information about the file and the current position in the file.
  • q or Q to exit less.
  • v to invoke vi with the current filename.
  • !command to execute a shell command. If command is omitted, you will be put into a shell prompt. You can exit the shell by using the exit command to be back into the less command.

Examples To invoke the less command for a file named file1, use the following command:

less file1

tail

You can use the tail command to display, on standard output, a file starting from a specified point from the start or bottom of the file. Whether it starts from the top of the file or end of the file depends on the parameter and flags used. One of the flags, -f, can be used to look at the bottom of a file continuously as it grows in size. By default, tail displays the last 10 lines of the file.

The following is a list of flags that can be used with the tail command:

  • -c number to start from the specified character position number.
  • -b number to start from the specified 512-byte block position number.
  • -k number to start from the specified 1024-byte block position number.
  • -n number to start display of the file in the specified line number.
  • -r number to display lines from the file in reverse order.
  • -f to display the end of the file continuously as it grows in size.

With all these flags, the number you can specify may be a number prefixed by a + (plus sign) or a - (minus sign). If you specify a + the tail command starts processing from the start of the file. If you specify a - or do not specify any sign, tail starts processing from the bottom of the file.

Examples Let us assume that we have a file called file1 that contains 30 lines. The contents of the file are displayed below:

This is the line 1
This is the line 2
This is the line 3
This is the line 4
This is the line 5
This is the line 6
This is the line 7
This is the line 8
This is the line 9
This is the line 10
This is the line 11
This is the line 12
This is the line 13
This is the line 14
This is the line 15
This is the line 16
This is the line 17
This is the line 18
This is the line 19
This is the line 20
This is the line 21
This is the line 22
This is the line 23
This is the line 24
This is the line 25
This is the line 26
This is the line 27
This is the line 28
This is the line 29
This is the line 30

If you want to see the last 10 lines of the file, execute the tail command without any flags as follows:

tail file1
This is the line 21
This is the line 22
This is the line 23
This is the line 24
This is the line 25
This is the line 26
This is the line 27
This is the line 28
This is the line 29
This is the line 30

In the preceding example, the last 10 lines of file1 are displayed. If you want to skip 27 lines from the start of the file, execute the following command:

tail +27 file1
This is the line 28
This is the line 29
This is the line 30

In this example, the display starts at the 28th line from the top of the file. If you want to start from a specified byte position in the file instead of the line position, use the -c flag as follows:

tail -c +500 file1
the line 27
This is the line 28
This is the line 29
This is the line 30

In this example, the display start at the 500th byte from the top of the file. If you want to specify an absolute line number from which to display the file, use the -n flag as in the following command:

tail -n -5 file1
This is the line 26
This is the line 27
This is the line 28
This is the line 29
This is the line 30

In this example, the display starts at the 5th line from the bottom. If you want to display the lines of file1 in reverse order, use -r flag as in the following command:

tail -r -n -5 file1
This is the line 30
This is the line 29
This is the line 28
This is the line 27
This is the line 26

In this example, the last 5 lines are displayed in reverse order with the last line first.

head

The head command displays a file on the standard output. The head command starts from the top of the file and displays the specified number of bytes or lines from the start of the file. By default, head displays 10 lines.

Following are the flags that can be used with the head command:

  • -c number to display the number of bytes from the top of the file.
  • -n number to display the number of lines from the top of the file.

The number can be specified without any sign or preceded by a -, both of which mean the same thing.

Examples Let us assume that we have file1 whose contents are the same as the one shown in the tail command.

If you want to display a specified number of lines from the top, use the -n flag as in the following command:

head -3 file1
This is the line 1
This is the line 2
This is the line 3

In this example, the first three lines of file1 are displayed. If you want to display the first specified number of bytes from the top of the file, use the -c flag as in the following command:

head -c 29 file1
This is the line 1
This is th

In this example, the first 29 bytes of file1 are displayed.

wc

The wc command counts the number of bytes, words, and lines in specified files. A word is a number of characters stringed together delimited either by a space or a newline character.

Following is a list of flags that can be used with the wc command:

  • -l to