UNIX Basics

Joanne Houlahan

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.

Command Forms

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 subdirectory
will 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 and Directories

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.

Help facilities, learning programs

man commanddisplays the UNIX manual entry for the command specified (such as ls)
nixtutorinteractive program for learning about and experimenting with UNIX command lines

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!!

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-cinterrupt, abort current command being executed
ctrl-ukill (undo), erase contents of current line
ctrl-h or BACKSPACEerase the previous character typed (BACKSPACE might not work on your computer, but ^h always does)
ctrl-dend of input while a programming is running; this has the effect of logging out if typed at the command prompt
ctrl-spauses output to the screen until ^q is pushed
ctrl-qcontinues output to the screen after a ^s
Keys that may not work as you expect

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.

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.
ls[-aCFR][name]list the files in the current (no argument) or named directory, or the files that match name (if wildcards are used)
-alist all the files (even those beginning with .)
-Clist files in multi-column format
-Fformatted listing of the files indicating their function (see above)
-Rrecursively list all subdirectories also
cpfromfile tofilemake a copy of fromfile called tofile
fromfile todirmake a copy of fromfile called fromfile in directory todir
mvfromfile tofilemove (rename) fromfile to tofile
fromfile todirmove (rename) fromfile to fromfile in directory todir
rmfile1 [file2 ...]removes file1 [file2 ...]
morefile1 [file2 ...]displays file1 [file2 ...] one screen at a time
catfile1 [file2 ...]displays (concatenates) file1 [file2 ...] on screen
lpfile1 [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
pwdprint working directory
mkdirdir1create subdirectory called dir1
rmdirdir1 [dir2 ...]remove (empty) directory dir1 [dir2 ...]
wgethttp://someURLweb get (download) a file from a URL
whoprint list of who is currently on the system
ps[-a]print the process id numbers (pid) and names of running processes
-aprint pids and names of all running processes
kill-9pidkill running process pid
grepstring fileprints the lines of file that contain string
sort[-dfooutfile]infilesorts the lines of infile
-ddictionary order (based only on letters, digits, spaces)
-ffold lower case into upper case (ignore case)
-ooutfileput the output in outfile

Redirecting input and ouput

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