You are on page 1of 9

Chapter 3: Unix Commands

Page 1 of 9

CHAPTER 3: UNIX COMMANDS


back to the Computing Guide Contents

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

3.1 On-Line Help


3.1.1 The man Command
To obtain information on any Unix command, use the on-line manual pages.
Syntax: man command

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.

3.1.2 Printing a Hardcopy of a man page


To print a hardcopy of a manual page, redirect the output to a file, and then send this file to a printer using one of the printing commands, as in the following example: To generate a file for hardcopy output of the ls command, type
man ls > ls.txt (SGI, Sun, HP Alpha)

You can then send the formatted file to a printer using qpr ls.txt.

Return to top of page

3.2 Directory Commands


http://box.mmm.ucar.edu/computing/doc/guide/ch_3.html 2/13/2012

Chapter 3: Unix Commands

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.

3.2.1 Creating a Directory (mkdir)


The mkdir command allows you to create directories.
Syntax: mkdir [options] directory

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

3.2.2 Removing Directories (rmdir)


To remove a directory, use the rmdir command. Note that you must first delete all files within a directory before deleting the directory itself.
Syntax: rmdir directory

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

An alternative is the recursive option with the remove command (rm).


Syntax: rm -ri directory

Note: Use the above command with extreme caution, as it will very quickly delete every file and subdirectory below the specified directory.

3.2.3 Moving around Directories (cd)


To move around in your directory structure, use the change-directory command, cd. The cd command specified by itself without any arguments will always return you to your login directory.
Syntax: cd 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

3.2.4 Directory Shortcuts


You can use shortcuts when specifying your directory pathname. Examples here show how to use these shortcuts with the cd command, but they can be used with any of the Unix commands when specifying filenames. Your home directory can be used as a starting point when specifying pathnames by using a tilde (~). The tilde instructs the shell to start the pathname from your home directory. For example, from the directory, /users/jones/memos/old, change to the subdirectory, /users/jones/samples with the command
cd ~/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.

3.2.5 Present Working Directory (pwd)


When moving around the Unix file system, it is helpful to check your present working directory, with the pwd command.
Syntax: pwd

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

Chapter 3: Unix Commands


Return to top of page

Page 3 of 9

3.3 File Commands


3.3.1 Listing Directory Files (ls)
To list the files in any directory, use the ls command.
Syntax: ls [options] [filename_spec]

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'

3.3.2 File Protections (chmod)


Following are the protections that can be set on a file with their symbolic or numeric representations.
Symbolic r w x Numeric 4 2 1 Protection Read access Write access Execute access

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

To remove write access to a file for group and others, enter


chmod g-w, o-w filename

3.3.3 Default File Protections (umask)


You can set up a default file protection for all new files created by using the umask command in your .login file.
Syntax: umask ugo

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

3.3.4 Copying Files (cp)


Use the cp command to make a copy of a file.
Syntax: cp [options] old_filename new_filename

3.3.5 Renaming Files (mv)


To rename a file, use the mv command.
Syntax: mv [options] old_filename new_filename

3.3.6 Deleting Files (rm)


To delete a file or files, use the rm command.
Syntax: rm [options] filename

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

Chapter 3: Unix Commands


rm -i *.f

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.

3.3.7 File Searches (grep)


At times, it is necessary to search through your files for a particular string of characters. The command to perform searches is grep.
Syntax: grep string file1 [file2 . . .]

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.

3.3.8 Viewing Files (more, cat, head and tail)


There are a number of commands that can be used for viewing files in the Unix operating system. This section will describe a few of these.

The more Command


The more command outputs a file to the screen a page at a time. To view the next page, press the space bar. To see just one new line at a time, press RETURN. The more utility also allows you to perform some basic editing functions such as string searches, moving forward and backward in the file, and entering the vi editor. CTRL-c aborts a more session.
Syntax: more [options] filename

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.

The cat Command


The cat command will also display file contents to a screen.
Syntax: cat filename

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

This command appends file1 to the end of file2.

The head Command


To view the first ten lines in a file, use the head command.
Syntax: head [-count] filename

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

The tail Command


To view the last ten lines in a file, use the tail command.
Syntax: tail [-count] 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.

3.3.9 Pattern Matching


In the previous sections, examples were given for matching exact strings. There are also certain metacharacters available in Unix that allow more flexibility in performing string searches.

Matching Any Single Character in a String Expression


You can use a period (.) to match any single character in an expression, and concatenate periods to represent an exact number of characters, as in the following example. To search a file for dollar amounts in the ten-thousand-dollar range, enter

http://box.mmm.ucar.edu/computing/doc/guide/ch_3.html

2/13/2012

Chapter 3: Unix Commands


grep 10,... proposal

Page 5 of 9

This would find matches such as 10,234 and 10,456. However, 10234, typed without a comma, would not match.

Matching Any Single Character in a File Name Specification


When specifying file names, use a question mark (?) to match any single character, and concatenate question marks to represent an exact number of characters. For example, entering
ls -l plot?.dat

would find files such as plot1.dat, plot2.dat or plots.dat. It would not find the file plot22.dat.

Matching Any Number of Occurrences of a Character


In an expression or filename specification, an asterisk (*) stands for any string of characters, even a null one. For example, to find all derivatives of the word meteorology in a file, enter
grep 'meteor*' filename

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

Return to top of page

3.4 Process Monitoring


It is important to recognize that in an X Window environment, a single user can generate a minimum of five processes just by logging on to a system, and can expand this number exponentially, depending on the number of windows open and software being executed. Therefore, you need to be conscientious about the number of CPU and memory resources you are using. This section will describe some of the Unix commands that you can use to monitor your processes.

3.4.1 The ps Command


The ps command is a general utility for checking process status. It reports information such as current percentage of the CPU being used by processes, percent of memory, accumulated CPU time, and other statistics. It is not a real-time display, but takes a snapshot of the system.
Syntax: ps [options]

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.

3.4.2 The top Command


The top command is a pseudo-real-time command that is useful for examining current processes on the system and for changing process priorities.
Syntax: top

http://box.mmm.ucar.edu/computing/doc/guide/ch_3.html

2/13/2012

Chapter 3: Unix Commands


To exit the top command, type q. To show other options, type h.

Page 6 of 9

3.4.3 The who and w Commands


Other commands to determine who is currently on the system are the who and w commands. The w command provides additional information about login and idle times for different processes.
Syntax: who Syntax: w

3.4.4 Killing Processes


There are a number of ways to kill processes on a workstation. To kill a job that is in the foreground, press CTRL-c. Use the kill command along with the process ID (PID). (PID information can be obtained from the ps command).
Syntax: kill -9 pid

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

3.4.5 Checking the Load on Other Workstations


If a workstation appears to be heavily loaded, you can check the load on other workstations with the ruptim command.
Syntax: ruptim [workstation]

Following is a sample output from this utility.


oak fir pine juniper cypress balsam walnut beech number number number number number number number number of of of of of of of of users: users: users: users: users: users: users: users: 1 15 25 15 9 15 3 4 load load load load load load load load average: average: average: average: average: average: average: average: 0.26, 0.26, 0.21, 0.39, 0.22, 0.18, 2.00, 1.17, 0.16, 0.06, 0.03, 0.12, 0.04, 0.15, 1.94, 1.15, 0.00 0.00 0.01 0.01 0.00 0.00 1.56 1.00

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.

3.4.6 Changing Process Priority


All Unix processes have a dynamically calculated priority. This priority changes over time as a process accumulates CPU time. To execute a program that will use several minutes of CPU time over a short wall-clock period, change the process priority so that it does not monopolize the machine. To do this, use the nice command when executing your process:
Syntax: nice -number argument

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

Return to top of page

3.5 Disk Usage


3.5.1 The df Command
The df command lists the current total disk usage for all file systems accessible by your workstation.
Syntax: df -k

3.5.2 The du Command


To determine your current disk quota usage, use the du command.
Syntax: du [options] directory

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

Chapter 3: Unix Commands


du /users/username

Page 7 of 9

For a summary of total disk usage, use the -s option.


du -s /users/username

For a summary of your own disk usage, enter


du -s ~

3.5.3 The quota Command


On the Sun, HP Alpha, and Linux workstations, the quota command displays your current usage. This command does not display relevant information on the SGI systems.
Syntax: quota -v username

On-line Information
man df man du man quota

Return to top of page

3.6 Command History


The shell keeps a history list of recently executed commands if you instruct it to do so. The following line should be placed in your .login and .cshrc files.
set history=nn

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

You could recall that command with the following.


!ls

The shell would then reexecute the most recent command line that began with ls.

Return to top of page

3.7 Unix Tools


The Unix operating system provides a variety of utilities that allow you to perform functions such as combining multiple commands and redirecting your output.

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 search the current system processes for a particular user, enter


ps -ef | grep username

3.7.2 I/O Redirection


In general, Unix commands send output to "standard out" (stdout), your terminal screen. At times, you may want this information written to a file instead. Unix allows you to either redirect output to a file or to have a Unix command read input from a file. The symbol > outputs information to a file, and < reads input from a file. For example, to perform a search on a directory listing and write that output to a file called "source.txt," enter
ls -l |grep '*.f' > source.txt

To have the Mail utility read in information from a file called "input.txt," enter
mail user@host < input.txt

3.7.3 The sort Command


The sort utility sorts the lines in a file. By default, it sorts the file in ASCII order, with numbers preceding alphabetic characters.
Syntax: sort filename

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

Chapter 3: Unix Commands


who | sort

Page 8 of 9

3.7.4 File Differences (diff)


It is sometimes useful to check the differences between two files. The diff command performs this function.
Syntax: diff [options] file1 file2

3.7.5 Counting Lines, Words, and Characters (wc)


It is often useful to know the number of lines, words, and characters in a file. The wc utility accomplishes this task. By default, it prints out all three of these fields.
Syntax: wc [options] filename

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

Return to top of page

3.8 Miscellaneous Utilities


There are several general-purpose utilities developed either within MMM or obtained from other sources. This section briefly describes some of these commands.

3.8.1 The phone Command


The phone utility accesses the on-line NCAR phone directory. It performs a search on an ASCII file and takes a string as its argument.
Syntax: phone string

For example, to search for information on a person with the last name of jones, enter
phone jones

For numbers of all MMM staff, enter


phone mmm | more

3.8.2 NCAR Central E-mail Database


To obtain e-mail information for individuals at NCAR that are not in the MMM Division, you can also access the NCAR central e-mail database with the following procedure.
1. Enter telnet directory 2. At the login prompt, enter directory 3. A menu will appear. Choose the appropriate option for searching for an individual name.

Some non-NCAR individuals may also be listed in this database, especially if they have an SCD account.

3.8.3 The ispell Command


The ispell utility checks a file for misspelled words. To use this command, enter
Syntax: ispell filename

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

3.8.4 The mmminfo Command


As there are a large number of systems of varying architectures within the division, we have developed a utility for checking to find out what architecture a particular machine is as well as other pertinent information such as location and IP address. To find out information about a particular system enter the mmminfo command.
mmminfo string

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.

3.8.5 The assist Command


Assist can also be accessed through our website. The assist utility will allow users to report problems or view problems that have been reported.
assist options

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

option, where the editor is

http://box.mmm.ucar.edu/computing/doc/guide/ch_3.html

2/13/2012

Chapter 3: Unix Commands


either "vi" or "emacs". It will ask you specific questions and place you in an editing session to type in information. After you have submitted a problem report, you should receive mail stating that your report has been received and informing you of a reference ID for future reference. -s Displays a listing of short-term entries in the assist database. Short-term entries are those that will be addressed within a short time frame (a day to a couple of weeks). Displays a listing of all requests in the assist database.

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.

Return to top of page

3.9 Changing Your Password


3.9.1 The mmmpasswd Command
New accounts within MMM are initially set up with a temporary password. When you first log in to a new account you should change the password. The standard Unix command for changing your password is passwd, however, this only changes your password on a single machine. Most MMM users access a variety of systems within the division. In order to change your password and have it propagated to all divisional systems, use the mmmpasswd utility.
Syntax: mmmpasswd

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.

3.9.2 Account Security


The security of your account is primarily your responsibility. To maintain account security, change your password often, at least twice a year, and use the following guidelines to select a password.
1. Your password should be at least six characters in length and contain least one non-alphabetic character. 2. Avoid using proper names, especially those associated with yourself or members of your family. 3. Do not write your password down. 4. Do not give your password to others. 5. Do not use words found in dictionaries, as many password-breaking programs use these dictionaries for guessing passwords.

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

You might also like