![]() ![]() ![]() ![]() ![]() |
UNIX Unleashed, System Administrator's Edition
- 5 -General Commandsby 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.
User Related CommandsThe 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. loginWhenyou 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. rloginUNIX 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:
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. telnetIf 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:
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. passwdAs 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:
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:
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)? > ExitWhen 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 CommandsWhen 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:
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. whichThis 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 whenceThe 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 whereThe 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 UsageUNIX 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. manThe 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:
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:
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 AdministrationThere 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. installThe 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.
The following are some of the flags that can be used for the install command:
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 shutdownThe 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:
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. ulimitThe 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:
Following are the flags that can be used with the ulimit command:
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. umaskThe 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:
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 CommandsIn 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:
In UNIX, each process has a number of attributes associated with it. The following is a list of some of these attributes:
killThe 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:
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:
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. niceAs 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 & psThe 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:
The following is a list of some of the flags for the ps command that determine which details are displayed for each process listed:
The following details are displayed if formatting flags are not used:
By using the formatting command, some of the details that can be obtained are as follows:
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 jobsIn 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 waitYou 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. nohupWhen 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 &
sleepIf 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 ....." CommunicationUNIX 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. cuThe 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:
The following arguments can be specified with the cu command:
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 ~):
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 ftpYou 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:
Once you are at the ftp> prompt, you can use several sub-commands. These sub-commands are
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. mailxIn 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:
Each mail has information associated with it. The following is a list of this information:
Following is a list of sub-commands you can use while in mail> prompt:
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. talkYou 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:
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. vacationIf 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:
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. writeThe 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:
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 ComparisonHere 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. cmpThe 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:
The flags that can be used with cmp command are
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 commIf 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:
The following is a list of flags that can be used with the comm command:
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 diffYou 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:
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:
The following is a list of some of the flags that can be used with the diff command:
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 diff3The 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:
The following is a list of flags that can be used with the diff3 command:
The format of the generated output is as follows:
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 dircmpIf 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:
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. sdiffThe 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:
The flags that can be used with sdiff command are as follows:
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 CommandsHere 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. touchThe 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 chmodYou 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:
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:
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:
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
chgrpIf 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 chownIn case you want to change the owner of a file or directory, use the chown command.
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 rmOnce 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:
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 (*).
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. mvIf 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:
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. cpThe 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:
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 catYou 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:
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 rcpSo 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:
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/* . lnSometimes 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:
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 CommandsWhen 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. mkdirTo 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:
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 rmdirOnce 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:
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 CommandsEach file and directory in UNIX has several attributes associated with it. UNIX provides several commands to inquire about and process these attributes. lsThe 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:
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:
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 findIf 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:
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 fileThe 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 CommandsHere 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. moreThe 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:
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:
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 lessThe 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:
Examples To invoke the less command for a file named file1, use the following command: less file1 tailYou 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:
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. headThe 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:
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. wcThe 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:
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. . readThe 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:
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 odThe 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:
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. pgThe 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:
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:
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 teeIf 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:
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 viThe 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.
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:
Following is a quick reference of sub-commands that can be used in the command mode for moving within the same line:
Following is a quick reference of sub-commands that can be used in the command mode for moving across the lines:
Following is a quick reference of sub-commands that can be used in the command mode for redrawing screen:
Following is a quick reference of sub-commands that can be used in the command mode for scrolling across pages:
Following is a quick reference of sub-commands that can be used in the command mode for searching patterns in the file:
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):
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):
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:
Following is a quick reference of sub-commands that can be used to save a file:
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:
Following is a quick reference of sub-commands that can be used to move between the current file and 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:
Following is a quick reference of some of the miscellaneous sub-commands:
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:
File Content Search CommandsWe 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. egrepThe 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:
Following is a list of flags that can be used with the egrep command:
The egrep command has some special features for the patterns you can specify. The features are as follows:
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 ***** fgrepLike 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:
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 grepThe 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:
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 ***** stringsThe 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:
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 ... ] PrintingYou 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. cancelIf 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 lpTo 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. prThe 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:
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 lpstatYou 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:
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 SchedulingUNIX 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. atThe at command allows you to:
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:
You will be allowed to execute the at command provided at least one of the following is true:
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:
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 atqThe 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:
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 crontabUNIX 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):
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:
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 StorageIn 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. compressYou 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:
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 cpioYou 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:
Following is a list of some of the flags that can be used with the cpio command:
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 ddThe 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:
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 packIf 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:
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 pcatThe 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 tarThe 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:
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 uncompressThe 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:
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 unpackThe 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 zcatThe 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 CommandsIn 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. dateYou 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:
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 envThe 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:
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 = iostatThe 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:
Following is a list of data items displayed for TTY and CPU statistics:
Following is a list of data items displayed as part of the physical volume utilization:
Following is a list of flags that can be used with the iostat command:
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 sarYou 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:
unameThe 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:
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:
uptime The uptime command displays the following information:
vmstatThe 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:
The details displayed for the processes are as follows:
The details displayed for the memory are as follows:
The details displayed for page are as follows:
The details displayed for faults are as follows:
The details displayed for CPU are as follows:
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:
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 ProcessingUNIX provides several commands to process the contents of a text file. cutYou 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:
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 exThe 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.
Once you are in the ex editor, you can use the following subcommands to move around in the file and edit the file:
The ex operates in the following modes:
fmtThe 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. foldThe 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:
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 joinThe 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:
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 pasteThe 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:
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 sortThe 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:
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] trYou 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:
Following is a list of some of the flags that can be used with the tr command:
You can specify the input and output sequence of characters in certain special ways as follows:
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" uniqThe 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:
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 sedYou 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:
The sed command uses two different areas while performing editing:
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:
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:
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 CommandsIn this section we will discuss some of the commands available to do miscellaneous operations in UNIX. bannerYou 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 ##### ## # # # # ###### ##### # # # # ## # ## # # # # ##### # # # # # # # # ##### # # # # ###### # # # # # # # ##### # # # # ## # ## # # # ##### # # # # # # ###### # # bcIf 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:
Some of the other operands that can be used are as follows:
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:
Following is a list of flags that can be used with the bc command:
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 calYou 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. ExamplesIf 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 calendarYou 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:
where date can be in a variety of formats such as:
On a Friday, the calendar command will display the messages for four days--Friday, Saturday, Sunday, and Monday. clearYou 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 timeYou 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:
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 xargsYou 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 ExpressionA 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.
A regular expression contains the following:
character setA character set is a list of one or more specified characters. The character set can be specified as follows:
Position SpecifierUNIX allows use of a number of special characters to specify certain special positions in a line. Following is a list of these special characters:
Meta CharactersA 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:
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 CommandsThere 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. SummaryIn 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:
|
||||||||||
|