This handout contains general information about using the UNIX operating system, as well as descriptions of facilities for learning more about specific UNIX commands. The main difference between a UNIX based machine and a Windows machine is that we must type our commands in order to work with files and programs, which means we have to learn how to use the commands before we start. We don't have menus and icons to choose from. In what follows, UNIX command names are typed in bold, options are indicated in square brackets [], and identifiers which must be replaced with specific names appear in italics.
If you want to practice these commands but don't have access to a unix/linux server (yet), try this website.
When you log into a unix account, the primary interface is a prompt which is waiting for you to type a command. Most UNIX commands are given in the form of a command name, followed by a list of options (not required), followed by a list of arguments. The options list is preceded by a minus sign (-) and consists of one or more characters, each of which represents a different option. The arguments are typically filenames or directories. In some cases an option itself may require an argument, in which case the option is separated from the other options by spaces and another minus sign. For example, the command line
ls -al subdirectorywill list all (-a option) the files in subdirectory, using the long listing format (-l option). Here, ls is the command name, -a and -l are both options shown combined, and subdirectory is an argument of the ls command indicating the name of the directory (file folder) that you want to see. The most general form or format of a specific command is also called its "syntax" and can be found in the unix manual entry for that command.
Files on the UNIX system are grouped into directories, same as folders in a Windows environment. A directory is a collection of files and other (sub)directories. Note that files and directories must NOT have spaces in their names in a unix environment!
The "highest" directory on a unix machine is called the "root directory", and is represented by a single slash (`/'). The root directory corresponds roughly to the Desktop on a Windows PC. The list of directories starting at root which lead to a specific file location is called the "directory path" or simply the "path" of the file. When typing a path, each directory is separated from the next by a (forward) slash. [This differs from a Windows machine which uses backslashes.]
Each user account on a unix server machine is associated with a directory called its "home directory" into which the user is automatically placed upon logging in. For example, account login jhoulah1 has the home directory /users/eng/cs/6/.jhoulah1 which means that the files and subdirectories associated with that account are located in the '.jhoulah1' directory (directory corresponding to the login), which is located in the '6' directory, which is located in the cs directory, which is located in the 'eng' directory (directory for the engineering school), which is located in the 'users' directory (directory of all users), which is located in the root directory (/). Thus, if jhoulah1 has a file called cal06, the full path name of that file is "/users/eng/cs/6/.jhoulah1/cal06".
The cd (change directory) command is used to move from one directory to another. For example, if you want to go forward into a sub-directory named personal, you would type "cd personal". This corresponds to double clicking on a folder on a Windows machine. If you want to go backwards into the directory containing the one where you are, you have to use two dots to refer to this "parent directory", as in "cd ..". The directory in which a user is working is called the "current directory" and the pwd (print working directory) command can be used to get the full path of the current directory. When referring to a file in the current directory, it is not necessary to specify the full path name. In order to access a file in a subdirectory of the current directory, one can either change to that directory or use a partial path name that would look like subdirectory/filename.
The ls (list) command is used to display the names of the files and
subdirectories in the current directory. There are three basic types of files.
The most common is a file of text. A directory is also considered a special
type of file. Thirdly, a file can be executable. The -F option for
the ls command will identify the types of files when they are listed by
displaying a slash (/) after directory names and an asterisk (*) after
executable filenames. Note that these characters are not part of the
directory or file names; they simply indicate the file type based on its
function. It is similar to how different icons are used to indicate file
types on a windows machine.
The man command and the nixtutor program can both be used to learn about UNIX commands. However, the
amount of a priori knowledge of UNIX commands necessary to use these
programs differs. The man command can only be used to look up very
detailed usage of commands whose names one already knows. With the
nixtutor program (written by former JHU students) one can learn about
basic commands and practice using them in a protected environment. There is
also some information on advanced commands. I highly recommend running the
nixtutor program the first time you get into your unix account!!
Often the delete (DEL) and arrow keys do not work the way you want or
expect them to in a UNIX environment. If you try them and get garbage, or
nothing seems to be happening, then hit ^u to undo that command line and start
over with a new prompt. [Within particular programs (such as pico), the arrow
keys are effective.]
You can use two options with the ls command to see if any of your
files contain invisible characters as a result of using these keys. The
command ls -ab will list all your files and show the non-printing
characters in octal (base 8) notation. The command ls -aq will list
all your files and show the non-printing characters as '?'s. In order to
access files with non-printing characters in their names (for instance, to
rename them or copy them to files with the correct filename) you can use the
'*' or '?' wildcard characters (as described above) to substitute for the
non-printing characters when typing the filename.
Help facilities, learning programs
Command Function man command displays the UNIX manual entry for
the command specified (such as ls)
nixtutor interactive program for learning about
and experimenting with UNIX command lines
Special characters used as abbreviations or substitutions
The following character combinations can be used with various commands to save
you typing. For example, ls .. will list the contents of the current
parent directory and ls *.html will list all files in the current
directory with extension html.
. represents the current directory
.. represents the parent directory of the current directory
~ represents your home directory
* represents any string of characters (a group wild card)
? represents any single character (another wild
card)
Special keys
Control key functions
There are some commands which are used to send special signals to the
operating system, mostly related to the typing of command lines. In order to
execute them, hold down the CONTROL (ctrl) key
while striking the appropriate letter key (lower case).
ctrl-c interrupt, abort current command being executed
ctrl-u kill (undo), erase contents of current line
ctrl-h or BACKSPACE erase the previous character typed
(BACKSPACE might not work on your computer, but ^h always does)
ctrl-d end of input while a programming is running;
this has the effect of logging out if typed at the command prompt ctrl-s pauses output to the screen until ^q is pushed
ctrl-q continues output to the screen after a ^s
Keys that may not work as you expect
Commonly used commands and options
If you go through the nixtutor program, there is an explanation of and
opportunity to experiment with many of these operations.
Command | Options | Arguments | Description |
---|---|---|---|
ls | [-aCFR] | [name] | list the files in the current (no argument) or named directory, or the files that match name (if wildcards are used) |
-a | list all the files (even those beginning with .) | ||
-C | list files in multi-column format | ||
-F | formatted listing of the files indicating their function (see above) | ||
-R | recursively list all subdirectories also | ||
cp | fromfile tofile | make a copy of fromfile called tofile | |
fromfile todir | make a copy of fromfile called fromfile in directory todir | ||
mv | fromfile tofile | move (rename) fromfile to tofile | |
fromfile todir | move (rename) fromfile to fromfile in directory todir | ||
rm | file1 [file2 ...] | removes file1 [file2 ...] | |
more | file1 [file2 ...] | displays file1 [file2 ...] one screen at a time | |
cat | file1 [file2 ...] | displays (concatenates) file1 [file2 ...] on screen | |
lp | file1 [file2 ...] | prints file1 [file2 ...] on the default printer (if there is one) | |
cd | [dir1] | change to directory dir1 if it is specified, or to the home directory if none is given | |
pwd | print working directory | ||
mkdir | dir1 | create subdirectory called dir1 | |
rmdir | dir1 [dir2 ...] | remove (empty) directory dir1 [dir2 ...] | |
wget | http://someURL | web get (download) a file from a URL | |
who | print list of who is currently on the system | ||
ps | [-a] | print the process id numbers (pid) and names of running processes | |
-a | print pids and names of all running processes | ||
kill | -9 | pid | kill running process pid |
grep | string file | prints the lines of file that contain string | |
sort | [-dfooutfile] | infile | sorts the lines of infile |
-d | dictionary order (based only on letters, digits, spaces) | ||
-f | fold lower case into upper case (ignore case) | ||
-ooutfile | put the output in outfile |
Unless otherwise specified, the default input and output devices are the keyboard and the screen. However, a few special characters can be used in conjunction with many commands to redirect standard input and output. They are the greater than symbol (>), the less than symbol (<) and the pipe (|).
command > outfile | the output of command is stored in outfile instead of being displayed on the screen |
command < infile | the input of command is read from infile instead of from the keyboard |
command1 | command2 | the output of command1 is used as input to command2 |
If you want to practice these commands but don't have access to a unix/linux server, try this website.