Frequently Used UNIX Commands
Most servers on the GAUL network are UNIX-based, meaning that a working knowledge of basic UNIX commands is essential. This tutorial details several commands that you may find useful when working in a UNIX environment.
Working with directoriesListing the contents of the current directory
Simple Directory Listing
The ls command is used to list the contents of the current directory:
obelix[38]% ls file1.txt file2.txt other/
The output above shows two files in the current directory, file1.txt and file2.txt, as well as a subdirectory other. Directories are denoted by a trailing /.
Detailed Directory Listing
By adding the -l flag, you can see more details on the files in a given directory. Reading from right to left below, a file or directory name is listed, followed by the date and time at which the file was last modified. Next, the size of the file (in bytes) is displayed. The remaining columns present security-related information which is beyond the scope of this tutorial.
obelix[43]% ls -l total 7 -rw------- 1 jshantz4 2002 14 Dec 7 06:45 file1.txt -rw------- 1 jshantz4 2002 27 Dec 7 06:45 file2.txt drwx------ 2 jshantz4 2002 3 Dec 7 06:45 other/
Showing Hidden Files and Directories
Finally, by adding the -la flag, you can see all files and directories in the current directory, including all hidden files or directories. Any file that begins with a period (.) will be hidden in UNIX unless the -a flag is passed to ls. Note that each directory contains a special directory pointer ., which refers to the current directory, as well as another directory pointer .., which refers to the parent directory (the directory above the current directory).
obelix[47]% ls -la total 24 drwx------ 3 jshantz4 2002 6 Dec 7 06:57 ./ drwx------ 50 jshantz4 other 138 Dec 7 06:44 ../ -rw------- 1 jshantz4 2002 0 Dec 7 06:57 .hiddenfile -rw------- 1 jshantz4 2002 14 Dec 7 06:45 file1.txt -rw------- 1 jshantz4 2002 27 Dec 7 06:45 file2.txt drwx------ 2 jshantz4 2002 3 Dec 7 06:45 other/
Showing the name of the current directory
To view the name of the current directory, use the pwd (print working directory) command:
obelix[48]% pwd /student/jshantz4/tmp
Creating a new directory
To create a new directory, use the mkdir command:
obelix[49]% ls file1.txt file2.txt other/ obelix[50]% mkdir somedir obelix[51]% ls file1.txt file2.txt other/ somedir/
Changing to another directory
Changing to a subdirectory of the current directory
To change to another directory, use the cd (change directory) command:
obelix[52]% pwd /student/jshantz4/tmp obelix[53]% cd other obelix[54]% pwd /student/jshantz4/tmp/other
Changing to the parent directory of the current directory
To change to the parent directory (the directory which contains the current directory as a subdirectory), enter .. as the directory name passed to cd:
obelix[52]% pwd /student/jshantz4/tmp/other obelix[53]% cd .. obelix[54]% pwd /student/jshantz4/tmp
Be sure to place a space between cd and .. or you will receive an error.
obelix[55]% cd.. cd..: Command not found.
Changing to your home directory
To return to your home directory, simply enter cd with no arguments:
obelix[56]% pwd /student/jshantz4/tmp/other obelix[57]% cd obelix[58]% pwd /student/jshantz4
Viewing the contents of a file
To view the contents of a file, use the cat (concatenate) command:
obelix[65]% ls file1.txt file2.txt other/ somedir/ obelix[66]% cat file1.txt Hello, world!
If the file is long and scrolls down the screen too fast, you can view it using the more command. This command displays the file one screen at a time, pausing on each screen until you press a key.
obelix[65]% ls file1.txt file2.txt other/ somedir/ obelix[66]% more file2.txt
Editing a file
Editing a file
While there are numerous text editors available for use in UNIX, you will likely find pico easiest to work with when you are first starting out.
obelix[65]% ls file1.txt file2.txt other/ somedir/ obelix[66]% pico file1.txtA text-based editor will then load, allowing you to edit your file. Use the arrow keys to move around the file.
Saving / Exiting
When finished editing your file, press Control+X (hold down Control and X simultaneously). You will be presented with three options:- Y - Save your changes and exit
- N - Exit without saving your changes
- Control+C - Do not save or exit
Downloading a file
Downloading a file
Sometimes it is useful to download files from the Internet at the command line. To do this, you can use the wget command. The address of the file to download is specified as an argument to wget. Multiple files can be downloaded at the same time by separating them with spaces.
obelix[20]% ls myfile.txt obelix[21]% wget http://www.csd.uwo.ca/courses/CS2208b/sum.s --2011-01-13 02:22:30-- http://www.csd.uwo.ca/courses/CS2208b/sum.s Resolving www.csd.uwo.ca... 129.100.23.247 Connecting to www.csd.uwo.ca|129.100.23.247|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 909 [text/x-asm] Saving to: `sum.s' 100%[======================================>] 909 --.-K/s in 0s 2011-01-13 02:22:30 (12.3 MB/s) - `sum.s' saved [909/909] obelix[22]% ls myfile.txt sum.s
Downloading a File Requiring Authentication
Sometimes, files are protected by a username and password. To specify such credentials to wget, use the --user and --password options:
obelix[20]% ls myfile.txt obelix[21]% wget --user jshantz4 --password secretpass http://www.csd.uwo.ca/courses/CS2208b/private/run1.pdf --2011-01-13 02:22:30-- http://www.csd.uwo.ca/courses/CS2208b/private/run1.pdf Resolving www.csd.uwo.ca... 129.100.23.247 Connecting to www.csd.uwo.ca|129.100.23.247|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 88499 (86K) [application/pdf] Saving to: `run1.pdf' 100%[======================================>] 88,499 --.-K/s in 0.02s 2011-01-13 02:29:01 (4.43 MB/s) - `run1.pdf' saved [88499/88499] obelix[22]% ls myfile.txt run1.pdf
Copying a file or directory
The cp command is used to copy files. Important: be careful with this command. If the destination file already exists, it will simply be overwritten, and you will not be asked for confirmation before this happens. Caveat emptor — buyer beware!.
Copying a file to a subdirectory
The first argument to cp should be the name of the file to copy, and the second argument should be the name of a directory to which the file should be copied:
obelix[71]% ls file1.txt file2.txt other/ somedir/ obelix[72]% cp file1.txt somedir/ # Copy file1.txt to the subdirectory somedir obelix[73]% cd somedir/ obelix[74]% ls file1.txt
Copying a file to the parent directory
You can also copy files to the parent directory, if desired:
obelix[94]% ls file3.txt obelix[95]% cp file3.txt .. # Copy file3.txt to the parent directory obelix[96]% cd .. obelix[97]% ls file1.txt file2.txt file3.txt other/ somedir/
Copying a file multiple directories above the current directory
If you need to copy a file to a directory several levels above the current directory, you can use the following command:
obelix[94]% ls file3.txt obelix[95]% cp file3.txt .. # Copy file3.txt to the parent directory obelix[95]% cp file3.txt ../.. # Copy file3.txt two directories above the current obelix[96]% cp file3.txt ../../.. # Copy file3.txt three directories above the current # and so on...
Making a duplicate of a file
If the second argument passed to cp is not the name of a directory, then the file will simply be copied to a new file with the specified name:
obelix[108]% ls file1.txt file2.txt obelix[109]% cp file1.txt NEWFILE # Make a copy of file1.txt named NEWFILE obelix[110]% ls NEWFILE file1.txt file2.txt obelix[111]% cat file1.txt # Display the contents of file1.txt Hello, world! obelix[112]% cat NEWFILE # Display the contents of NEWFILE Hello, world!
Copying a directory
To copy a directory, along with all files and directories contained within it, pass the -r flag (recursive) to cp
obelix[108]% ls file1.txt file2.txt other/ obelix[109]% cp -r other .. # Copy the 'other' directory to the parent directory obelix[110]% cp -r other www # Make a copy of the 'other' directory (and all its contents) named www obelix[111]% ls file1.txt file2.txt other/ www/
Moving a file or directory
The mv command is used to move files. Important: be careful with this command. If the destination file already exists, it will simply be overwritten, and you will not be asked for confirmation before this happens. Caveat emptor — buyer beware!.
Moving a file or directory to a subdirectory
The first argument to mv should be the name of the file or directory to move, and the second argument should be the name of a directory to which the file should be moved:
obelix[71]% ls file1.txt file2.txt other/ somedir/ obelix[72]% mv file1.txt somedir/ # Move file1.txt to the subdirectory somedir obelix[73]% ls file2.txt other/ somedir/ obelix[74]% cd somedir/ obelix[75]% ls file1.txt
Moving a file or directory to the parent directory
You can also move files or directories to the parent directory, if desired:
obelix[94]% ls file1.txt file2.txt other/ somedir/ obelix[95]% mv file2.txt .. # Move file2.txt to the parent directory obelix[96]% ls file1.txt other/ somedir/ obelix[97]% cd .. obelix[98]% ls file2.txt
Moving a file or directory multiple directories above the current directory
If you need to move a file to the directory several levels above the current directory, you can use the following command:
obelix[94]% ls file1.txt file2.txt file3.txt obelix[95]% mv file1.txt .. # Move file1.txt to the parent directory obelix[96]% mv file2.txt ../.. # Move file2.txt two directories above the current obelix[97]% mv file3.txt ../../.. # Move file3.txt three directories above the current # and so on...
Renaming a file or directory
To rename a file or directory, you can use the mv (move) command. Important: be careful with this command. If the destination file already exists, it will simply be overwritten, and you will not be asked for confirmation before this happens. Caveat emptor — buyer beware!.
Renaming a file
To rename a file, use the mv command. The second argument should be the new filename desired.
obelix[94]% ls file1.txt file2.txt other/ somedir/ obelix[95]% mv file2.txt h.txt # Rename file2.txt to h.txt obelix[96]% ls file1.txt h.txt other/ somedir/
Renaming a directory
To rename a directory, use the mv command. The second argument should be the new directory name desired.
obelix[94]% ls file1.txt file2.txt other/ somedir/ obelix[95]% mv other zzz # Rename other to zzz obelix[96]% ls file1.txt file2.txt somedir/ zzz/
Deleting a file or directory
To delete a file or directory, we use the rm (remove) or rmdir (remove directory) commands, respectively. Important: as always, be careful with these commands. You will not be asked for confirmation before files and/or directories are deleted. As soon as you issue the command, your files and/or directories are gone.
Deleting a file
To delete a file, use the rm command, passing to it a list of all files to be deleted. Multiple files can be deleted at the same time by separating them by spaces.
obelix[94]% ls file1.txt file2.txt file3.txt file4.txt obelix[95]% rm file1.txt # Delete file1.txt obelix[96]% ls file2.txt file3.txt file4.txt obelix[97]% rm file2.txt file3.txt # Delete several files obelix[98]% ls file4.txt
Deleting all files in the current directory
In UNIX, you can use the wildcard * to match to all files in the current directory. So, to delete all files in the current directory, pass * as an argument to rm:
obelix[94]% ls file1.txt file2.txt file3.txt file4.txt obelix[95]% rm * # Delete all files in the current directory obelix[96]% ls
Deleting all files of a certain type in the current directory
We may wish to restrict our deletion to only files of a certain type, such as all files with the extension txt. To do this, we again use the wildcard *, but this time we specify the extension we wish to delete:
obelix[94]% ls file1.txt file2.txt other1.doc other2.doc obelix[95]% rm *.txt # Delete all files with the extension .txt obelix[96]% ls other1.doc other2.doc
Deleting all files of certain types in the current directory
Suppose we wish to delete all files of two different types in a given directory, such as all files with the extensions txt and doc. To do this, we use the wildcard *, but this time we enumerate each extension between braces, separating each by a comma:
obelix[94]% ls
file1.txt other1.doc test.mp3
obelix[95]% rm *.{txt,mp3} # Delete all files with the extension .txt or .mp3
obelix[96]% ls
other1.doc
Deleting a directory
To delete a directory, use the rmdir command, passing to it the name (or a space-separated list of names) of the directory to delete:
obelix[94]% ls file1.txt dir1/ dir2/ dir3/ obelix[95]% rmdir dir1 # Delete the directory 'dir1' obelix[96]% ls file1.txt dir2/ dir3/
Note that the directory to be deleted must be empty; otherwise, you will receive an error and the directory will not be deleted:
obelix[94]% ls file1.txt dir1/ dir2/ dir3/ obelix[95]% rmdir dir2 # Delete the directory 'dir2' rmdir: directory "dir2": Directory not empty
Deleting a directory that is not empty
To delete a directory, along with all the files and subdirectories contained within it, use the rm command, passing to it the flags -rf (recursive, force):
obelix[94]% ls dir1/ dir2/ dir3/ obelix[95]% cd dir2 obelix[96]% ls file1.txt file2.txt file3.txt obelix[97]% cd .. obelix[98]% rm -rf dir2 obelix[99]% ls dir1/ dir3/
Checking your disk usage
The du command allows you to view the amount of space consumed by all files in the current directory, and all of its subdirectories.
Viewing the size of the current directory and all subdirectories
To view the size of the current directory and all subdirectories, simply use the du command with no arguments:
obelix[55]% ls dir1/ dir2/ dir3/ file1.txt file2.txt obelix[56]% du 21 ./dir2 33420 ./dir1 15493 ./dir3 48938 .
The output above can be interpreted as the follows:
- The files and directories within the subdirectory dir2 consume 21 KB
- The files and directories within the subdirectory dir1 consume 33420 KB (~33 MB)
- The files and directories within the subdirectory dir3 consume 15493 KB (~15 MB)
- The files in the current directory (.), along with all files and directories within its subdirectories (dir1, dir2, and dir3) consume a total of 48938 KB (~48 MB)
Viewing the size of the current directory and all subdirectories in "human readable" format
You may find the -h flag useful when using the du command. Rather than reporting all sizes in KB, it scales each size to a more human readable format:
obelix[55]% ls dir1/ dir2/ dir3/ file1.txt file2.txt obelix[56]% du -h 21K ./dir2 33M ./dir1 15M ./dir3 48M .
In the output above, one can see that different units are used to report the sizes of the various directories, making them more readable. You may see the following units:
- K - Kilobytes (1,024 bytes)
- M - Megabytes (1,048,576 bytes or 1,024 kilobytes)
- G - Gigabytes (1,073,741,824 bytes or 1,024 megabytes)
- T - Terabytes (1,099,511,627,776 bytes or 1,024 gigabytes)
Viewing a summary of the size of the current directory and all subdirectories
Usually, we do not wish to see a breakdown of the sizes of all subdirectories in a given directory. Instead, we simply want to know how much space the files in the current directory consume, along with all files in any subdirectories. To do this, we pass the -s flag:
obelix[55]% ls dir1/ dir2/ dir3/ file1.txt file2.txt obelix[56]% du -h -s 48M .
Changing your password
To change your GAUL password, use the passwd command. You will be prompted for your current password, and then asked to enter your new password twice. Note that the password characters that you type will not be displayed, for security purposes. At any time, you can cancel by pressing CTRL+C:
obelix[78]% passwd passwd: Changing password for jshantz4 Enter existing login password: New Password: Re-enter new Password: passwd: password successfully changed for jshantz4 passwd: credential information changed for jshantz4
Logging out
To log out of an existing session, you can use either the logout or exit commands:
obelix[83]% exit logout Connection to obelix.gaul.csd.uwo.ca closed.
Printing a file to a departmental printer
obelix[78]% lpr -Pmc342 myfile.ps # Print myfile.ps to the printer in MC 342
A list of printers is shown below. Note that the printer duplex can print on both sides of a page.
| Printer | Location | Sample Command | Cost / Page |
| lw | I/O Counter | lpr -Plw myfile.txt | $0.05 |
| duplex | I/O Counter | lpr -Pduplex myfile.txt | $0.05 |
| mc10 | MC 10 | lpr -Pmc10 myfile.txt | $0.05 |
| mc325 | MC 325 | lpr -Pmc325 myfile.txt | $0.05 |
| mc342 | MC 342 | lpr -Pmc342 myfile.txt | $0.05 |
| pa225 | Physics 225 | lpr -Ppa225 myfile.txt | $0.05 |
Checking your available print quota
Students are given 50 pages worth of print quota for each Computer Science course taken. Each printed page costs $0.05, which is automatically deducted from your quota. To check your remaining balance, use the chkquota command:
obelix[82]% chkquota User jshantz4 has $71.80 of quota and can print on printer personal
If you wish to purchase additional print quota, please see Angie in the main office (MC 355).
Getting help with a command
Getting help with a command
The man (manual) command allows you to view help (called a manpage) for a particular command. Manpages show all the different ways of using a command, but can be quite terse at times. Thus, you may need to read through the manpage for a given command several times before you fully understand how to use it.
obelix[36]% man ls # View the manpage for the ls command
Searching for a command
Sometimes, we might not know the command to use to do something we desire. In this case, we can search all manpages by keyword using the -k flag:
obelix[56]% man -k yes # Search all manpages for the keyword 'yes' ckyorn ckyorn (1) - prompts for and validates yes/no erryorn ckyorn (1) - prompts for and validates yes/no helpyorn ckyorn (1) - prompts for and validates yes/no valyorn ckyorn (1) - prompts for and validates yes/no yes yes (1) - generate repetitive affirmative output xeyes xeyes (6) - Eyes follow your pointer
In the output above, the first column shows the name of the command, while the third column describes the command briefly. The middle column shows the name and section number of the manpage in which the command is discussed. Notice that the commands ckyorn, erryorn, helpyorn, and valyorn are all discussed in section 1 of the ckyorn manpage. To get help with any of these commands, we would thus use the command man ckyorn.
Viewing a non-default section of a manpage
Manpages provide help not only for UNIX shell commands, but also for system commands, C library functions, file formats, and so on. Sometimes, when we want to view help on a particular command, there are numerous manpage sections from which to choose. For example, consider the search results for printf below:
obelix[56]% man -k printf # Search all manpages for the keyword 'printf' printf printf (1) - write formatted output printf printf (3c) - print formatted output printf printf (3ucb) - formatted output conversion
In the output above, there are three different manpage sections listed. If we type the command man printf, we will see section 1 (printf (1)) of the printf manpage. To view another section, you specify the section number before the command name:
obelix[57]% man printf # View printf (1) -- the default section obelix[58]% man 3c printf # View printf (3c) obelix[59]% man 3ucb printf # View printf (3ucb) obelix[58]% man 1 printf # View printf (1)
Note that we explicitly requested printf (1) in the last command above. This is not necessary since the first manpage is the default. The table below describes the different sections a manpage might have. You will most often want to look at section 1 of a given manpage, since this section pertains to commands you use at the command line.
| Section | Description |
| 1 | General commands |
| 2 | System calls |
| 3 | C library functions |
| 4 | Special files and drivers |
| 5 | File formats and conventions |
| 6 | Games and screensavers |
| 7 | Miscellaneous |
| 8 | System administration commands and daemons |

