Professional Documents
Culture Documents
Unix Command
Unix Command
Page 1 of 9
This chapter provides general information on a number of basic Unix commands. All of these commands contain numerous options. Some of the more common options will be discussed. For further information on commands, view the on-line help information using the man command. You can also sometimes obtain information on command syntax by entering a command with incorrect options. In some cases, the system will respond by displaying the correct syntax. On-Line Help man Printing a man page Directory Commands Creating a Directory (mkdir) Removing a Directory (rmdir) Moving around Directories (cd) Directory Shortcuts Present working directory (pwd) File Commands Listing Directory (ls) File Protections (chmod) Default File Protections (umask) Copying Files (cp) Renaming Files (cp) Deleting Files (rm) File Searches (grep) Viewing Files (more, cat, head, tail) Pattern Matching Process Monitoring Process Status (ps) Current Processes (top) who and w Commands Killing Processes (kill) System Loads (ruptim) Process Priority (nice and renice) Disk Usage Disk Usage for Filesystems(df) Disk Usage (du) Disk Quota (quota) Command History Unix Tools Pipes I/O Redirection Sorting (sort) File Differences (diff) Counting Lines, Words, Characters (wc) Miscellaneous Utilities NCAR Telephone Directory (phone) NCAR Central E-Mail Database Spell Checker (ispell) System Information (mmminfo) System Assistance (assist) Changing your Password Changing Your Password (mmmpasswd) Account Security
If you do not know the name of the actual command you need, execute man with the -k option.
man -k keyword
For example, to find out all the commands related to directories, type
man -k directory
This command outputs information to your screen about the commands that pertain to directories, along with a brief description of each command's functions. To access man pages for local utilities, add /usr/local/man to your path variable.
You can then send the formatted file to a printer using qpr ls.txt.
Page 2 of 9
The Unix file system allows users to create several levels of subdirectories for logically grouping files. This section describes the basic utilities for creating these directories and moving around within them.
From a login directory, such as /users/jones, create a subdirectory, memos, with the following command.
mkdir memos
This creates the directory, /users/jones/memos You can create multiple directories with one command. From the login directory, /users/jones, the command,
mkdir projects samples
creates the directories, /users/jones/projects and /users/jones/samples. You can also specify a directory that is not under your current directory by specifying its absolute position. From the directory, /users/jones/samples, the following command would create the directory, /users/jones/memos/old.
mkdir /users/jones/memos/old
From the directory, /users/jones/memos, the following command would delete the subdirectory, old.
rmdir old
To remove that same directory from the directory, /users/jones, use the command
rmdir memos/old
Note: Use the above command with extreme caution, as it will very quickly delete every file and subdirectory below the specified directory.
For example, from the main directory, /users/jones, change to the sub-directory, /users/jones/memos, with the command
cd memos
From the subdirectory, /users/jones/memos, change to the directory, /users/jones/samples with the command,
cd /users/jones/samples
You can also use cd to move to the home directory of another user.
cd ~username
In addition, use the .. notation for moving around directories. The .. refers to the parent directory of your current directory. For example, from the directory, /users/jones/memos, change to the subdirectory, /users/jones/samples, with the command,
cd ../samples
The .. says to move up to the parent directory, in this case /users/jones, and then down to the sub-directory, samples. You can also use cd .. to back up from subdirectories level by level.
On-line Information
man man man man man mkdir rmdir cd pwd rm (see option -ri)
http://box.mmm.ucar.edu/computing/doc/guide/ch_3.html
2/13/2012
Page 3 of 9
The ls command by itself will list the files in your present working directory. By default, the ls command lists only the names of the files in the directory. The following examples show some of the more commonly used options.
1. To list all the Unix hidden files, such as .login or .cshrc in your home directory, enter ls -a 2. For a long listing that shows file protections, size, and date, enter ls -l 3. To recursively list all the files in the current directory and any subdirectories of the current directory, enter ls -R 4. It is sometimes useful to use the ls command in conjunction with other Unix commands. For instance, if there are too many files in a directory to fit on one screen, the output can display to the screen a page at a time. ls -l | more 5. To search for a subset of files, use the ls command with the Unix search command, grep. To find all files written on a particular date, enter ls -l |grep 'Jun 25'
With the chmod command, you can modify protections on a file for three classes of users: the user (u), group (g), or others (o) by entering
Syntax: chmod ugo filename
The protection for each class of user can be obtained by adding the numerical equivalent for each type of access. For instance, to give the owner read, write, and execute access, it would be 4+2+1 or 7. To give group read and write access to the file would be 4+2 or 6, and to give world read access would be 4. The resulting command would be
chmod 764 filename
You can use wild card characters in the filename specification, or specify multiple files separating them by spaces. The following sample listing of a file shows owner with read/write access and group and world with read only. The command, chmod 644 would produce these protections.
-rw-r--r-- 1 waukau 5417 Jun 6 05:28 test
To include execute access to this file for all user classes, enter chmod 755, which produces these protections.
-rwxr-xr-x 1 waukau 5417 Jun 6 05:28 test
You can also use the symbolic representation for adding or removing access to files. For example, to give execute access to all categories (user, group, and others), enter
chmod +x filename
The access modes specified in the umask command are the complement of the chmod command. You specify those access modes you do not want a particular class to have, as in the following examples.
1. To remove write access for group and others, specify umask 022 2. To remove all access for group and others, specify umask 077
You can use wild cards to remove multiple files. When deleting with wild cards, it is best to have the rm command ask you whether you want to delete each individal file. To do this, enter
http://box.mmm.ucar.edu/computing/doc/guide/ch_3.html
2/13/2012
Page 4 of 9
You are prompted for a response for each file that matches this criterion (in this case, all files that have the suffix, .f). If you answer y, the file is deleted. If you answer n (or any other response), the file is not deleted.
By default, the grep command matches the letter case of the string specified, as in the following examples.
1. To search all files in the directory for the string, "May," enter grep May * 2. To find any occurrence of the string, "May," regardless of letter case, use the -i option. grep -i May * This would find strings such as "may," "maybe," or "MAY," along with any other occurrence of that string.
It is useful to pipe (|) the output of the grep command into the more command to see the listing one screen at a time, as shown below. For further information on pipes, see Section 3.7.1.
grep -i May * | more
Note: Certain special characters must be "escaped" with a "\" when searching for them in regular expressions.
Following are some helpful options to use with the more utility.
q ib Exit immediately from more. Move backward by screens, where i is the number of screens Move forward by screens, where i is the number of screens
if
/string Search forward for string. Backward searches are not possible.
Also, use cat for quickly creating a short file, by entering the following.
cat > filename
After pressing the return key, type in the text. To save and exit the file, press CTRL-D. An additional feature of cat is that it allows you to concatenate two files together by entering the following
cat file1 >> file2
If the -count option is specified, you can display more than ten lines. The following command would display the first 20 lines.
head -20 filename
If the -count option is specified, you can display more than ten lines. The following command would display the last 20 lines.
tail -20 filename
It is helpful to "pipe" the head and tail command outputs into the more command when -count exceeds 20. See Section 3.7.1.
http://box.mmm.ucar.edu/computing/doc/guide/ch_3.html
2/13/2012
Page 5 of 9
This would find matches such as 10,234 and 10,456. However, 10234, typed without a comma, would not match.
would find files such as plot1.dat, plot2.dat or plots.dat. It would not find the file plot22.dat.
This would show occurrences of the words, "meteorological", "meteorologist", "meteors", etc. As another example, to find all files with the file extension .f, enter
ls -l *.f
On-line Information
man man man man man man man man man man man ls chmod umask cp mv rm grep more cat head tail
The ps command specified by itself will display your current processes. To display information about all processes on the system, enter
ps -ef (on SGI, Sun, and HP Alpha)
If there is more information than will fit on one screen, use the ps option with the more utility, as follows:
ps -aux | more
To find information about a specific user on the system, use the ps command with the grep search utility, as follows:
ps -aux | grep username
Below is a sample output from the ps command and a table that explains the information displayed.
UID root root smith jones smith brown Column UID PID PPID C STIME TTY TIME CMD PID 157 260 10566 11741 10644 11656 PPID 1 1 10564 11739 10510 11440 C 0 0 0 1 0 0 STIME Mar 18 Mar 18 07:02:31 11:12:30 07:09:25 10:53:33 TTY ? ? pts/4 pts/8 pts/2 pts/7 TIME 0:03 0:02 0:01 0:00 0:14 0:00 CMD /usr/sbin/inetd -s /usr/sbin/vold /usr/local/bin/tcsh -csh emacs NEW_CUTILS.c idt -soft ppi-cesar.cgm
Description User ID of process Process id number Process ID of parent process Processor utilization for scheduling (obsolete) Starting time of process Controlling terminal of process, ? when no controlling terminal Cumulative execution time for the process Command name
The columns and information displayed varies between architectures. Use the man command to check for specifics.
http://box.mmm.ucar.edu/computing/doc/guide/ch_3.html
2/13/2012
Page 6 of 9
To kill a background job, use the jobs command to find its job ID (n) and enter
kill -9 %n
Another useful utility is pkill, available on the Sun, Linux, and SGI architectures. Your current processes will be listed to the screen one at a time sorted by the age of the process, with your oldest process (most likely your login shell) appearing first. You will be given the option to kill each process. Responding yes to the inquiry will kill the process; a carriage return will retain the process.
Syntax: pkill
The last three columns are 1-, 5-, and 15-minute averages. Values indicate whether the CPU is underutilized (< 1.0), 100% utilized (1.0), or processes are waiting (> 1.0). If you receive the error message, permission denied when executing this command, you need to modify your .rhosts file. The .rhosts file should include the systems being checked as well as the system you are logged on to.
The higher the number specified, the lower the priority of the process. The default is 10, and you can specify numbers as high as 20. Note that you can only increase this number. For example, a process started with a value of 18 cannot be lowered to 10 with nice -10, except by root. For instance, to execute a program called a.out at a low priority, enter
nice -n 19 a.out
If you have already started a process without the nice command, you can use the renice command to change the priority of the process.
Syntax: renice -number process_id
Processes can also be reniced from the top command (see Section 3.4.2) by typing r followed by the priority number and process ID (PID).
On-line Information
man man man man man man man ps top who w kill nice renice
By default, the du command by itself will give a summary of quota usage for the directory specified and all subdirectories below it.
http://box.mmm.ucar.edu/computing/doc/guide/ch_3.html
2/13/2012
Page 7 of 9
On-line Information
man df man du man quota
where nn is the number of lines you wish to retain; 40 is a good number. There are a number of ways to re-execute commands maintained in the history file. !! entered on the command line executes the most recently executed command. If you enter the history command, a list of your most recently executed commands will be displayed with a line number next to each command. To re-execute a specific command, enter
!line_number
Another mechanism for recalling a previously executed command is to enter the first few letters of the previous command. For example, if you had previously entered a command to recursively look through your directory structure for all directory files, using
ls -lR | grep drw
The shell would then reexecute the most recent command line that began with ls.
3.7.1 Pipes
Pipes allow you to connect various combinations of Unix commands together. The pipe symbol is the vertical line, |, on your keyboard. For example, to search a directory listing for a particular string, and output the information to the screen a page at a time, enter
ls -l | grep 'string' | more
To have the Mail utility read in information from a file called "input.txt," enter
mail user@host < input.txt
You can also use the sort command in combination with other commands using pipes. To sort the output from a who command, enter
http://box.mmm.ucar.edu/computing/doc/guide/ch_3.html
2/13/2012
Page 8 of 9
This command is useful when combined with other commands. For example, to find the number of files in a directory, enter
ls -l | wc -l
On-line Information
man sort man diff man wc
For example, to search for information on a person with the last name of jones, enter
phone jones
Some non-NCAR individuals may also be listed in this database, especially if they have an SCD account.
The ispell utility will flag each word that it does not find in its dictionary. The flagged word will be displayed at the top of the screen followed by a list of numbered words that are possible options.To select one of the suggested spellings, enter the number of the appropriate word. If none of the suggested words is correct, and you wish to enter a replacement word, enter R. If the flagged word is correct, either press the space bar or enter I to put the word in your personal dictionary. When using ispell on a TeX file, use the -t option.
Syntax: ispell -t filename
where string can be the information you want to check on, such as a system name, IP address, or individual's name. This will display basic information about the system. If you want information related to networking use the -n option. For complete information on a system enter the -a option. There is also a man page available for the mmminfo command.
Following is a list of the available options. More detailed information can be found in the man pages man assist .
-e editor To log a problem, use the -e
http://box.mmm.ucar.edu/computing/doc/guide/ch_3.html
2/13/2012
Page 9 of 9
-l
-r request-id List a complete description of specified request. -u username List brief descriptions of all requests submitted by the specified user. -w Show who is on-duty. This person is responsible for monitoring the assist database for that day, and can also be contacted directly for immediate problems.
You will be prompted for your old password, new password, and verificiation of the new password. Password information will not be displayed to the screen as it is entered. This information will then be propagated to other systems within a half hour.
You should always change your password using the mmmpasswd command. Passwords will then be propagated to other systems.
On-line Information
man ispell
Hardcopy References
Unix Made Easy Unix Primer Plus Sun OS User's Guide, Getting Started The Little Gray Book: Ultrix Primer The Big Gray Book: The Next Step with Ultrix
Return to the top of page Copyright UCAR 1998 - Disclaimer - mmminfo@ncar.ucar.edu Last Modified: 1 July 2000
http://box.mmm.ucar.edu/computing/doc/guide/ch_3.html
2/13/2012