TOC
BACK
FORWARD
HOME

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 count only the number of lines in the file.
  • -w to count only the number of words in the file.
  • -c to count only the number of bytes in the file.

You can use multiple filenames as argument to the wc command.

Examples If you want to know the number of bytes, words, and lines in file1, execute the following command:

wc file1
     25     125     491 file1

This example shows that file1 has 25 lines, 125 words, and 491 bytes. If you want to find only the number of words in file1, use the -w flag as in the following command:

wc -w file1
    125 file1

If you want to get the word count on file1 and file2, execute the following command:

wc -w file1 file2
    125 file1
    463 file2
    588 total

Notice that if you use multiple files, you will get an extra line in the output that has the total of all files. .

read

The read command is used in shell scripts to read each field from a file and assign them to shell variables. A field is a string of bytes that are separated by a space or newline character. If the number of fields read is less than the number of variables specified, the rest of the fields are unassigned.

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

  • -r to treat a \(backslash) as part of the input record and not as a control character.

Examples Following is a piece of shell script code that reads first name and last name from namefile and prints them:

while read -r lname fname
do
       echo $lname","$fname
done < namefile

od

The od command can be used to display the contents of a file in a specified format. Usually, this command is used to look at executable files or other files that are nontext, which most of the UNIX commands cannot process. You can also specify the offset from where you want the display of the file start.

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

  • -d to display the output as a signed decimal number.

  • -i to display the output as an unsigned decimal number.

  • -f to display the output as a floating number.

  • -b to display the output as an octal value.

  • -h to display the output as a hexadecimal value.

  • -c to display the output as ASCII characters.

You can specify the offset of the byte where you want to start the display after the filename. If the offset is preceded by 0x, the offset is interpreted as a hexadecimal number. If the offset is preceded by 0, the offset is interpreted as an octal number. The offset can be suffixed by b for bytes, k for kilobytes (1024 bytes) and m for megabytes (1024 x 1024 bytes).

Examples To display the contents of file1, execute the following command: .

od file1 | more
0000000  000737 000007 031147 104407 000000 000000 000000 000000
0000020  000110 010007 000413 000001 000002 024250 000001 056674
0000040  000012 030504 000001 052320 000000 001000 000000 000000
0000060  000001 055020 000004 000002 000004 000004 000007 000005

This displays file1 in decimal format. If you want to display the file in hexadecimal format, use the -h flag as in the following command:

od -h file1 | more
0000000  01df 0007 3267 8907 0000 0000 0000 0000
0000020  0048 1007 010b 0001 0002 28a8 0001 5dbc
0000040  000a 3144 0001 54d0 0000 0200 0000 0000
0000060  0001 5a10 0004 0002 0004 0004 0007 0005

If you want to start the display at byte position 40 and display in ASCII format, use the following command:

od -c file1 +40 | more
0000040   \0  \n   1   D  \0 001   T 320  \0  \0 002  \0  \0  \0  \0  \0
0000060   \0 001   Z 020  \0 004  \0 002  \0 004  \0 004  \0 007  \0 005

It is possible to display the contents of file1 in octal, ASCII, and hexadecimal format all at once, using the following command:

od -bch file1 | more
0000000  001 337 000 007 062 147 211 007 000 000 000 000 000 000 000 000
         001 337  \0 007   2   g 211 007  \0  \0  \0  \0  \0  \0  \0  \0
            01df    0007    3267    8907    0000    0000    0000    0000
0000020  000 110 020 007 001 013 000 001 000 002 050 250 000 001 135 274
          \0   H 020 007 001 013  \0 001  \0 002   ( 250  \0 001   ] 274
            0048    1007    010b    0001    0002    28a8    0001    5dbc
0000040  000 012 061 104 000 001 124 320 000 000 002 000 000 000 000 000
          \0  \n   1   D  \0 001   T 320  \0  \0 002  \0  \0  \0  \0  \0
            000a    3144    0001    54d0    0000    0200    0000    0000
0000060  000 001 132 020 000 004 000 002 000 004 000 004 000 007 000 005
          \0 001   Z 020  \0 004  \0 002  \0 004  \0 004  \0 007  \0 005
            0001    5a10    0004    0002    0004    0004    0007    0005.

pg

The pg command can be used to display the contents of a file one page at a time, just like the more and less commands. The pg command pauses at the end of each screen display so that you can enter a number of sub-commands that can be used to search a string in the file, go backward or forward in the file, and so on.

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

  • -c to clear the screen at the end of each page of display and start the display at the top of the screen.

  • -e continues to the next file at the end of one file, if the pg command is invoked with multiple files. Usually, pg pauses at the end of each file.

  • -f to truncate lines that are longer than the width of screen display.

  • -p string to display the string as the pg command prompt. The default prompt is : (colon). If the string specified is %d, the page number is displayed at the prompt.

  • -s to highlight all messages and prompts issued by the pg command.

  • +number to start the display at the specified line number in the file.

  • -number to set the size of the display screen to the specified number of lines.

  • +/pattern/ to search for the pattern in the file and start the display at that line.

A number of sub-commands can be used with the pg command when it pauses at the end of each screen of display. You must press the Return key after entering each sub-command. Following is a list of some of these sub-commands:

  • -number to go backward the number of pages specified by number.

  • +number to go forward the number of pages specified by number.

  • l to go forward in the file by one line.

  • number l to start the display in the file at the line specified by number.

  • +numberl to go forward the number of lines specified by number in the file.

  • -numberl to go backward the number of lines specified by number in the file.

  • d to go forward by half a screen.

  • -d to go backward by half a screen.

  • -n to indicate to pg that it should interpret and execute the sub-commands as they are entered without waiting for the newline character to be entered.

  • Ctrl-l to redraw the current screen.

  • $ to go to the last page of the file.

  • number /pattern/ to search forward for the pattern in the file starting at the beginning of the next page. If number is specified, then pg searches for the specified occurrence number of pattern. The search does not wrap around. If you want to search backward, use ? (question mark) instead of / (slash).

  • number p to start executing the pg command on the previous file if number is not specified. If number is specified, start at the file whose position in the list of files is number before the current file.

  • number n to start executing the pg command on the next file if number is not specified. If number is specified, start at the file whose position in the list of files is number after the current file.

  • sfilename to save the current file being processed in the specified filename.

  • q or Q to quit the pg command.

Examples Let us assume that we have file1 whose content is the same as that shown in the tail command.

To change the number of lines to be displayed by the pg command, prefix the size by a - (minus sign) as in the following command:

pg -7  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
:

In this example, the number of lines displayed is modified to 7. On the other hand, if you want to start the display at the 7th line, prefix the number by a + (plus sign) as in the following command:

pg +7 file1

If you want to modify the default prompt of : (colon) with your personalized prompt, use the -p flag as in the following command:

pg -7 -s -p "Enter Sub-command -> " 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
Enter Sub-command ->

In this example, the default prompt has been replaced by Enter Sub-command -> prompt. If you want to start the file with the line where the pattern line 5 appears, execute the following command:

pg +/"line 5"/ file1

tee

If you want to execute a command and want its output redirected to multiple files in addition to the standard output, use the tee command. The tee command accepts input from the standard input, so it is possible to pipe another command to the tee command.

Following is an optional flag that can be used with the tee command:

  • -a to append to the end of the specified file. The default of the tee command is to overwrite the specified file.

Examples If you want to use the cat command on file1 to display on the screen, but you want to make a copy of file2, use the tee command as follows:

cat file1 | tee file2 | more

If you want to append file1 to the end of an already existing file2, use the flag -a as in the following command:

cat file1 | tee -a file2 | more

vi

The vi command can be used to edit one of more files using full screen mode. If a filename is not provided, UNIX creates an empty work file without any name. If a filename is provided the file does not exist, an empty work file with the specified name is created. The vi command does not modify existing files until the changes are saved.


CAUTION: The vi command does not lock a file while editing it. So it is possible that more than one user can edit it at the same time. The version of the file saved last is the one that is retained.

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

-c sub-command to execute the specified sub-command before placing the specified file in editing mode.

-r filename to recover the specified filename.

-R to place the specified file in editing mode with read-only option so that any modifications made cannot be saved.

-ynumber to set the editing window to a size with number of lines.

The following is a list of modes the vi editor has:

  • command mode is the default mode when you enter vi. In this mode, you can enter various sub-commands to manipulate the lines, such as deleting lines, pasting lines, moving to a different word, moving to a different line, and so on.

  • text input mode is the mode in which you can modify the text in the lines or enter new lines. You can enter this mode by using sub-command a, i, or c from the command mode. To return to the command mode, press the Escape key.

  • command entry mode is the mode in which you can enter certain sub-commands that require entering additional parameters. Some of these sub-commands are the w sub-command, which requires a filename, or the / sub-command, which requires entry of a pattern. You can use the Escape key to return to command mode.

Following is a quick reference of sub-commands that can be used in the command mode for moving within the same line:

  • h to move the cursor left to the previous character in the same line.

  • l to move the cursor right to the next character in the same line.

  • j to move the cursor down to the next line in the same column.

  • k to move the cursor up to the previous line in the same column.

  • w to move the cursor to the start of next small word in the same line

  • W to move the cursor to the start of the next big word in the same line.

  • b to move the cursor to the start of the previous small word in the same line.

  • B to move the cursor to the start of the previous big word in the same line.

  • e to move the cursor to the end of the next small word in the same line.

  • E to move the cursor to the end of the previous big word in the same line.

  • fc to move to the next character c in the same line.

  • Fc to move to the previous character c in the same line

  • tc to move the cursor to one column before the next character c in the same line.

  • Tc to move the cursor to one column after the previous character c in the same line.

  • number| to move the cursor to the specified column number.

Following is a quick reference of sub-commands that can be used in the command mode for moving across the lines:

  • + or Enter to move the cursor to the next line's first non-blank character.

  • - to move the cursor to the previous line's first non-blank character.

  • 0 to move the cursor to the first character of the current line.

  • $ to move the cursor to the last character of the current line.

  • H to move the cursor to the top line of the screen.

  • L to move the cursor to the last line of the screen.

  • M to move the cursor to the middle of the screen.

Following is a quick reference of sub-commands that can be used in the command mode for redrawing screen:

  • z- to make the current line as the last line of the screen and redraw the screen.

  • z. to make the current line as the middle line of the screen and redraw the screen.
  • Ctrl-l to redraw the screen.

  • /pattern/z- to find the next occurrence of the pattern and make that the last line of the screen

Following is a quick reference of sub-commands that can be used in the command mode for scrolling across pages:

  • Ctrl-f to move forward by one screen.

  • Ctrl-d to move forward by one-half screen.

  • Ctrl-b to move backward by one screen.

  • Ctrl-u to move backward by one-half screen.

  • Ctrl-e to scroll window down by one line.

  • Ctrl-y to scroll window up by one line.

Following is a quick reference of sub-commands that can be used in the command mode for searching patterns in the file:

  • /pattern to search for the specified pattern in the forward direction. If end of file is reached, the search wraps around.

  • ?pattern to search for the specified pattern in the backward direction. If top of the file is reached, the search wraps around.

  • n to repeat the last search in the same direction as was specified in the last search.

  • N to repeat the last search in the opposite direction of what was specified in the last search.

  • /pattern/+number to position the cursor at the specified of number lines after the line in which the pattern has been found.

  • /pattern/-number to position the cursor the specified number of lines before the line in which the pattern has been found.

  • % to find the matching braces or parenthesis.

Following is a quick reference of sub-commands that can be used to enter text in the text entry mode.(You can terminate the text entry at any time by pressing the Escape key):

  • a to start entering text after the cursor position.

  • A to start entering text at the end of the line.

  • i to start entering text before the cursor position.

  • I to start entering text before the first non-blank character in the line.

  • o to insert an empty line after the line in which the cursor is positioned.

  • O to insert an empty line before the line in which cursor is positioned.

Following is a quick reference of sub-commands that can be used to modify text from the command mode. (You can terminate the text entry at any time by pressing the Escape key):

  • cc or S to change a complete line.

  • C to change the contents of a line after the cursor position.

  • cw to change the word where the cursor is positioned.

  • dd to delete the current line.

  • D to delete the rest of the line beyond where the cursor is positioned.

  • dw to delete part of the word where the cursor is positioned.

  • J to join the contents of the next line to the end of the current line.

  • rc to replace the character at the cursor position with the character c.

  • R to overwrite the contents of the current line.

  • u to undo the last modification.

  • x to delete the character at the cursor position.

  • X to delete the character to the left of the cursor position.

  • ~ (tilde) to change uppercase letter to lowercase or vice versa.

  • . to repeat the last change.

  • << to shift the current line to the left.

  • >> to shift the current line to the right.

Following is a quick reference of sub-commands that can be used to move or copy text from one part of the file to another:

  • p to paste contents of the undo buffer (as a result of deleting or yanking) after the cursor position.

  • P to paste contents of the undo buffer (as a result of deleting or yanking) before the cursor position.

  • "bd to delete text into the named buffer b.

  • "bp to paste contents of the named buffer b.

  • yy to yank the current line into the undo buffer.

  • Y to yank the current line into the undo buffer.

  • yw to yank the word from the current cursor position into the undo buffer.

Following is a quick reference of sub-commands that can be used to save a file:

  • :w to save the changes to the original file.

  • :w filename to save the changes to the specified filename if the file filename does not exist. If you try to save an already existing file using this sub-command, you will get an error.

  • !w filename to save the changes to the specified filename if the file filename already exists.

Following is a quick reference of sub-commands that can be used to move between various files if you have invoked vi with multiple files:

  • :n to start editing the next file in the list of files specified when vi was invoked.

  • :n filenames to specify a new list of files to be edited.

Following is a quick reference of sub-commands that can be used to move between the current file and the alternate file:

  • :e filename to invoke vi with filename becoming the alternate file.

  • :e! to load the current file again. If there have been changes made to the current file, those changes are discarded.

  • :e + filename to invoke vi with filename and start editing at the end of the file rather than at the start.

  • :e + number filename to invoke vi with filename and start editing at the specified line number.

  • :e # to start editing the alternate file.

Following is a quick reference of sub-commands that can be used to add lines to the current file from other sources:

  • :r filename to read the complete filename and add it after the current line.

  • :r !command to execute the specified command and add the output after the current line.

Following is a quick reference of some of the miscellaneous sub-commands:

  • Ctrl-g to get information about the current file being edited.

  • :sh to start shell so that commands can be executed. You can return by using the exit command or Ctrl-d.

  • :!command to execute the specified command.

  • !! to re-execute the last :!command.

  • :q to quit vi. If you try to quit using this sub-command and you have made modifications to the file, UNIX will not allow you to quit.

  • :q to quit vi irrespective of any changes made to the file.


  • or :wq to save changes to the original file and exit vi.

You can use a special file called .exrc in which you can specify special vi sub-commands. To use these sub-commands in a vi session, use a : (colon) in front of the command. Some of these sub-commands are as follows:

  • Ab abb ph to abbreviate ph to abb.

  • unab abbreviation to turn the abbreviation off.

  • map m seq to map a sequence of vi commands to a character or key.

File Content Search Commands

We have seen that we can use the find command to search for filenames in a directory. For searching for a pattern in one or more files, use the grep series of commands. The grep commands search for a string in the specified files and display the output on standard output.

egrep

The egrep command is an extended version of grep command. This command searches for a specified pattern in one or more files and displays the output to standard output. The pattern can be a regular expression where you can specify special characters to have special meaning, some of which are as follows:

  • . to match any single character.

  • * to match one or more single characters that precede the asterisk.

  • ^ to match the regular expression at the beginning of a line.

  • $ to match the regular expression at the end of a line.

  • + to match one or more occurrences of a preceding regular expression.

  • ? to match zero or more occurrences of a preceding regular expression.

  • [ ] to match any of the characters specified within the brackets.

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

  • -b to display the block number at the start of each line found.

  • -c to display the count of lines in which the pattern was found without displaying the lines.

  • -f filename to specify a filename that contains the patterns to be matched.

  • -h to suppress the filenames as part of the display if more than one file is being searched.

  • -i to search, ignoring the case of the letter.

  • -l to list just the filenames in which the specified pattern has been found.

  • -n to display the relative line number before each line in the output.

  • -q to suppress all outputs.

  • -s to display error message if an error occurs.

  • -v to find lines not matching the specified pattern.

  • -w to search for specified patterns as words

  • -x to match the patterns exactly to a line

The egrep command has some special features for the patterns you can specify. The features are as follows:

  • You can specify a + (plus sign) at the end of a pattern that matches one or more occurrences of the pattern.

  • You can specify a ? (question mark) at the end of a pattern that matches zero or one occurrence of the pattern.

  • You can specify a | (vertical bar or pipe) between two patterns to match either one or both (or operator).

  • You can specify a pattern within a left and a right parentheses to group the patterns.

Examples Let us assume that we have a file called file1 whose contents are shown below using the more command:

more file1
*****  This file is a dummy file *****
which has been created
to run a test for egrep
grep series of commands are used by the following types of people
   programmers
   end users
Believe it or not, grep series of commands are used by pros and novices alike
*****  THIS FILE IS A DUMMY FILE *****

If you want to find all occurrences of dummy, use the following command:

egrep dummy file1
*****  This file is a dummy file *****

If you want to find all occurrences of dummy, irrespective of the case, use the -i flag as in the following command:

egrep -i dummy file1
*****  This file is a dummy file *****
*****  THIS FILE IS A DUMMY FILE *****

If you want to display the relative line number of the line that contains the pattern being searched, use the -n flag as in the following command:

egrep -i -n dummy file1
1:*****  This file is a dummy file *****
8:*****  THIS FILE IS A DUMMY FILE *****

If you are just interested in finding the number of lines in which the specified pattern occurs, use the -c flag as in the following command:

egrep -i -c dummy file1
2

If you want to get a list of all lines that do not contain the specified pattern, use the -v flag as in the following command:

egrep -i -v dummy file1
which has been created
to run a test for egrep
grep series of commands are used by the following types of people
   programmers
   end users
Beleive it or not, grep series of commands are used by pros and novices alike

If you are interested in searching for a pattern that you want to search as a word, use the -w flag as in the following command:

egrep -w grep file1
grep series of commands are used by the following types of people
Beleive it or not, grep series of commands are used by pros and novices alike

Notice that the search did not result in finding the pattern egrep because it contains e before the pattern grep. The use of -w flag forced egrep to search for the pattern grep delimited by spaces or newline characters.

If you want to search for a pattern that is the only string in a line, use the -x command as in the following command:

egrep -x "   end users" file1
   end users

Now, let us examine some of the special features of egrep. If you want to find out where either of two specified patterns occur, use the following command where you can use the | (vertical bar) to separate the two patterns:

egrep "(dummy|pro)" file1
*****  This file is a dummy file *****
   programmers
Beleive it or not, grep series of commands are used by pros and novices alike

In the above example, the lines that contain either the pattern dummy or pro occur are displayed. In case you are interested in searching for either pros or programmers, use the ? (question mark) at the end of the pattern as in the following command:

egrep "pro(grammer)?s" file1
   programmers
Beleive it or not, grep series of commands are used by pros and novices alike

In the above example, the pattern matches both pros and programmers due to the fact that (grammer)? matches zero or one occurrence of grammer with the zero occurrence giving pros and one occurrence giving programmers.

To search for lines containing only capital letters C, D, E or F, use regular expressions as follows:

egrep [C-F] file1
*****  THIS FILE IS A DUMMY FILE *****

fgrep

Like egrep and grep, fgrep also searches one or more files for a specified string and displays output on standard output. The fgrep command is supposed to be the faster version of the grep command but in reality may not be. Please notice that the fgrep command is used to search for a specified string and not pattern (regular expression where special characters can be used to indicate special meaning).

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

  • -b to display the block number at the start of each line found.

  • -c to display the count of lines in which the pattern was found without displaying the lines.

  • -f filename to specify filename that contains the patterns to be matched.

  • -h to suppress the filenames as part of the display if more than one file is being searched.

  • -i to search ignoring the case of the letter.

  • -l to list just the filenames in which the specified pattern has been found.

  • -n to display the relative line number before each line in the output.

  • -q to suppress all outputs.

  • -s to display error message if an error occurs.

  • -v to find lines not matching the specified pattern.

  • -w to search for specified patterns as words.

  • -x to match the patterns exactly with a line.

Examples Let us assume that we have a file called file1 whose contents are shown below using the more command:

more file1
*****  This file is a dummy file *****
which has been created
to run a test for egrep
grep series of commands are used by the following types of people
   programmers
   end users
Beleive it or not, grep series of commands are used by pros and novices alike
*****  THIS FILE IS A DUMMY FILE *****

If you want to find all occurrences of dummy, use the following command:

fgrep dummy file1
*****  This file is a dummy file *****

If you want to find all occurrences of dummy irrespective of the case, use the -i flag as in the following command:

fgrep -i dummy file1
*****  This file is a dummy file *****
*****  THIS FILE IS A DUMMY FILE *****

If you want to display the relative line number of the line that contains the pattern being searched, use the -n flag as in the following command:

fgrep -i -n dummy file1
1:*****  This file is a dummy file *****
8:*****  THIS FILE IS A DUMMY FILE *****

If you are just interested in finding the number of lines in which the specified pattern occurs, use the -c flag as in the following command:

fgrep -i -c dummy file1
2

If you want to get a list of all lines that do not contain the specified pattern, use the -v flag as in the following command:

fegrep -i -v dummy file1
which has been created
to run a test for egrep
grep series of commands are used by the following types of people
   programmers
   end users
Beleive it or not, grep series of commands are used by pros and novices alike

If you are interested in searching for a pattern that you want to search as a word, use the -w flag as in the following command:

fgrep -w grep file1
grep series of commands are used by the following types of people
Beleive it or not, grep series of commands are used by pros and novices alike

Notice that the search did not result in finding the pattern egrep because it contains e before the pattern grep. The use of -w flag forced egrep to search for grep delimited by spaces or newline characters.

If you want to search for a pattern that is the only string in a line, use the -x command as in the following command:

fgrep -x "   end users" file1
   end users

grep

The grep command can be used to search for a specified pattern in one or more files, and displays the matching output on standard output.

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

  • -b to display the block number at the start of each line found.

  • -c to display the count of lines in which the pattern was found without displaying the lines.

  • -E to indicate that the grep command behaves as the egrep command.

  • -F to indicate that the grep command behaves as the fgrep command.

  • -f filename to specify filename that contains the patterns to be matched.

  • -h to suppress the filenames as part of the display if more than one file is being searched.

  • -i to search, ignoring the case of the letter.

  • -l to list just the filenames in which the specified pattern has been found.

  • -n to display the relative line number before each line in the output.

  • -q to suppress all outputs.

  • -s to display error message if an error occurs.

  • -v to find lines not matching the specified pattern.

  • -w to search for specified patterns as words.

  • -x to match the patterns exactly with a line.

Examples Let us assume that we have a file called file1 whose contents are shown below using the more command:

more file1
*****  This file is a dummy file *****
which has been created
to run a test for egrep
grep series of commands are used by the following types of people
   programmers
   end users
Beleive it or not, grep series of commands are used by pros and novices alike
*****  THIS FILE IS A DUMMY FILE *****

If you want to find all occurrences of dummy, use the following command:

grep dummy file1
*****  This file is a dummy file *****

If you want to find all occurrences of dummy irrespective of the case, use the -i flag as in the following command:

grep -i dummy file1
*****  This file is a dummy file *****
*****  THIS FILE IS A DUMMY FILE *****

If you want to display the relative line number of the line that contains the pattern being searched, use the -n flag as in the following command:

grep -i -n dummy file1
1:*****  This file is a dummy file *****
8:*****  THIS FILE IS A DUMMY FILE *****

If you are just interested in finding the number of lines in which the specified pattern occurs, use the -c flag as in the following command:

grep -i -c dummy file1
2

If you want to get a list of all lines that do not contain the specified pattern, use the -v flag as in the following command:

grep -i -v dummy file1
which has been created
to run a test for egrep
grep series of commands are used by the following types of people
   programmers
   end users
Beleive it or not, grep series of commands are used by pros and novices alike

If you are interested in searching for a pattern that you want to search as a word, use the -w flag as in the following command:

grep -w grep file1
grep series of commands are used by the following types of people
Beleive it or not, grep series of commands are used by pros and novices alike

Notice that the search did not result in finding the pattern egrep because it contains e before the pattern grep. The use of -w flag forced egrep to search for the pattern grep delimited by spaces or newline characters.

If you want to search for a pattern that is the only string in a line, use the -x command as in the following command:

grep -x "   end users" file1
   end users

Now, let us examine some of the special features of grep. If you want to find out which lines start with capital letter A through C, use the following command:

grep "^[A-C]" file1
Beleive it or not, grep series of commands are used by pros and novices alike

In this example, the ^ (caret) indicates that the following character is searched for at the beginning of each line. In case you are interested in searching for all lines that do not start with capital letters A through F, use the following command:

grep "^[^A-F]" file1
*** his fle is a dummy file *****
which has been created
to run a test for egrep
grep series of commands are used by the following types of people
   programmers
   end users
*****  THIS FILE IS A DUMMY FILE *****

In this example, the ^ (caret) outside the [ ] searches for the following character at the beginning of the line where the ^ (caret) inside the [ ] indicates the match should be made where it does not contain A through F, thus meaning that all lines that do not have A through F at the beginning of the line will be matched.

To search for lines containing only capital letters C, D, E or F, use regular expression as follows:

grep [C-F] file1
*****  THIS FILE IS A DUMMY FILE *****

strings

The strings command can be used to search for strings in executable files where a string consists of four or more printable characters terminated by a null or newline.

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

  • -a or - to search the entire file, not just the data section.

  • -o to list each string preceded by its offset in the file (in octal).

  • -Number to specify minimum string length other than the default of 4.

Examples If you want to find the strings that exist in the strings command executable file, execute the following command in a directory that contains the command:

strings strings
|@(#)56
1.17  com/cmd/scan/strings.c, cmdscan, bos320, 9227320b 5/7/92 10:21:20
Standard input
strings.cat
/usr/mbin/strings
Usage: strings [ -a ] [ -o ] [ -# ] [ file ... ]
%7o
%7o

If you also want the offset of the strings in the executable file for the strings command, use the -o flag as follows:

strings -o strings
   6017 |@(#)56
   6027 1.17  com/cmd/scan/strings.c, cmdscan, bos320, 9227320b 5/7/92 10:21:20
   6140 Standard input
   6164 strings.cat
   6200 /usr/mbin/strings
   6224 Usage: strings [ -a ] [ -o ] [ -# ] [ file ... ]
   6314 %7o
   6330 %7o

If you want to limit you search to only, say, 15 characters or more in size in the strings command executable, execute the following command:

strings -o -15 strings
   6027 1.17  com/cmd/scan/strings.c, cmdscan, bos320, 9227320b 5/7/92 10:21:20
   6200 /usr/mbin/strings
   6224 Usage: strings [ -a ] [ -o ] [ -# ] [ file ... ]

Printing

You may have several documents that you want to print, and you may have several printers attached to your computer where you can print. Here we will discuss some of the commands that direct printing of specified documents to specified printers and find out the status of the printers. We will also discuss commands to cancel specified printing jobs.

In a UNIX system, you can have multiple printers but only one of the printers can be set up as the default printer to which all the print requests are sent if a printer name is not specified.

cancel

If you have earlier queued up requests to print one or more documents using the lp command and you wish to cancel these requests, use the cancel command. Using the cancel command, you can either cancel a specified job or all queued requests to a specified printer queue. If you are an ordinary user, cancel jobs that have your user ID only.

You can either specify one or more job ID, or a printer name with the cancel command.

Examples To cancel a job with ID 734, use the following command:

cancel 734

To cancel all queued requests that you have queued up in the printer our_printer, use the following command:

cancel our_printer

lp

To print one or more files to a specified printer, use the lp command. By default, the lp command accepts input from the standard input. If more than one file is specified, the files are printed in order of their appearance in the command. The files you are printing should exist until they are printed because the lp command does not make copies of the file while printing (unless you use the -c flag).

Following is a list of a commands that can be used with the lp command:

-c to make a copy of the file so that the file can be deleted or modified while the printing is still going on.

-dprintqueue to specify the print queue where the print request is to be directed.

-m to notify the requesting user upon successful completion of the print request by mail.

-ncopies to specify the number of copies to be printed.

-ttitle to print the specified title on the banner page.

Examples To print the file file1, execute the following command:

lp file1

In this example, file1 will be printed on the default line printer. If you want to print on our_printer that is next to you, use the -d flag in the following command:

lp -dour_printer file1

If file1 is big and you want to get notification after the print job is successfully completed, use the -m flag in the following command:

lp -m -dmain_printer file1

If you want to print multiple copies of file1 for distribution to your colleagues, use the -n flag in the following command:

lp -n15 -dour_printer file1

The above example will print 15 copies of file1 on the printer called our_printer. If you want to print a title urgent memo in the banner page, use the -t flag as in the following command:

lp -n15 -t"urgent memo" -dour_printer file1

The above example will print 15 copies of file1 on our_printer with the title urgent memo printed on the banner page.

pr

The pr command accepts input from the standard input and generates output on the standard output by default. This command formats the output into pages with name of file, date, time and page numbers. If the line length is larger than the page width, it is truncated. As the pr command formats and paginates the output, you can pipe the output of the pr command to a print command like lp to print the output.

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

  • -d generates the output with double spacing.

  • -f or -F to use a form-feed to a new page instead of a sequence of line-feed characters.

  • -h "heading" to print heading instead of the filename as header on each page.

  • -l pagelength to set the number of lines to be printed on each page to pagelength instead of the default of 66.

  • -n to specify the width of the line number to be printed in front of each line. Optionally, you can specify a character to be printed between the line number and the contents of the line.

  • -oindent to indent each line by indent columns.

  • -p to pause after each page if the output is being directed to standard output. To continue, use the Enter key.

  • -r to suppress diagnostics messages.

  • -t to suppress printing of page header and page footers.

  • -wwidth to set the width of each page to width instead of the default of 72.

  • +pagenumber to specify that the display should start at page number pagenumber instead of 1.

Examples Let us assume we have a file called file1 in the current directory, the contents of which are shown below using the more command:

more file1
This is a test file for pr command
We will use it to show the usage of various flags of pr command

The plain vanilla use of the pr command is as follows:

pr file1
Wed Dec  4 00:40:14 1996 file1 Page 1

This is a test file for pr command
We will use it to show the usage of various flags of pr command

If you want to display the output in double spacing, use the -d flag in the following command:

pr -d file1
Wed Dec  4 00:40:14 1996 file1 Page 1

This is a test file for pr command
We will use it to show the usage of various flags of pr command

If you want to print a title other than the filename, use the -h flag in the following command:

pr -h "TEST FILE FOR pr COMMAND" file1
Wed Dec  4 00:40:14 1996 TEST FILE FOR pr COMMAND Page 1

This is a test file for pr command
We will use it to show the usage of various flags of pr command

If you do not want to print the headers, use the -t flag in the following command:

pr -t file1
This is a test file for pr command
We will use it to show the usag fvrious fags of pr command

If you want to print the line numbers in front of each line and you want to print a - (hyphen) between the line number and the line, use the -n flag in the following command:

pr -n-5 file1
Wed Dec  4 00:40:14 1996 file1 Page 1

    1-This is a test file for pr command
    2-We will use it to show the usage of various flags of pr command

lpstat

You can use the lpstat command to display the current status of all line printers. If the lpstat command is executed without any flags, it displays the status of each printer with the entries queued by the lp command.

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

  • -aqueue or -cqueue or -pqueue to display status as well as information on jobs in the specified list of queue.

  • -d to display the default line printer information.

  • -oqueue or -ojobnumber to display the status of the specified queue or to display the status of the specified jobnumber.

  • -r to display status and job information of all queues.

  • -s to display summary information on all queues.

  • -t to display detailed status information on all queues.

  • -uusername to display status of print requests started by specified username.

  • -vprintername to display a list of specified printername.

Examples If you want to find out about all the printers in you system, use the lpstat command without any flags as in the following command:

lpstat | more
Queue   Dev   Status    Job Files              User         PP %   Blks  Cp Rnk
------- ----- --------- --- ------------------ ---------- ---- -- ----- --- ---
m_prt   lp0   READY
prt_01  bshde READY
prt_02  lp0   READY

If you want to get information about the default line printer, use the -d flag as in the following command:

lpstat -d
Queue   Dev   Status    Job Files              User         PP %   Blks  Cp Rnk
------- ----- --------- --- ------------------ ---------- ---- -- ----- --- ---
m_prt   lp0   READY

If you are printing file1 on printer_01, to find out about the status of the printer and the job, use the -a flag as in the following command:

lpstat -aprinter_01
Queue   Dev   Status    Job Files              User         PP %   Blks  Cp Rnk
------- ----- --------- --- ------------------ ---------- ---- -- ----- --- ---
systems lpprt READY
prt_01: prt_01 is ready and printing
prt_01: Rank   Owner      Job  Files                                 Total Size
prt_01: active testuser      735  file1                                 156486 bytes

Scheduling

UNIX gives you the ability to schedule scripts and commands for execution at a later point in time. You can specify the exact time when the command should be run. UNIX also provides a way of reporting on the scheduled jobs and removing them if you do not want to execute them.

at

The at command allows you to:

  • Schedule a command for execution at a specified time.

  • Display a list of scheduled jobs.

  • Remove jobs from the scheduled jobs list.

You can schedule jobs by specifying either the absolute time or a time relative to the current time.

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

  • -l to display a list of jobs scheduled by you.

  • -m to mail a report of successful execution of the job.

  • -t date to schedule a job to be executed at the specified date and time.

  • -r joblist to remove the jobs specified in the job list from the queue.

You will be allowed to execute the at command provided at least one of the following is true:

  • The system has at.allow and your user name appears in the at.allow file.

  • The system has at.deny and your name does not appear in the at.deny file.

The exact location of the at.allow and at.deny files depends on the UNIX system you are working with.

The at commands accepts the time, day, and relative increments in a variety of formats. Some of the formats are as follows:

  • 1830 December 4
  • 6:30 pm December 4
  • 6:30 P December 4
  • now + 2 hours
  • tomorrow 1830
  • 1830 next week
  • 1830 Tuesday next week

Examples If you want to schedule a job called my_job at 11:00 p.m. today assuming that the current time is 9:30 p.m., execute any one of the following commands:

at 2300 my_job
at 23:00 my_job
at 11:00 pm my_job
at 11:00 P my_job
at 2300 today my_job

If the time currently is 11:30 p.m., the jobs will be scheduled at 11:00 p.m. the next day.

To schedule my_job 6 hours from now, use the following command:

at now + 6 hours my_job

To schedule my_job at 6:30 p.m. next week, use the following command:

at 6:30 pm next week my_job

In the preceding example, if today is Thursday and the current time is 5:30 p.m., my_job will be scheduled for 5:30 p.m. next Thursday. If the current time is 7:30 p.m., my_job will be scheduled for 6:30 p.m. next Friday.

To list the jobs scheduled, use the -l flag as in the following command:

at -l
testuser.850519800.a       Fri Dec 13 18:30:00 1996
testuser.849858400.a       Fri Dec  6 02:46:40 1996

To remove a scheduled job, use the -r command as in the following command:

at -r testuser.850519800.a
at file: testuser.850519800.a deleted

atq

The atq command can be used to list the jobs scheduled at a later time. The jobs are displayed in the order of the time scheduled with earlier scheduled jobs displayed first.

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

  • -c to display a list of jobs in order of time at which the at command was executed to schedule the jobs.

  • -n to display the number of scheduled jobs.

Examples To list all jobs scheduled using the at command, use the following command:

atq
testuser.849915000.a       Fri Dec  6 18:30:00 1996
testuser.850519800.a       Fri Dec 13 18:30:00 1996

If you want to list all jobs scheduled by the time the corresponding at command was run rather than when the scheduled jobs are supposed to run, use the -c flag as in the following command:

atq -c
testuser.850519800.a       Fri Dec 13 18:30:00 1996
testuser.849915000.a       Fri Dec  6 18:30:00 1996

If you want to find out the number of jobs scheduled currently, use the -n flag in the following command:

atq -n
2 files in the queue

crontab

UNIX systems have a deamon running all the time that can run jobs at regularly scheduled intervals. You can specify the jobs that the crontab command will execute in a file, and the cron deamon will check it when the cron deamon is initialized or when additions or modifications are made to the file.

The entries you can make in the crontab file consist of the following fields (separated by spaces or tab character):

  • minute
  • hour
  • day (of the month)
  • year
  • day of the week
  • command

Each of these fields can have more than one discrete value (separated by commas) or a range of values or an * (asterisk) meaning all values are to be matched.

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

  • -l to list your crontab file.

  • -e to edit or create the crontab file.

  • -r to remove your crontab file.

  • -v to list the status of the crontab jobs.

Examples If you want to display the string Time to go for lunch at 12:30 pm every day, set up the following:

30 12 * * * * echo "Time to go for lunch"

If you want to execute my_job on Friday at 4:00 p.m. every week, setup the following:

0 16 * * 5 my_job

Storage

In this section we will discuss a number of commands that can be used for file management, that is, to backup files to different media, to restore files from different media, to compress files to save disk space, to uncompress files to restore, and so on.

compress

You can use the compress command to reduce the size of a file. A file created by the compress command has a .Z appended to its name. The compressed file retains the permission and time attributes of the original file.

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

  • -d to force the compress command to act as an uncompress command.

  • -c to compress the file to standard output (which can be redirected to another file) so that the original file is intact.

  • -f or -F to compress the file and overwrite the compressed file if it already exists.

  • -v to display the compression percentage.

  • -V to display the current version and compile options.

Examples To compress file1, execute the following command:

compress file1

If you want the compression statistics, use the -v flag in the following command:

compress -v file1
file1: Compression: 50.85%  -- replaced with file1.Z

cpio

You can use the cpio command to copy files to archival medium from disk or to restore from archival medium to disk. There are three major forms of the cpio command:

  • cpio -o to read standard input for path names and copy them to standard output.

  • cpio -i to read from standard input archival files and create disk files.

  • cpio -p to read standard input for path name and copy to the specified directory.

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

  • a to modify the access time of copied files to the current file.

  • B to indicate that cpio should do block I/O.

  • d to create a directory if the specified directory doe not exist.

  • f to copy files that do not match the specified pattern.

  • r to copy files interactively with the option of modifying the filename.

  • t to create a list of files without actually copying a file.

  • u to overwrite a file if it already exists.

  • v to list the filenames being copied.

Examples If you have a list of files that you want to copy to a diskette, execute the following command:

ls *.txt | cpio -ov > /dev/rfd0
file1.txt
file2.txt
55 blocks

The above example will copy all files that have an extension of .txt and will display filenames being copied.

Now if you want to list the files on the diskette, use the t and v flags as in the following command:

cpio -itv < /dev/rfd0
100644 testuser      13771 Dec 07 00:13:38 1996 file1.txt
100644 testuser      13947 Dec 07 00:13:30 1996 file2.txt
55 blocks

If you want to copy the files from the diskette and rename them while copying, use the r flag as in the following command:

cpio -ir "*.txt" < y
Rename <file1.txt>
file3.txt
Rename <file2.txt>
file4.txt
55 blocks

In the preceding example, file1.txt will be renamed to file3.txt and file2.txt will be renamed to file4.txt.

If you want to copy all files from the current directory as well as all the files under its sub-directories, use the -p flag. Additionally, you can use the d flag so that all the needed directories are created. You can execute the commands as follows:

find . -print | cpio -pd /u/testuser/cpiodir

dd

The dd command can be used to read data from standard input and copy it to the standard output after converting the data according to specified conversion parameters. Along with the data conversion, the physical attributes, such as block size, can also be modified by specifying appropriate parameters.

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

  • bs=blocksize to specify the block size of the input and output file. This overrides the ibs and obs flags.

  • if=filename to specify the input filename to be copied.

  • ibs=blocksize to specify the block size of the input file.

  • fksip=numberofeof to specify number of End-OF-File markers to be skipped in the input file before starting copy.

  • files=numberoffiles to specify the number of files to be copied such as from a tape containing multiple files.

  • count=numberofblocks to copy specified number of blocks from the input file.

  • skip=nummberofblocks to skip the specified number of blocks in the input file before starting copy.

  • of=filename to specify the output filename to be created.

  • obs=blocksize to specify the block size of the output file.

  • seek=recordnumber to specify the record number in the output file to start copying the input file to.

  • conv=conversionparameter to specify the type of conversion to be used. Some of the values of this parameter can be ASCII, EBCDIC, block, unblock, lcase, ucase.

Examples If you have a file from a system that stores data in EBCDIC and you want to convert the data to ASCII, use the following command:

dd if=file1 of=file2 conv=ascii

The above command will read file1 and convert each character of this file to ASCII and copy them to file2.

If you want to copy file1 on disk to a tape with a block size of 1024, use the following command:

dd if=file1 of=/dev/rmt0 bs=1024 conv=sync

If you want to copy the third file on a tape to a file called file1, use the following command:

dd if=/dev/rmt0 fskip=2 of=file1

If you want to print a memo in capital letters, use the following command to convert file1 to file2 and then print file2:

dd if=file1 of=file2 conv=ucase
lp -dmain_printer file2

pack

If you want to save space, use the pack command to compress a file in a way similar to the compress command. The pack command compresses a file and generates a new file with .z appended to the filename. The original file is removed. The amount of space saved depends on the contents of the file. Usually you can get about 30 percent to 50 percent compression for text files. By default, the pack command will not compress if it cannot reduce the size of the file.

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

  • - to display statistics about compression.

  • -f to force packing.

Examples If you have a file called file1 that you want to compress, use the following command:

pack file1
pack: file1: 41.7% Compression

If you want more information about the compression, use the - (hyphen) flag in the following command:

pack - file1
pack: file1: 41.7% Compression
        from 28160 to 16404 bytes
        Huffman tree has 15 levels below root
        102 distinct bytes in input
        dictionary overhead = 124 bytes
        effective  entropy  = 4.66 bits/byte
        asymptotic entropy  = 4.62 bits/byte

In some cases, pack may not compress the file and will give you an error as in the following command:

pack file1
pack: file1: no saving
        - file unchanged

In such a case, to force compression, use the -f flag as in the following command:

pack -f  file1
pack: file1: 40.8% Compression

pcat

The pcat command can be used to uncompress a file to the standard output. This command does not have any flags.

Examples If you want to uncompress a file called file1.z that you have earlier created using pack command on file1, use the following command:

pcat file1

You can also use

pcat file1.z

tar

The tar command is used to copy files from disk to an archival medium (usually tape) or vice versa. The tar command does not provide any recovery from tape errors.

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

  • -c to create a new archive and write the file details at the beginning of the archive.

  • -t to generate a list of files in the archive.

  • -x to obtain one or more file from an archive. If you specify a directory name, all files in the directory are extracted. If no file or directory is specified, all files in the specified archive are extracted. If the one or more files extracted do not exist, they are created with the original user ID if you have root authority; otherwise, they are created with your user id.

  • -m to use the time of extraction from the archive as the modification time of the extracted file.

  • -p to restore the files with their original permission ignoring the current setting of the umask.

  • -f archive to use the specified archive as the archive name instead of the system default.

  • -v to display the name of each file as it is processed.

Examples If you want to extract all files in the /u/testuser directory from the archive file on the /dev/rmt1 tape device, use the following command:

tar --xvf /dev/rmt1 /u/testuser

If you want to archive a file to an archive on the default tape drive, use the following command:

tar -c file1

uncompress

The uncompress command can be used to uncompress a file that has earlier been compressed using the compress command. By default, the uncompress command uncompresses a file in place; that is, the compressed file is deleted and the uncompressed file without the .Z suffix is created in its place. The uncompressed file retains the permission and modification time attributes of the compressed file, but the user and the group of the file are changed to that of the user uncompressing the file.

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

  • -f or -F to force the uncompress even though a file by the name of the uncompressed file may already exist.

  • -c to uncompress the specified by file to the standard output retaining the original compressed file.

  • -v to display a message with the uncompressed filename.

  • -q to suppress display of compression statistics from the uncompress command.

Examples If you want to uncompress file1.Z, use either of the two following commands:

uncompress file1

or

uncompress file1.Z

If you want to uncompress file1.Z to standard output retaining the original compressed file, use the -c flag in the following command:

uncompress -c file1

unpack

The unpack command can be used to uncompress files that have been compressed using the pack command and have the .z extension. The uncompressed file is created at the same place as the compressed file and the compressed file is removed. The uncompressed file retains attributes, such as the user, group, permission, access and modification time of the compressed file. The unpack command will not uncompress the file if a file by the name of the uncompressed file already exists.

If you want to uncompress file1.z, use either of the following two commands:

unpack file1

or

unpack file1.z

zcat

The zcat command can be used to uncompress a file (that has been compressed using the compress command) to the standard output, retaining the compressed file. You can redirect the standard output to another file to get an expanded version of the compressed file. This command works the same way as the uncompress command with the -c flag.

Examples If you want to create a copy of the uncompressed version of a file without destroying the compressed file, use the following command:

zcat file1.Z > file2

The above example will create file2, which is uncompressed version of file1.Z at the same time retaining file1.Z.

Status Commands

In this section we will discuss several commands that display the status of various parts of the system. These commands can be used to monitor the system status at any point in time.

date

You can use the date command to display the current date and time in a specified format. If you are root user, use the date command to set the system date.

To display the date and time, you must specify a + (plus) sign followed by the format. The format can be as follows:

  • %A to display date complete with weekday name.

  • %b or %h to display short month name.

  • %B to display complete month name.

  • %c to display default date and time representation.

  • %d to display the day of the month as a number from 1 through 31.

  • %D to display the date in mm/dd/yy format.

  • %H to display the hour as a number from 00 through 23.

  • %I to display the hour as a number from 00 through 12.

  • %j to display the day of year as a number from 1 through 366.

  • %m to display the month as a number from 1 through 12.

  • %M to display the minutes as a number from 0 through 59.

  • %p to display AM or PM appropriately.

  • %r to display 12-hour clock time (01-12) using the AM-PM notation.

  • %S to display the seconds as a number from 0 through 59.

  • %T to display the time in hh:mm:ss format for 24 hour clock.

  • %U to display the week number of the year as a number from 1 through 53 counting Sunday as first day of the week.

  • %w to display the day of the week as a number from 0 through 6 with Sunday counted as 0.

  • %W to display the week number of the year as a number from 1 through 53 counting Monday as first day of the week.

  • %x to display the default date format.

  • %X to display the time format.

  • %y to display the last two digits of the year from 00 through 99.

  • %Y to display the year with century as a decimal number.

  • %Z to display the time-zone name, if available.

Examples If you want to display the date without formatting, use date without any formatting descriptor as follows:

date
Sat Dec  7 11:50:59 EST 1996

If you want to display only the date in mm/dd/yy format, use the following commands:

date +%m/%d/%y
12/07/96

If you want to format the date in yy/mm/dd format and time in hh:mm:ss format, use the following command:

date "+%y/%m/%d %H:%M:%S"
96/12/07 11:57:27

Following is another way of formatting the date:

date +%A","%B" "%d","%Y
Sunday,December 15,1996

If you want the Julian date, use the following command:

date +%j
350

If you want to find the week number for the current week, you have two options, the W and U, as shown in the following commands:

date +%W
49
date +%U
50

env

The env command can be used to display the current environment or change one or more of the environment variables and run a specified command. The changes are effective only during the execution of the command.

Following is a is an optional flag that you can use with the env command:

  • -i to indicate that only the variables setup as part of the env command are used for the specified command and all the current variable setups are ignored.

Examples If you want to display the current environment, use the following command:

env

Let us assume that we have a script called my_job that displays the current setting of the variable called LANG.

If you execute the script my_job as part of the env command without modifying the LANG variable, you will get the following result:

env PATH=/u/testuser/jobs:$PATH my_job
LANG = C

If you modify the LANG variable as part of the env command, you will get the following result:

env LANG=C++ PATH=/u/testuser/jobs:$PATH my_job
LANG = C++

If you use the -i flag and do not modify LANG as part of the env command, that variable is not available to my_job and you will get the following result:

env -i PATH=/u/testuser/jobs:$PATH my_job
LANG =

iostat

The iostat command can be used to obtain statistics about CPU, disks and TTY for a system. The first time you run iostat after the most recent booting of the system, the iostat provides the statistics since then. After that, iostat provides statistics since the last execution of the iostat command.

The iostat command displays the following details:

  • TTY and CPU header

  • TTY and CPU statistics detail

  • Physical volume header

  • One line for each physical volume

Following is a list of data items displayed for TTY and CPU statistics:

  • tin displays the number of characters read by the system for all TTYs.

  • tout displays the number of characters written by the system for all TTYs.

  • %user displays the utilization percentage of the CPU at the application level.

  • %system displays the utilization percentage of the CPU at the system level.

  • %idle displays the utilization percentage of the CPU while it was idling (this represents the unused utilization of the CPU).

  • %iowait displays the idling percentage of the CPU while waiting for the i/o request.

Following is a list of data items displayed as part of the physical volume utilization:

  • %tm_act displays the active utilization percentage of the physical volume.

  • Kbps displays the number of kilobytes transferred per second to or from the physical volume.

  • tps displays the number of physical i/o requests to the physical volume.

  • msps displays average number of milliseconds required for each seek of the physical volume.

  • Kb_read displays the number of kilobytes read from the physical volume.

  • Kb_wrtn displays the number of kilobytes written to the physical volume.

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

  • -d to display only physical volume utilization report. This cannot be used with -t flag.

  • -t to display only TTY and CPU utilization report. This cannot be used with the -d flag.

Examples If you want to display only the TTY and CPU utilization, use the -t flag as in the following command:

iostat -t

tty:      tin         tout      cpu:   % user    % sys     % idle    % iowait
          0.5         78.7              32.6     25.2       35.7       6.4

If you want only the utilization of physical volume of disk1, use the -d flag as in the following command:

iostat -d disk1

Disks:       % tm_act     Kbps      tps    Kb_read   Kb_wrtn
disk1            6.7       4.3       5.0    2339721   4048758

sar

You can use the sar command to report on system information. The sar command allows you to save the information and report on it. By default, the sar command generates the CPU utilization reports, but you can use various flags to collect information about other system activities.

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

  • -A to report data on all system activities.

  • -a to report data on the usage of file system access routine.

  • -b to report buffer activities.

  • -c to report system calls like forks, execs, and so on.

  • -e optionally followed by time in hh:mm:ss format to specify the time when the data accumulation should be terminated.

  • -f file to extract data from the specified file.

  • -i seconds to extract data from the file for closest time closest in seconds.

  • -k to report on kernel activity.

  • -m to report on semaphore and message activities.

  • -o file to save the activity data in the specified file.

  • -r to report on paging statistics.

  • -s optionally followed by time in hh:mm:ss format to specify the time to start the data accumulation.

  • -v to report on process and i-node activity.

  • -y to report on TTY activity.

uname

The uname command displays details about the operating system and computer system on the standard output. You can use certain flags to set the system name.

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

  • -m to display the machine ID.
  • -r to display the release number of the operating system.
  • -s to display system name.
  • -v to display operating system version.
  • -S name to modify the system name.
  • -a to display the machine id, release number of operating system, and system name.

Examples If you want to display details about the hardware and operating system, you can use the -a flag as in the following command:


uname -a
AIX main_system 2 3 000010000526

In the above example, the information displayed is as follows:

Operating system name AIX
machine name main_system
Operating system release number 2
Operating system version number 3
machine is 000010000526

uptime

The uptime command displays the following information:

  • The current time

  • The length of time the system has been up

  • The number of users currently logged one

  • The number of jobs executing in the system

vmstat

The vmstat command can be used to get information about the processes, virtual memory, physical volumes and CPU activity. The information includes the utilization of CPU, virtual memory, and physical volume, which can be used to monitor the load on the system.

The first invocation of vmstat displays the statistics since the system startup, and subsequent invocations display statistics since the last invocation. You can specify a count and an interval parameter to control the number of reports generated and interval between the reports.

The details displayed by vmstat are as follows:

  • Processes

  • Virtual memory

  • Page

  • Faults

  • CPU

The details displayed for the processes are as follows:

  • r displays the number of processes that are placed in the queue ready to execute.

  • b displays the number of processes that are placed in the queue waiting for execution.

The details displayed for the memory are as follows:

  • avm displays the number of pages being consumed (the pages are from the page space).

  • fre displays the number of pages in the free list.

The details displayed for page are as follows:

  • re displays the number of page reclaims per second observed in the specified interval.

  • pi displays number of pages brought in from page space in the specified interval.

  • po displays the number of pages swapped out to page space in the specified interval.

  • fr displays the number of pages freed in the specified interval.

  • sr displays the number of page examined, to determine whether they can be freed, in the specified interval.

  • cy displays the number of clock revolutions per second.

The details displayed for faults are as follows:

  • in displays the number of interrupts per second in the specified interval.

  • sy displays the number of system calls per second in the specified interval.

  • cs displays the number of context switches per second in the specified interval.

The details displayed for CPU are as follows:

  • us displays the percentage utilization of CPU at the application during the specified interval.
  • sy displays the percentage utilization of CPU at the system during the specified interval.
  • id displays the percentage utilization of CPU idling during the specified interval without any i/o wait.
  • wa displays the percentage utilization of CPU idling during the specified interval due to disk i/o requests.

You can specify up to four physical volume names to get the number of transfers that occurred in those disks in the specified interval.

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

  • -s to display the statistics since the system initialization.

Examples If you want to display the statistics five times intervals of five seconds, execute the following command:

vmstat 5 5
procs    memory             page              faults        cpu
----- ----------- ------------------------ ------------ -----------
 r  b   avm   fre  re  pi  po  fr   sr  cy  in   sy  cs us sy id wa
 1  0 44036   120   0   0   0 125  275   0 366 1458 391 33 25 36  6
 1  0 44036   120   0   0   0 542  938   0 446 4932 246 65 24  0 12
 1  0 44036   121   0   0   0 624 1116   0 453 5848 259 64 25  0 11
 1  0 44037   124   0   0   0 512 1010   0 434 4812 266 59 25  0 16
 0  0 44037   121   0   0   0 564 1109   0 426 4838 265 64 24  0 11

Text Processing

UNIX provides several commands to process the contents of a text file.

cut

You can use the cut command to extract data from each line of a text file. This command can be used from a file that contains data records so that each line consists of one or more fields separated by tab characters.

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

  • -ccharacterlist to specify a list of characters to be cut from each line.

  • -ffieldlist to specify a list of fields to be cut from each line. You can additionally specify a flag -dcharacter to override the character to be interpreted as the field delimiter. You can also specify flag -s to suppress lines that do not have the specified delimiter character.

Examples Let us assume that we have a file called file1 whose contents are as follows:

more file1
Misty     Ghosh
Saptarsi        Guha
Sanjiv  Guha

In this file, the fields are separated by tab characters.

If you want to extract the first field, use the following command:

cut -f1 file1
Misty
Saptarsi
Sanjiv

If you want to cut the characters 2 to 6, use the following command:

cut -c2-5 file1
isty
apta
anji

If you want to cut all characters in the first field up to the first s character, use the following command:

cut -d"s" -f1  file1
Mi
Saptar
Sanjiv  Guha

You will notice that the third line is cut completely. To suppress lines that do not contain the s character, use the -s flag as in the following command:

cut -d"s" -s -f1  file1
Mi
Saptar

ex

The ex command invokes the ex editor to edit one or more files.

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

-c subcommand to perform the specified subcommand on the specified file before invoking the ex command.

  • -R to disallow updating the file.

  • -wsize to set the window to number of lines equal to size

  • -v to invoke the vi editor.

  • -r file to do the recovery on the specified file.

Once you are in the ex editor, you can use the following subcommands to move around in the file and edit the file:

  • z to invoke full screen mode.

  • u to undo the last change made.

  • n to move to the next file if you have invoked ex editor with multiple files.

  • /pattern/ to find a pattern in the file.

  • d to delete one or more lines.

  • a to append.

The ex operates in the following modes:

  • command mode: When the ex editor starts, it is in command mode which is a : (colon) prompt where you can enter a sub-command.

  • text input mode: In this mode, you can add or change text in the file. You can enter text using the a, i or c sub-commands. The use of these sub-commands will allow you to enter text in the buffer. You can return to the command mode by entering a . (a single period) as the first character of the text buffer.

fmt

The fmt command can be used to format files to a 72-character line by default. The fmt command preserves the blank lines in the input file as well as the spacing between words. You can modify the line length using the -Width flag.

Examples Let us assume that we have file1 whose contents are shown below:

more file1
This is a test file for fmt

The fmt command     formats a file
for mail command

Notice that we have a blank line in the file and the spacing between command and formats on the third line is more than one character. Let us now format file1 using the fmt command to create file2 as in the following command:

fmt file1 > file2

Now let us see the contents of file2 using the more command as follows:

more file2
This is a test file for fmt

The fmt command     formats a file for mail command

In the above file, the blank line and inter-word spacing have been preserved.

fold

The fold command can be used to generate multiple lines from a single line by splitting the line at the specified position. By default, the line length is 80 bytes. A newline character is inserted at the end of the line.

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

  • -b to specify the position in bytes.

  • -s to split a line after the last space at a position that is less than or equal to the specified width.

  • -w width to specify the line width.

Examples Let us assume that we have file1 containing one line of 129 characters which is shown below:

more file1
The fold command can be used on files which have line lengths more than 80 bytes
, it breaks the line into multiple 80 byte lines

If you want to split the line at byte position 40, use the following command:

fold -w 40  file1 > file2; more file2
The fold command can be used on files wh
ich have line lengths more than 80 bytes
, it breaks the line into multiple 80 by
te lines

In the above example, the split happens in the middle of words. If you do not want to split words, use the -s flag as in the following command:

fold -w 40 -s  file1 > file2; more file2
The fold command can be used on files
which have line lengths more than 80
bytes, it breaks the line into multiple
80 byte lines

join

The join command can be used to merge two files (one can be standard input) to create a third file (can be standard output). Each line in the file is merged on the basis of a field that has the same value in both input files to create one line in the output file. The fields in each file are separated by either a space or tab character.

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

  • -1 field or -j1 field to specify that the join should be made on the basis of the field in the first file.

  • -2 field or -j2 field to specify that the join should be made on the basis of the field in the second file.

  • -e string to specify that blank fields in the output file be replaced by the specified string.

  • -o fileid.fieldnumber to specify that the output should consist of the specified fields. You can specify multiple fields separating them by commas.

  • -t character to modify the field separator character from the default value of space.

  • -a fileid to generate output line for each line in the file specified by fileid parameter for lines that cannot be matched to the lines in the other file using join field. The output lines are produced in addition to the default output.

  • -v fileid to generate output line for each line in the file specified by fileid parameter for lines that cannot be matched to the lines in the other file using join field. The default output is not produced.

Examples Let us assume we have two files, file1 and file2, whose contents are shown as follows:

more file1
computer1 16MB 1.2GB 17inch CDROM
computer2 8MB 840MB 14inch
computer3 12MB 1.6GB 17inch
computer4 4MB 270MB 14inch

more file2
computer1 1stfloor office5
computer3 2ndfloor office9A
computer4 1stfloor office2
computer5 3rdfloor office1

If you want to join the two files and display only the matching lines, execute the following command:

join file1 file2
computer1 16MB 1.2GB 17inch CDROM 1stfloor office5
computer3 12MB 1.6GB 17inch 2ndfloor office9A
computer4 4MB 270MB 14inch CDROM 1stfloor office2

If you want to join the two files and display the matching lines as well as the nonmatching lines from the specified file, use the -a flag in the following command:

join -a1 file1 file2
computer1 16MB 1.2GB 17inch CDROM 1stfloor office5
computer2 8MB 840MB 14inch
computer3 12MB 1.6GB 17inch 2ndfloor office9A
computer4 4MB 270MB 14inch CDROM 1stfloor office2

The above example displays the line with computer2 from file1 because it does not have a matching line in file2. If you want to display only the lines that do not match lines from the specified file, use the -v flag in the following command:

join -v2 file1 file2
computer5 3rdfloor office1

The above example displays the line with computer5 from file2 because it does not have a matching line in file1.

If you want to display only certain fields from the input files to the output file, use the -o flag as in the following command:

join -o 1.1 2.2 2.3 1.5 file1 file2
computer1 1stfloor office5 CDROM
computer3 2ndfloor office9A
computer4 1stfloor office2 CDROM

In the above example, the line with computer3 is displayed with one field short because that field is not present in the input file. You can insert a fixed legend in the empty field in the output by using the -e flag in the following command:

join -o 1.1 2.2 2.3 1.5 -e"NO CDROM" file1 file2
computer1 1stfloor office5 CDROM
computer3 2ndfloor office9A NO CDROM
computer4 1stfloor office2 NO CDROM

paste

The paste command can be used to paste lines from one or more files (one of them can be a standard input) to the standard output, which can be redirected to a file. The paste command concatenates the line from each input file to the output file separating them by the tab character (default).

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

  • -dlist to specify characters that will be used to separate corresponding lines from the input files in the output file. You can specify multiple characters if you have multiple input files.

  • -s to merge subsequent lines from input file for each input file, one at a time, separated by the specified delimiter character.

Examples Let us assume that we have two files, file1 and file2, whose contents are shown below:

more file1
computer1 16MB 1.2GB 17inch CDROM
computer2 8MB 840MB 14inch
computer3 12MB 1.6GB 17inch
computer4 4MB 270MB 14inch

more file2
computer1 1stfloor office5
computer3 2ndfloor office9A
computer4 1stfloor office2
computer5 3rdfloor office1

If you want to merge file1 and file2, use the following command:

paste file1 file2
computer1 16MB 1.2GB 17inch CDROM       computer1 1stfloor office5
computer2 8MB 840MB 14inch      computer3 2ndfloor office9A
computer3 12MB 1.6GB 17inch     computer4 1stfloor office2
computer4 4MB 270MB 14inch      computer5 3rdfloor office1

The lines from file1 and file2 are separated by tab characters.

If you want to modify the default separator from the tab character to, say, / (slash), use the -d flag in the following command:

paste -d"/" file1 file2
computer1 16MB 1.2GB 17inch CDROM/computer1 1stfloor office5
computer2 8MB 840MB 14inch/computer3 2ndfloor office9A
computer3 12MB 1.6GB 17inch/computer4 1stfloor office2
computer4 4MB 270MB 14inch /computer5 3rdfloor office1

If you want to merge the lines from within each input file, use the -s flag in the following command:

paste -d"/" -s file1 file2
computer1 16MB 1.2GB 17inch CDROM/computer2 8MB 840MB 14inch/computer3 12MB 1.6G
B 17inch/computer4 4MB 270MB 14inch
computer1 1stfloor office5/computer3 2ndfloor office9A/computer4 1stfloor office
2/computer5 3rdfloor office1

sort

The sort command is used to sort one or more files in the specified order by the specified key. It can also be used to merge files that have already been sorted. When more than one file is used, the sort command concatenates these files before sorting according to specifications.

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

  • -kkey to specify the key on which to sort. The specification for the key includes the starting field and column position and end field and column position.

  • -A to specify that sorting be done according to ASCII collating sequence.

  • -c to check whether the specified files are sorted according to the specified key and order.

  • -d to sort according to dictionary order.

  • -f to change all letters to uppercase before the sort.

  • -i to ignore nondisplayable characters for comparison.

  • -m to merge pre-sorted input files.

  • -n to sort according to numeric value.

  • -ofile to redirect the output to the specified file instead of standard output.

  • -r to sort the output in the reverse order of the specified order.

  • -u to create only one line in the output for lines that sort identically.

Examples Let us assume that we have a file called file1 whose contents are shown below:

more file1
disk drive
memory
video memory
monitor
[tape drive]
CD-ROM
3.5inch diskette
modem
monitor
sound blaster

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

sort file1
3.5inch diskette
CD-ROM
[tape drive]
disk drive
memory
modem
monitor
monitor
sound blaster
video memory

If you want to sort in the reverse order, use the -r flag in the following command:

sort -r file1
video memory
sound blaster
monitor
monitor
modem
memory
disk drive
[tape drive]
CD-ROM
3.5inch diskette

If you want to sort according to alphabetic order, use the -d flag in the following command:

sort -d file1
3.5inch diskette
CD-ROM
disk drive
memory
modem
monitor
monitor
sound blaster
[tape drive]
video memory

In the above example, the line [tape drive] is sorted as tape drive because the [ and ] are ignored due to the -d flag.

If you want only one line to be retained in case more than one line are sorted equally, use the -u flag in the following command:

sort -u file1
3.5inch diskette
CD-ROM
[tape drive]
disk drive
memory
modem
monitor
sound blaster
video memory

In the above example, the line monitor appears only once, although there are two such entries in the file, due to use of the -d flag.

If you want to sort file1 according to the uppercase letter sort order, use the -f flag as in the following command:

sort -f file1
3.5inch diskette
CD-ROM
disk drive
memory
modem
monitor
monitor
sound blaster
video memory
[tape drive]

tr

You can use the tr command to translate or delete characters from standard input to generate standard output. Following is the detail of the main functions of the tr command:

  • Translate characters specified input to a new specified character in the output.

  • Delete specified characters input from the input to generate the output.

  • To delete all but the first occurrence of the specified characters.

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

  • -c to translate all but the specified characters by the specified new character.

  • -d to delete the specified characters.

  • -s to delete all but the first occurrence of the specified characters.

You can specify the input and output sequence of characters in certain special ways as follows:

  • [character1-character2] to specify a range of characters including character1 and character2.

  • [character*number] to specify number occurrences of character.

  • [character*] to specify the use of as many as are needed occurrences of character so that the input string of characters to be translated matches the output characters to be translated to.

  • [:characterlist:] to specify a list of characters as input or output string. The characterlist can be upper, lower, alpha, space, digit, and so on.

Examples Let us assume that we have file1 whose contents are shown as follows:

more file1
"this       is a test file
for tr command"
"it has 4 lines
but should be 1 line"

If you want to change the double quotes to spaces use the following command:

tr '\"' ' ' < file1
 this       is a test file
for tr command
 it has 4 lines
but should be 1 line

If you want to change all lowercase letters to uppercase letter, use the following command:

tr [:lower:] [:upper:] < file1
"THIS       IS A TEST FILE
FOR TR COMMAND"
"IT HAS 4 LINES
BUT SHOULD BE 1 LINE"

If you want to delete all the newline characters from this file, use the -d flag in the following command:

tr -d '\n' < file1
"this       is a test file for tr command""it has 4 lines but should be 1 line"

If you want to delete all but first occurrence of a space and replace the space by a - (hyphen) use the -s flag in the following command:

tr -s ' ' '-' < file1
"this-is-a-test-file-
for-tr-command"
"it-has-4-lines-
but-should-be-1-line"

uniq

The uniq command can be used to eliminate duplicate adjacent lines from a file or from standard input to generate standard output or another file. This is the default operation. However, it is possible to compare only part of a line for comparison by using certain flags.

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

  • -c to precede each line with a number while displaying the output (the number specifies the number of occurrence of the line in the input file).

  • -d to display only the lines that occur multiple times adjacent to each other in the input file.
  • -u to display only the lines that appear only once in the input file.

  • -s numberofcharacter or +numberofcharacters to specify the number of characters from the start of a line that will be ignored while comparing adjacent lines.

  • -numberoffields or -f numberoffields to specify the number of fields from the start of a line that will be ignored while comparing adjacent lines.

Examples Let us assume that we have file1 whose contents are displayed below:

more file1
This is line 1
This is line 1
This is line 2
This is line 3
THIS IS line 3
This is line 4

If you want to find out unique lines in file1, use the following command:

uniq file1
This is line 1
This is line 2
This is line 3
THIS IS line 3
This is line 4

In the above example, the first line has been dropped because it is identical to the second line. If you want to display only the duplicate lines use the -d flag in the following command:

uniq -d file1
This is line 1

If you want to display the lines that appear only once in file1, use the -u flag in the following command:

uniq -u file1
This is line 2
This is line 3
THIS IS line 3
This is line 4

In the above example, the first two lines have not been displayed because they are identical. If you want to skip the first two fields while comparing adjacent lines, use the -f flag in the following command:

uniq -f 2 file1
This is line 1
This is line 2
This is line 3
This is line 4

sed

You can use the sed command to edit a file using a script. In the script, you can specify commands to edit one or more lines according to rules specified as part of one or more commands.

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

  • -e command to use the specified sed command to edit the file.

  • -f filename to use the filename as the editing script to edit the file.

  • -n to suppress messages from sed.

The sed command uses two different areas while performing editing:

  • pattern area to hold selected lines for editing.

  • hold area to temporarily hold the lines.

The sed sub-commands can affect either all of the lines or only the specified lines.

Following is a list of some of the sub-commands that can be used with the sed command:

  • # to specify start of comments. Everything in a line following the # is treated as comments.

  • :label to specify an addressable label that can be used in the script.

  • [/pattern/]= to write to output the line number of each line that contains the specified pattern.

  • [address]a\textstring to append textstring to each lines specified by the address.

  • [address1][,address2]c\textstring to replace the lines in the specified address range with the textstring.
  • [address1][,address2]d to delete the lines in the specified address range.

  • [address]i\textstring to insert textstring before each specified line.

  • [address1][,address2]p to print the lines in the specified address range.

  • [address1][,address2]n to specify that the current line be displayed and the next line be made the current line.

  • [address1][,address]N to specify that the current line be appended to the contents of the pattern area separated by a newline character.

  • [address]q to exit when the specified address is encountered.

  • [address1][,address2]s/old pattern/new pattern/[flag] to specify that change the old pattern by new pattern in the specified range. The behavior of the replacement can be modified by specified flags.

  • [address1][,address2]w file to write the contents of the specified range to the specified file.

  • [address1][,address2]y/old character list/new character list/ to modify each character in the old character list by the corresponding character in the new character list.

The above sub-commands are the ones that affect the pattern area used by the sed command. Now let us look at some of the sub-commands that affect the hold area:

  • [address1][,address2]g to copy the contents of the hold area to the pattern area which then become the new content of the pattern area.

  • [address1][,address2]G to append the contents of the hold area to the pattern area following the specified address.

  • [address1][,address2]h to copy the contents of the pattern area to the hold area which then become the new contents of the hold area.

  • [address1][,address2]H to append the contents of the pattern area to the hold area following the specified address.

  • [address1][,address2]x to exchange the contents of pattern and hold area.

Examples Let us assume that we have file1 whose contents are displayed below:

more file1
This file is a test file for sed command
----------------------------------------
The sed command is used for stream editing files
------------------------------------------------
The sed command a number of sub-commands which may be used to do the
--------------------------------------------------------------------
editing in specified line
-------------------------

If you want to print the line numbers of the line in which a specified pattern is found, use the following command:

sed -e "/sed/=" file1
1
This file is a test file for sed command
----------------------------------------
3
The sed command is used for stream editing files
------------------------------------------------
5
The sed command a number of sub-commands which may be used to do the
--------------------------------------------------------------------
editing in specified line
-------------------------

In the above example, the line numbers are displayed for the lines containing the pattern sed. If you want to add a specified text after each specified line, use the following command:

sed -f sfile file1
This file is a test file for sed command
+++++++++++++++++++++++++++++++++
----------------------------------------
The sed command is used for stream editing files
+++++++++++++++++++++++++++++++++
------------------------------------------------
The sed command a number of sub-commands which may be used to do the
+++++++++++++++++++++++++++++++++
--------------------------------------------------------------------
editing in specified line
-------------------------

where the file sfile contains the following line:

/sed/a\
+++++++++++++++++++++++++++++++++

In the above example, a string of +s (plus signs) is printed after each line containing the string sed. If you want to delete lines containing a specified string, use the following command:

sed -f sfile file1
This file is a test file for sed command
The sed command is used for stream editing files
The sed command a number of sub-commands which may be used to do the
editing in specified line

where sfile contains the following:

/---/d

In the above example, all lines that contain the string --- will be deleted. If you want to change all occurrences of a particular string by another one, use the following command:

sed -f sfile file1
This file is a test file for sed command
++++++++++++++++++++++++++++++++++++++++
The sed command is used for stream editing files
------------------------------------------------
The sed command a number of sub-commands which may be used to do the
--------------------------------------------------------------------
editing in specified line
-------------------------

where sfile contains the following:

1,3s/----/++++/g

In the above example, all occurrences of ---- are replaced by ++++ for lines 1 through 3. If you want to insert a specified string before each line containing a specified string, use the following command:

sed -f sfile file1
++++
This file is a test file for sed command
----------------------------------------
++++
The sed command is used for stream editing files
------------------------------------------------
++++
The sed command a number of sub-commands which may be used to do the
--------------------------------------------------------------------
editing in specified line
-------------------------

where sfile contains the following:

/sed/i\
++++

In the above example, a string ++++ is printed before each line in which the string sed appears. If you want to change each occurrence of a character by another, use the following command:

sed -f sfile file1
This file is A test file for sed commAnd
++++++++++++++++++++++++++++++++++++++++
The sed commAnd is used for streAm editing files
------------------------------------------------
The sed command a number of sub-commands which may be used to do the
--------------------------------------------------------------------
editing in specified line
-------------------------

where sfile contains the following:

1,3s/-/+/g 

In the above example, each occurrence of a - (hyphen) is modified to a + (plus) and each occurrence of a is modified to A between lines 1 through 3, both inclusive. If you want to delete all lines but the ones in which the specified pattern occurs, use the following command:

sed -f sfile file1
This file is a test file for sed command
The sed command is used for stream editing files
The sed command a number of sub-commands which may be used to do the

where sfile contains the following:

/sed/!d

In the above example, the ! (exclamation mark) is used to denote that all lines except those which contain the string sed are to be processed.

Miscellaneous Commands

In this section we will discuss some of the commands available to do miscellaneous operations in UNIX.

banner

You can use the banner command to print one or more characters in large size.

Example If you want to print the word banner in large size on the standard output, use the following command:

banner banner

 #####     ##    #    #  #    #  ######  #####
 #    #   #  #   ##   #  ##   #  #       #    #
 #####   #    #  # #  #  # #  #  #####   #    #
 #    #  ######  #  # #  #  # #  #       #####
     #  #    #  #   ##  #   ##  #       #   #
 #####   #    #  #    #  #    #  ######  #    #

bc

If you want to perform simple arithmetic expression in UNIX, use the bc command. By default, all the numbers are assumed to be decimal numbers, but you can perform operations on octal or hexadecimal numbers. You can also scale the decimal numbers. The bc command accepts input first from the specified file followed by standard input. You can, however, use input redirection to accept input only from a file.

The arguments that can be used with the bc commands are as follows:

  • Variable name (one letter)
  • Variable array name (letter[expression])
  • A literal like scale

Some of the other operands that can be used are as follows:

  • + for adding
  • - for subtracting
  • / for division
  • * for multiplication
  • % for percentage
  • ++ for adding one to the preceding variable
  • -- for subtracting one from the preceding variable
  • = to assign a value
  • sqrt for square root computation
  • length for getting length of a number
  • scale for specifying the number of digits after the decimal

You can also use C program-like statements, expression, and functions. There are some special arithmetic functions you can use in bc. Some of these functions are:

  • s(x) for sine of x
  • c(x) for cosine of x
  • l(x) for log of x

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

  • -c to compile the bc program parameters but not execute them

  • -l to include the library of math functions

Examples Let us assume that we have file1, which contains the following bc command parameters:

more file1
b=5
c=10
a=b+c
a

If you want to compile the contents of file1 without executing them, use the -c flag in the following command:

bc -c < file1
 5sb
 10sc
lblc+sa
laps.
q

If you want to execute the contents of file1, use the following command:

bc < file1
15

Let us assume that we have file1 whose contents are displayed below:

a=0
j=50
for (i=1; i<=j; i++) a=i+a;
a

If we execute the bc command with this file as input, this will add all numbers from 1 through 50 and display the total as follows:

bc < file1
1275

cal

You can use the cal command to display the calendar for one or more months on standard output. If you do not specify any arguments, cal displays the calendar for the current month. You can specify the month and year for which you want to display the calendar. If you specify only one argument, cal will display a calendar for all 12 months of the specified year.

Examples

If you want to display the calendar of the current month, execute the following command:

cal
       December 1996
Sun Mon Tue Wed Thu Fri Sat
 1   2   3   4   5   6   7
 8   9  10  11  12  13  14
15  16  17  18  19  20  21
22  23  24  25  26  27  28
29  30  31

If you want to display the calendar for January, 1995, use the following command:

cal 1 1995
       January 1995
Sun Mon Tue Wed Thu Fri Sat
 1   2   3   4   5   6   7
 8   9  10  11  12  13  14
15  16  17  18  19  20  21
22  23  24  25  26  27  28
29  30  31

If you want to obtain calendars for all 12 months of 1997, use the following command:


cal 1997
                                1997

          January                     February
Sun Mon Tue Wed Thu Fri Sat  Sun Mon Tue Wed Thu Fri Sat
             1   2   3   4                            1
 5   6   7   8   9  10  11    2   3   4   5   6   7   8
12  13  14  15  16  17  18    9  10  11  12  13  14  15
19  20  21  22  23  24  25   16  17  18  19  20  21  22
26  27  28  29  30  31       23  24  25  26  27  28

           March                        April
Sun Mon Tue Wed Thu Fri Sat  Sun Mon Tue Wed Thu Fri Sat
                         1            1   2   3   4   5
 2   3   4   5   6   7   8    6   7   8   9  10  11  12
 9  10  11  12  13  14  15   13  14  15  16  17  18  19
16  17  18  19  20  21  22   20  21  22  23  24  25  26
23  24  25  26  27  28  29   27  28  29  30
30  31
            May                         June
Sun Mon Tue Wed Thu Fri Sat  Sun Mon Tue Wed Thu Fri Sat
                 1   2   3    1   2   3   4   5   6   7
 4   5   6   7   8   9  10    8   9  10  11  12  13  14
11  12  13  14  15  16  17   15  16  17  18  19  20  21
18  19  20  21  22  23  24   22  23  24  25  26  27  28
25  26  27  28  29  30  31   29  30

           July                        August
Sun Mon Tue Wed Thu Fri Sat  Sun Mon Tue Wed Thu Fri Sat
         1   2   3   4   5                        1   2
 6   7   8   9  10  11  12    3   4   5   6   7   8   9
13  14  15  16  17  18  19   10  11  12  13  14  15  16
20  21  22  23  24  25  26   17  18  19  20  21  22  23
27  28  29  30  31           24  25  26  27  28  29  30
                             31
         September                     October
Sun Mon Tue Wed Thu Fri Sat  Sun Mon Tue Wed Thu Fri Sat
     1   2   3   4   5   6                1   2   3   4
 7   8   9  10  11  12  13    5   6   7   8   9  10  11
14  15  16  17  18  19  20   12  13  14  15  16  17  18
21  22  23  24  25  26  27   19  20  21  22  23  24  25
28  29  30                   26  27  28  29  30  31

         November                     December
Sun Mon Tue Wed Thu Fri Sat  Sun Mon Tue Wed Thu Fri Sat
                         1        1   2   3   4   5   6
 2   3   4   5   6   7   8    7   8   9  10  11  12  13
 9  10  11  12  13  14  15   14  15  16  17  18  19  20
16  17  18  19  20  21  22   21  22  23  24  25  26  27
23  24  25  26  27  28  29   28  29  30  31
30

calendar

You can use the calendar command to get reminders from messages stored in a special file named calendar in the current directory. The messages are stored in the following format:

  • date message
  • message date

where date can be in a variety of formats such as:

  • March 7
  • Mar 7
  • mar 7
  • march 7th
  • 3/7
  • */7 (7th of each month)

On a Friday, the calendar command will display the messages for four days--Friday, Saturday, Sunday, and Monday.

clear

You can use the clear command to clear the screen of your workstation. This command checks the terminal type to find out the terminal type to determine how to clear the screen.

Examples To clear the screen on your terminal, use the following command:

clear

time

You can use the time command to obtain the execution time of a script, command, or program. The execution time is displayed with the following three times:

  • real
  • user
  • system

Examples If you want to find out the execution time of a script sample, use the following command:

time sample
real    0m6.49s
user    0m0.02s
sys     0m0.03s

xargs

You can use the xargs command to group multiple arguments and input them to a command. xargs passes as many arguments to the command as necessary to ensure that the maximum size limit for command line arguments is not exceeded.

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

-eendoffilecharacter to specify the character to be used to terminate the argument string. The default is _ (underline) character

-istring to use each line as a single parameter in place of the string variable specified as part of the command line. The default string is {}.

-lnumber to specify the number of non-empty lines to be used as arguments to the command for each invocation. The last invocation can use less than the specified number.

-nnumber to specify the number of arguments to be used in each invocation. The last invocation can use less than the number specified.

-p to ask for confirmation before executing the command.

-ssize to set the maximum size of the argument list for each invocation.

-t to echo the constructed command to the standard error.

Examples Let us assume that we have xfile whose contents are shown below:

more xfile
file1 file2 file3
file4 file5 file6
file7 file8 file9

If you want to pass only two arguments to the ls command at a time, use the -n flag in the following command:

xargs -n2  ls  < xfile
file1  file2
file3  file4
file5  file6
file7  file8
file9

If you want to pass two lines at a time to the ls command, use the -l flag in the following command:

xargs -l2  ls  < xfile
file1  file2  file3  file4  file5  file6
file7  file8  file9

If you want to confirm the command to be executed before actually executing the command, use the -p flag in the following command:

xargs -l2 -p ls  < xfile
sfile1file2 file3 file4 file5 file6 ?...y
file1  file2  file3  file4  file5  file6
ls file7 file8 file9 ?...y
file7  file8  file9

In the above example, you have to use the character y to confirm that the command should be executed. If you want to rename all the files that start with the name file (file1 through file9), use the -i flag following command:

ls file* | xargs -t -i cp {} {}.old
cp file1 file1.old
cp file2 file2.old
cp file3 file3.old
cp file4 file4.old
cp file5 file5.old
cp file6 file6.old
cp file7 file7.old
cp file8 file8.old
cp file9 file9.old

In the above example, the -t flag forces the display of the constructed command to the standard error.

Regular Expression

A regular expression in UNIX is a string of one or more characters and meta-characters. The commands that accept regular expressions, first have to expand it to get the specified pattern before matching it to the input. The matching is done character by character.


CAUTION: The regular expression looks similar to the file matching pattern used by some commands like the find command. But the regular expression is not same as file matching pattern.

A regular expression contains the following:

  • character set, which matches one or more characters at the specified position.

  • count, which specifies the number of the previous character to be repeated. This is an * (asterisk) to specify that zero or more of the previous character should be repeated.

  • position specifier, which is a set of special characters to indicate certain fixed positions like start of a line, end of a line, and so on.

  • meta characters to specify special meaning.

character set

A character set is a list of one or more specified characters. The character set can be specified as follows:

  • range of characters, which can be specified as two characters separated by a hyphen enclosed within square brackets. This matches one occurrence of a character within the specified range. If you specify a ^ (caret) in front of the range, the matching is reversed, that is, all characters except those in the specified range will be matched.

  • list of characters, which can be a list of individual characters enclosed within square brackets. This matches one occurrence of one of the characters in the list. You can specify a ^ (caret) in front of one or more characters to match all characters except those.

Position Specifier

UNIX allows use of a number of special characters to specify certain special positions in a line. Following is a list of these special characters:

  • ^ at the start of a regular expression to specify beginning of a line.

  • $ at the end of the regular expression to specify end of a line.

Meta Characters

A meta character is a character that, when used as part of a regular expression, has a special meaning. Following is a list of these meta-characters:

  • . to match all characters except newline.

  • * to match zero or more of the preceding characters or regular expressions.

  • ^ to match the regular expression following it at the beginning of the line (for this to work, you must specify ^ as the first character of the regular expression).

  • $ to match the regular expression preceding it at the end of the line (for this to work you must specify $ as the last character of the regular expression).

  • [ ] to match exactly one of the enclosed character. The characters enclosed can be a range or a list of individual characters.

  • \{n1,n2\} to match a minimum of n1 and a maximum of n2 occurrences of the preceding character or regular expression.

  • \ to interpret the following character as a regular character rather than a meta-character.

  • \( \) to save the enclosed regular expression for later use. These can then be reused by using \1 through \9.

  • \< to match the following regular expression at the beginning of a word.

  • \> to match the preceding regular expression at the end of a word.

  • ? to match zero or one instance of the preceding regular expression.

  • + to match one or more instance of the preceding regular expression.

Examples Let us assume that we have a file called file1 whose contents are shown below:

more file1
This is a test
THIS IS A TEST
This is really a test
Beleive it, this is really a test
This is a test, better believe it

In its simple form, you can specify a string of characters as a regular expression. If you want to find the string really in file1 use the following command:

grep really file1
This is really a test
Believe it, this is really a test

If you want to find the string THIS at the beginning of a line, use ^(caret) at the beginning of a regular expression in the following command:

grep ^THIS file1
THIS IS A TEST

If you want to find the string it at the end of a line, use $ (dollar sign) at the end of a regular expression in the following command:

grep it$ file1
This is a test, better believe it

If you want to find both believe and Believe, use the following command:

grep [Bb]elieve file1
Believe it, this is really a test
This is a test, better believe it

In the above example, [Bb] matches the single characters B or b. If you want to find characters other than the specified one, use the following command:

grep [T][^h] file1
THIS IS A TEST

In the above example, the [^h] matches anything other than h, hence the [T][^h] matches anything that starts with T and followed by any character other than h. If you want to match any six-character string preceded and followed by a space, use the following command:

grep " ...... " file1
This is really a test
Believe it, this is really a test
This is a test, better believe it

In the above example, the " ......" will match a string such as really or better preceded and followed by a space. If you want to modify all strings that start with a t and have a t at the end and have any two characters in the middle, use the following command:

sed "s/\(t\)..\1/----/g" file1
This is a ----
THIS IS A TEST
This is really a ----
Believe i----his is really a ----
This is a ----, better believe it

In the above example, \(t\) saves the character t and \1 uses the t at the specified position. If you want to find one or more instances of a regular expression, use the following command:

egrep it+ file1
Believe it, this is really a test
This is a test, better believe it

In the above example, it+ tells egrep to find one or more instances of the string it. If you want to find whether a regular expression is repeated a specified number of times, use the following command:

egrep tt\{1,4\} file1
This is a test, better believe it

In the above example, at least one, and a maximum of four, repetitions of the expression it are matched. If you wan to modify all characters other than letters, use the following command:

sed "s/[^a-zA-Z ]/:/g" file1
This is a test
THIS IS A TEST
This is really a test
Believe it: this is really a test
This is a test: better believe it

In the above example, all characters other than a through z, A through Z and spaces will be replaces by a : (colon).

Executing Commands

There are several ways to execute the commands you have learned so far. In this section, we will learn about some of the ways in which a command can be executed in isolation and in conjunction with other commands.

UNIX, by default, accepts input from standard input, which is the keyboard, and displays output on standard output, which is the terminal. You can, however, use the UNIX redirection facility to redirect the input from a file or output to a file.

You can execute a command in the foreground or in the background. When you invoke a command, by default it executes in the foreground. You can force a command in the background by using the & (ampersand) sign. You can start a command in the foreground, then force it into the background. To achieve this, you use Ctrl-z to suspend it and then use the bg command to put it in the background.

Because all UNIX commands accept input from standard input and generate output to standard output, there is a convenient way of passing output of one command to the next using the | (pipe) character. You can have a string of commands, each connected to the next using a pipe.

Summary

In this chapter you have learned about various UNIX commands. Most of these commands should work on different UNIX systems as described, but you may find that some of the commands or flags behave differently. Following is a list of some of the activities you can do using various UNIX commands:

  • Log in to related activities using commands like login, rlogin, and passwd.

  • Creating, renaming, deleting, and copying files and directories using commands like cp, rm, rmdir, and mkdir.

  • Searching for text in files using commands like grep.

  • Granting access to files and directories for users using commands like chmod and chgrp.

  • Modify contents of files using commands like vi and sed.

  • Displaying contents of files using commands like more, tail, head, and pg.

TOCBACKFORWARDHOME


©Copyright, Macmillan Computer Publishing. All rights reserved.