by Sanjiv Guha
UNIX has commands that are native to UNIX, and commands that you (or someone at your installation) can write. Depending on the UNIX version you are running and the shell you are in, the commands differ. In this chapter, commands that are available in most UNIX versions are discussed. Each command can have several arguments and several flags associated with it. The general form of a command is
command [flags] [argument1] [argument2] ...
Flags are preceded by a hyphen. Several flags can be specified together with only one hyphen. For example, the following two commands are equivalent:
ls -a -l ls -al
Depending on the command, the arguments can be optional or mandatory. All commands accept inputs from the standard input, display output on standard output and display error message on standard error. You can use UNIX redirection capabilities to redirect one or more of these. Standard input is where UNIX gets the input for a command, standard output is where UNIX displays output from a command, and standard error is where UNIX displays any errors as a result of the execution of a command.
All commands, when executed successfully, return a zero return code. However, if the commands are unsuccessful or partially successful, they return non-zero return codes. The return codes can be used as part of control logic in shell scripts.
CAUTION: Though the commands described here work on most of the UNIX systems in a general way, the behavior of the flags associated with each command can differ. You should use the man command on your system to learn about details of the command and its flags.
The commands related to logging in and out of the system are discussed in the following sections. User Related Commands are those that are used to log in and out of your UNIX system. They might differ slightly from system to system, but the commands discussed here are common to most systems.
Whenyou start working on a UNIX system, the first thing you need is a login ID. Each user in a UNIX system has a unique login ID that identifies the user and any characteristics associated with the user. When you first connect to a UNIX system, you get the login prompt. The login prompt usually asks for the following information:
login: password:
You will be allowed into a system, if, and only if, you enter the login ID and password correctly. When the login is successful, you get information, such as last unsuccessful login, last successful login, whether you have any mail, messages from the system administrator, and more. Following is an example of a successful login:
******************************************************************************* * * * You are now logged on to the host1 computer * * * ******************************************************************************* Last unsuccessful login: Thu Nov 7 22:32:41 1996 on tty1 Last login: Fri Nov 8 01:17:04 1996 on tty1 /u/testuser:>>
While logging in, there are shell scripts that are executed. The script that gets executed depends on the shell you are running. For example, in Bourne shell, the .profile file in you home directory is execute, in Korn shell, the file pointed to by the environment variable ENV is executed.
You should put in as part of the startup file all the commands that you want to be executed before login. For example, you can set the search path, the terminal type, and various environment variables or run special programs, depending on your requirements. The following is an example of a Korn shell .profile file for user testuser. Here the PATH variable is being set to include directories that user testuser uses for daily work, and the mail is being checked.
PATH=$PATH:/u/testuser:/u/testuser/exe:/u/testuser/script PATH=/usr2/cdirect/local:$PATH export PATH
You can invoke the login command from the command line also. The user is optional. If user is not provided, the system prompts for login id followed by the password prompt, if needed. If user is provided, the system prompts for a password, if needed.
UNIX provides you with the rlogin command to let you move between various computers in a network. The rlogin command is similar to the telnet command described in this section.
To be allowed access to a remote host, you must satisfy the following conditions:
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.
If you are in an environment where you work with multiple UNIX computers networked together, you will need to work on different machines from time to time. The telnet command provides you with a facility to login to other computers from your current system without logging out of your current environment. The telnet command is similar to the rlogin command described earlier in this section.
The hostname argument of telnet is optional. If you do not use the host computer name as part of the command, you will be placed at the telnet prompt, usually, telnet>. There are a number of sub-commands available to you when you are at the telnet> prompt. Some of these sub-commands are as follows:
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.
As you have seen, every time you try to log in to a computer system, you are asked for your user ID and password. Although users can be set up without passwords, most users will have a password, which they will must use when logging in to a computer system.
When you first get you user ID set up in a computer, the system or security administrator will assign you a temporary password using the root user ID. The first time you try to log in, the system will ask you to change your password; you will use the new password for all subsequent logins.
However, you can change you password if, for example, you think that somebody else has come to know it. As a security precaution, it is a good practice to modify your password frequently. You can use the passwd command to change your password.
When you issue the passwd command, you are first prompted for your current password, followed by two prompts to enter your new password. The new password must match on both entries. This is a precaution to ensure that you do not type in something you did not intend and are subsequently unable to login. Your new password cannot be the same as you current password. When you are typing your password (both old and new), UNIX does not display them.
Whenever you change your password, the new password must follow the rules set up at your installation. Some of the rules that govern passwords are as follows:
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)? >
When you log in to a UNIX system, you are always placed in a shell. The shell may be the Bourne shell, C shell, Korn shell, or other. As we have seen, we can log in to other system with rlogin or telnet commands. The exit command allows you to exit the current shell.
You can also exit your current shell by typing CTRL-d. (Hold down the Ctrl key and the d key together.) If you are on the command line and press Ctrl-d, you will be logged off.
When you try to execute a command, UNIX has to locate the command before it can execute it. UNIX uses the concept of search path to locate the commands you are trying to execute. The search path is a list of directories in the order to be searched for locating commands.
The default search paths set by the installation usually have the standard directories like /bin, /usr/bin, and other installation specific directories. You can modify the search path for your environment as follows:
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.
This command can be used to find whether a particular command exists in you search path. If it does exist, which tells you which directory contains that command.
Examples To find out where the which command resides, you can execute the following command:
which which
The system responds with the following message, meaning that the command which exists in the directory /usr/bin.
/usr/bin/which
The whence command is a more verbose form of the which command in Korn shell. It has a flag -v associated with it to produce output in a verbose form. For a native command that has not been aliased, it generates output similar to which.
Examples To get information about the which command, you can execute the following command:
whence which /usr/bin/which
If you use the -v flag, you get the verbose for
which is /usr/bin/which
However, if you have aliased commands, then the output is different. For example, if you have aliased rm as rm -i (remove command with confirmation flag set), the command
which rm
generates the output
rm -i
while with the flag -v, it generates the output
rm is an alias for rm -i
The where command is used to obtain the full pathname of one or more files or directories. There are no flags associated with the where command. When the pathname is displayed, it is prefixed by the hostname, and if there are no arguments, the full pathname of the current directory is displayed.
Examples If you want to find the path name of the current directory, assuming that you are in the directory /u/testuser, the following command:
where
will result in
box1:/u/testuser
UNIX provides you with online help to learn about various commands and their options and flags. You may be familiar with the most often used commands and their various options, but to find out about less popular commands or command usage, you can use the online help provided by UNIX.
The man command is used to display the online UNIX manual pages, which include commands, files, sub-routines, and so on. You have to provide the man command with the name of the object you are looking for. If you do not know the full name, you can use the UNIX wildcard to specify the object name. You can even find out more about the man command itself by using the man command.
The following are some of the flags and arguments that can be used for the man command:
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
There are some functions that only the UNIX system administrator at you installation can perform. These include starting the system, shutting down the system, setting up new user accounts, monitoring and maintaining various filesystems, installing new software on the system, and more. In this section we will discuss the commands necessary to fulfill these duties.
The install command is used to install new versions of current software programs or brand new software programs. The basic function of the install command is to copy the binary executable and any associated files to the appropriate directories. In the case of new versions of existing software, the files are copied over the existing files and retain their permissions. The install command also prints a log of messages of what it has done. The system administrator can then modify the permissions and ownership defined for that installation.
NOTE: For new software, the default permissions are set to 755 with owner and group both set to bin. The default directories searched to find whether the command is present are /usr/bin, /etc, and /usr/lib in that order.
The following are some of the flags that can be used for the install command:
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
The system administrator may need to shutdown the system at the end of the day or to do hardware maintenance on the UNIX machine. To shutdown the machine in an orderly fashion, use the shutdown command. This command can be executed only with the root user authority. All users get a shutdown message when this command is executed, and they receive a final completion message. This process is not complete until all users receive the message. By default, this command brings down the system to a single user mode from a multi-user mode.
The following is a list of some of the flags that can be used with the shutdown command:
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.
The ulimit command is available in Korn shell and Bourne shell, and can be used to set limits on certain resources for each process. The corresponding command in C shell is limit. There are two types of limits:
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.
The umask command is used by the system administrator to set the default value to be assigned to each file created by a user. You, as a user, can modify this default setting.
Three groups of permissions are associated with a file or directory--owner, group, and world (sometimes referred to as others). The permissions for these three groups are assigned using octal numbers--one octal number for each group. The values for each group depend on the following three bits:
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
In UNIX, a process is a program that has its own address space. Usually, a command or a script that you can execute consists of one or more processes. Simple commands like umask have only one process associated with them, while a string of commands connected by pipes has multiple processes associated.
The processes can be categorized into the following broad groups:
In UNIX, each process has a number of attributes associated with it. The following is a list of some of these attributes:
The kill command is used to send signals to an executing process. The process must be a nonforeground process for you to be able to send a signal to it using this command.
The default action of the command is to terminate the process by sending it a signal. It is possible, however, that the process may have been programmed for receiving such a signal. In such a case, the process will process the signal as programmed. You can kill only the processes initiated by you. However, the root user can kill any process in the system.
The flags associated with the kill commands are as follows:
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.
As you have seen, a relative priority is associated with each process. The relative priority governs the resources allocated to it by the operating system. The nice command lets you modify the priority of one or more processes so that you can assign them higher or lower priority. You can increase the priority only if you have root privileges though.
A negative number signifies a higher priority than a positive number. The value is usually in the range of -20 to 20.
If you do not specify an increment, the nice command takes a value equal to or higher than the current process, provided you have appropriate privileges. If you do not have sufficient privileges, the priority is not affected at all.
Usually, you should use the nice command to lower the priority of background or batch processes for which you do not need fast turn around.
Examples If you want to find, at a lower priority in background, all the C source files in the current directory or its sub-directories, execute the following command:
nice find . -name *.c -print &
This will set the process to a default nice priority, which may be 10. To run the process at an even lower priority, execute the following command:
nice 16 find . -name *.c -print &
The following command can also be executed to achieve the same result:
nice -n 16 find . -name *.c -print &
The ps command is used to find out which processes are currently running. Depending on the options, you can find all processes or only those initiated by you user ID. This command provides you with details about the various background and batch processes running in the system. It can provide information only about the active processes.
When the ps command is executed without any flags or arguments, it lists all processes (if any) initiated from the current terminal.
Following is a list of some of the flags that determine what processes are listed by the ps command:
The following is a list of some of the flags for the ps command that determine which details are displayed for each process listed:
NOTE: Some of the flags used in the ps command are not preceded by a hyphen (-).
The following details are displayed if formatting flags are not used:
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
In UNIX, there is a subtle difference between processes and jobs. A job typically is one command line of commands, which can a single command, a shell script, or a chain of piped commands. In a chain of piped commands, each command has a unique process ID, but all have the same job ID.
The C shell and some versions of Korn and Bourne shell offer the jobs command. You can use the jobs command to find out the details about active jobs. Once you have the job ID, you can start using it to do primitive job controls.
You can use % (percent sign) in front of the job number to indicate that the number is a job number rather than a process ID.
Examples If you want to bring job number 5 from background to foreground, execute the following command:
fg %5
If you had used 5 instead of %5, UNIX would have interpreted it as the process ID 5.
If you have one job called sample_job running in the background, execute the following command:
jobs
to get the details of the job. The display looks like:
[1] + Running nohup sample_job > sample_log &
If you use the -l option, the process number will also be displayed as follows:
[1] + 270384 Running nohup sample_job > sample_log &
You need not know the job number to take any action on it. You can use the name of the job preceded by a % (percent sign) to identify the job. You can use regular UNIX wildcards to achieve this.
In the preceding example, you can use the name sample_job to identify the job. UNIX also allows you to use %\? to identify the unique part of the job name. In this example, it may be %\?job (Please note that the character \ has been used as ? has special meaning in UNIX. Specifying a \ in front of a character tells UNIX not to interpret the character as a control character.) To kill the job sample_job, use the following command:
kill %sample_job
or, you cause the following, if sample_job is the only one you are currently executing. Otherwise, it will kill all jobs with job name ending in job:
kill %\?job
You can use the wait command to wait for completion of jobs. This command takes one or more process IDs as arguments. This is useful while doing shell programming when you want a process to be finished before the next process is invoked. If you do not specify a process ID, UNIX will find out all the processes running for the current environment and wait for termination of all of them.
Examples If you want to find out whether all the processes you have started have completed, execute the following command:
wait
If you want to find out whether the process ID 15060 has completed, execute the following command:
wait 15060
The return code from the wait command is zero if you invoked the wait command without any arguments. If you invoked the wait command with multiple process IDs, the return code depends on the return code from the last process ID specified.
When you are executing processes under UNIX, they can be running in foreground or background. In a foreground process, you are waiting at the terminal for the process to finish. Under such circumstances, you cannot use the terminal until the process is finished. You can put the foreground process into background as follows:
Ctrl-z bg
The processes in UNIX will be terminated when you logout of the system or exit the current shell whether they are running in foreground or background. The only way to ensure that the process currently running is not terminated when you exit is to use the nohup command.
The nohup command has default redirection for the standard output. It redirects the messages to a file called nohup.out under the directory from which the command was executed. That is, if you want to execute a script called sample_script in background from the current directory, use the following command:
nohup sample_script &
The & (ampersand) tells UNIX to execute the command in background. If you omit the &, the command is executed in foreground. In this case, all the messages will be redirected to nohup.out under the current directory. If the nohup.out file already exists, the output will be appended to it. If the permissions of the nohup.out file is set up so that you cannot write to that file, UNIX will create or append to the nohup.out file in your home directory. There may be instances where neither of these files can be accessed. For example, you may have run out of disk space. In such circumstances, UNIX will not initiate the nohup command.
The nohup command allows you to redirect the output into any file you want instead of the default file nohup.out by using standard UNIX redirection capabilities.
When you initiate the nohup command in the background (by using & at the end of the command line), UNIX displays the process ID associated with it, which can later be used with commands like ps to find out the status of the execution.
Examples If you want to find the string sample_string in all the files in the current directory, and you know that the command will execute for quite a while, execute the following command:
nohup grep sample_string * &
UNIX responds with the following message:
[2] 160788 Sending output to nohup.out
The first line of the display is the process ID, and the second line is the informational message about the output being directed to the default nohup.out file. You can later go into the file nohup.out to find out the result of the grep command.
In the preceding example, if you would like to redirect the output to a file called mygrep.out, execute the following command:
nohup grep sample_string * > mygrep.out &
In this case, UNIX displays only the process ID as follows:
[2] 160788
You can also execute shell scripts, which can contain multiple commands, using the nohup command. For example, if you want to execute a script called my_script in the current directory and redirect the output to a file called my_script.out, execute the following command:
nohup my_script > my_script.out &
CAUTION:: You should be careful not to redirect the output of a script you want to execute to the script itself.
If you want to wait for a certain period of time between execution of commands, use the sleep command. This can be used in cases where you want to check for, say, the presence of a file, every 15 minutes. The argument is specified in seconds.
Examples If you want to wait for 5 minutes between commands, use:
sleep 300
Here is part of a small shell script that reminds you twice to go home, with a 5-minute wait between reminders. (The script is incomplete.)
echo "Time to go home" sleep 300 echo "Final call to go home ....."
UNIX has several commands that are used to communicate with host computers for purposes of connecting to another host, transferring files between host computers, and more. You may need these commands, for example, to transfer a file from another host to the local host you are working on (provided you have sufficient access to do so). These commands are different from the commands used to log in to other computers because these allow you to communicate with other hosts without logging in.
The cu command allows you to connect to another host computer, either directly or indirectly. That is, if you are currently on host1 and you use cu to connect to host2, you can connect to host3 from host2, so that you are connected directly to host2 and indirectly to host3.
Following is a list of some of the flags that can be used with cu command:
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
You can use the ftp command to transfer files between two host computers. This command allows you to move files from a remote host to a local host, or to move files from the local host to a remote host. The ftp command allows only simple forms of file transfer. That is, you will not be able to copy files from directories recursively using a single ftp command. Instead you must transfer each file or directory individually. You should also be aware that ftp can be used between different types of systems, which means that system-dependent file attributes might not be preserved when files are transferred.
While using the ftp command, you need to use a login and password to login to the remote host. However, it is possible to login to the remote host without a password if the home directory has a .netrc file and that file contains the macro necessary for login to the remote host.
If a login ID and password are needed, the ftp command prompts for that information. In such a case, you will not be able to proceed until you are able to successfully login using the correct user ID and password.
Once you are able to login successfully, the ftp command displays an ftp> prompt. In the ftp> prompt, you can use a number of sub-commands to perform file transfers.
You can specify the hostname as part of the ftp command or open a connection to a host in the ftp> prompt. If you specify the hostname as part of the command line, you are prompted for login and password before you are put into the ftp> prompt. For example, if you execute the following command:
ftp box2
you will be prompted for user ID and password.
Some of the flags that can be used with the ftp command are as follows:
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.
In UNIX, you can send mail to other users in the system and receive mail from them by using the mailx commands. The mailx commands provide sub-commands to facilitate saving, deleting, and responding to messages. This command also provides facilities to compose and edit messages before finally sending it to one or more users.
The mail system on UNIX uses mailboxes to receive mail for a user. Each user has a system mailbox in which all mail for that user is received pending action by the user. The user can read, save, and delete the mail once the mail is received. Once the user has read mail, it can be moved to a secondary or personal mailbox. The default secondary mailbox is called the mbox. The mbox is usually present in the home directory of the user. However, the user can specify the name of a file as a secondary mailbox. All messages saved in the mailbox mbox are save indefinitely until moved to other secondary mailboxes, which are sometimes known as folders. You can use the folders to organize your mail. For example, you can organize folders by subject matter and save all mail pertaining to a subject in a particular mailbox.
You can send messages to one or more users using the mailx command. This command allows you to send mail to users on the same host or other hosts in the network to which the local host is connected. You will not get a positive acknowledgment if the mail delivery is successful, but if the mail cannot be delivered, you will get notification.
Following is a list of some of the flags that can be used with the mail command:
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.
You can converse with another user in real time using the talk command if the other user is logged on. Using the talk command, you can converse with users on local host or remote host.
The talk command takes one mandatory argument--user name or user and host name. You can optionally provide a second argument specifying the TTY onto which the user is logged.
The user on the remote host can be specified in one of the following formats:
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.
If you want to notify a mail sender that you are on vacation, you can use the vacation command. The message sent can be a customized message if you create the message in a file called .vacation.msg in your home directory. If this file does not exist, a system default message is sent. By default, the system message is sent only once a week to a user who sends mail to you.
The vacation command can be used to forward messages you receive during vacation to other users by using the .forward file in your home directory. Include the user names of all the users to whom you want the messages to be forwarded. The entry in the .forward file is of the following format:
testuser, "|/usr/bin/vacation testuser"
The vacation command also lets you store the names of users who sent messages to you while you were on vacation. These user names are stored in the .vacation.dir and .vacation.pag files in your home directory.
The vacation command has one option flag, which is used as follows:
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.
The write command can be used to hold a conversation with another user in the local host or remote host just like the talk command. To hold a conversation with another user, the following must be true:
The user must be logged on.
The user must not have denied permission by using the mesg command.
A message consists of all the characters you have typed until you hit the Enter key. Both you and the other user can send messages this way. To end the conversation, use Ctrl-d.
The following is a list of some of the flags that can be used with the write command:
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
Here some of the commands that can be used for comparing the contents of the file are presented. These commands compare the contents and, depending on various options, generate outputs of what the differences are between the various files. There are also commands that can be used to compare the contents of directories.
The cmp command compares the contents of two files and generates output into standard output. It is possible to have one of the files be standard input but not both. You can use a - (hyphen) to indicate that the file is standard input, which is terminated using Ctrl-d. The cmp command should be used for nontext files to find out whether they are identical. For text files, use the diff command, discussed later.
The following are the outputs generated by the cmp command:
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
If you have files that are sorted and you want to compare them, use the comm command. The comm command can be used to either exclude or include the common lines between the two files. You can use a - (hyphen) to indicate that one of the files is to be accepted from standard input.
The default output is generated on the standard output in three columns, which are as follows:
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
You can compare text files with the diff command but cannot compare nontext files. You can use diff to compare individual files or multiple files with identical names in different directories. For comparing individual files, use a - (hyphen) to indicate that one of the files is to be accepted from the standard input.
The group of output lines of the diff command is preceded by an information line containing the following information:
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
The diff command compares two files. If you want to compare three files at the same time, use the diff3 command. The diff3 command writes output to the standard output that contains the following notations to identify the differences:
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
If you want to compare the contents of two directories, use the dircmp command. This command compares the names of the files in each directory and generates a list of filenames that exist only in one of the directories followed by filenames that exist in both and whether they are identical or not.
The following is a list of flags that can be used with dircmp command:
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.
The command sdiff compares two files and displays output on the standard output in a side-by-side format. Following is the detail of the display format:
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
Here we will discuss several commands that can be used to manipulate various attributes of one or more files, as well as to copy and move files from one location to another. The various attributes that can be manipulated include modification time, permission, and more.
The touch command can be used for a number of purposes depending on whether a file already exists. If a file does not exist, the touch command will create it if you have write access to the directory. If a file is already present, the touch command modifies the last modification time of the file.
Examples To create a file called testfile in the current directory, execute the following command:
touch testfile
To create testfile in the /u/testuser/testdir, execute the following command:
touch /u/testuser/testdir/testfile
You may need to modify the permission of a directory or files either to secure them or to make them accessible to others. You can use the chmod command to modify the permission or files and directories. The permission in UNIX is specified in octal number (0 thorough 7). Permission for a file or directory can be specified for the following:
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
NOTE: Modifying the permissions will not have any effect on the root user. The root user has access to all files and directories irrespective of the permissions you may have granted.
If you want to change the group to which the file belongs, use the chgrp command. The group must be one of the groups to which the owner belongs. That is, the group must be either the primary group or one of the secondary groups of the owner.
Examples Assume user testuser owns the file testfile, and the group of the file is staff. Also assume that testuser belongs to the groups staff and devt. To change the owner of testfile from staff to devt, execute the following command:
chgrp devt testfile
In case you want to change the owner of a file or directory, use the chown command.
CAUTION:: On UNIX systems with disk quotas, only the root user can change the owner of a file or directory.
Examples If the file testfile is owned by the user called testuser, to change ownership of the file to user friend, you must execute the following command:
chown friend testfile
Once you are done using a file and you do not want to use it any more, then you would like to remove the file so as to regain the space utilized by the file. The rm command lets you do this by removing files permanently from the disk. If an entry is the last link to a file, the file is deleted. To remove a file from a directory, you do not need either read or write permission to the file, but you do need write permission to the directory containing the file. The rm command is usually used to remove files, but it provides a special flag -r to remove files in a directory recursively, including the directory and its sub-directories.
Following is a list of some of the flags that can be used with the rm command:
The native version of the rm command does not ask for confirmation while removing files. So you should be careful when using wildcards with the rm command.
Examples If you want to remove all files starting with test in the current directory, execute the following command:
rm test*
However, if you make a typing mistake and type the following:
rm test *
you will remove all the files because of the asterisk (*).
CAUTION:: Be careful when using wildcards with the rm command. Be sure before you remove a file, because once a file is removed, it cannot be recovered.
To avoid any such mistakes, use the -i flag to indicate that you want to execute the rm command in interactive mode. In this mode, the system will ask you for confirmation before removing the file. Only if you confirm it with a y will the system remove the file. Following is the dialog you can have with the system if you want to remove two files in the current directory: testfile1 and testfile2, using the -i flag with the rm command:
rm -i testfile* Remove file testfile1? y Remove file testfile2? y
You can use the flag -f with the rm command if you do not want to get any messages from the command. Usually rm will display a messages that a file is not present if you do not provide the correct name of the file. However, using the -f flag forces rm to not display any messages. If you execute the following command:
rm -f testfile
the file testfile will be deleted if present, and no action will be taken if testfile is not present. In either case, you will not get any message from the rm command. Also, the rm -f command always has a return code of 0 (zero).
You can use the -r flag to remove files in directories recursively including dire