Introduction to the Unix command line

by Benjamin P Haley

Version 4
by (unknown)
Version 38
by (unknown)

Deletions or items before changed

Additions or items after changed

1 = History =
2 -
Most popular computer operating systems, like Microsoft Windows and Mac OS X, offer primarily (but not exclusively) graphical user interfaces. The user interacts with files and programs by pointing and clicking with a mouse. Unix, which is a considerably older operating system than Windows or OS X, also offers a graphical user interface (more than one, actually), but Unix originally presented a ''command line interface'', in which the user interacts with the operating system by typing commands at a text prompt and reading textual output from programs. The ''command line interface'' is alive and well in Unix today, and many Unix users consider it the most powerful and efficient system for basic operations like navigating through directories.
+
Many contemporary computer operating systems, like Microsoft Windows and Mac OS X, offer primarily (but not exclusively) graphical user interfaces. The user interacts with files and programs by pointing and clicking with a mouse. Unix, which is a considerably older operating system than Windows or OS X, also offers a graphical user interface (more than one, actually), but Unix originally presented a ''command line interface'', in which the user interacts with the operating system by typing commands at a text prompt and reading textual output from programs. The command line interface is alive and well in Unix today, and many Unix users consider it the most powerful and efficient system for basic operations like navigating through directories and handling files.
3
4 = Terminology =
5 -
In this document, we use the term Unix to refer to a family of operating systems. The look and feel of the graphical interfaces of these systems, as well as the file system layout, may differ significantly from one system to the next, but they all have their (conceptual) roots in the original Unix developed at Bell Labs. This family of operating systems includes Linux (also called GNU/Linux) distributions like Debian, Red Hat, and Ubuntu, as well as Sun Solaris. Mac OS X is a curious case; many of its users never touch the ''command line interface'', but all the commands described below are all available in OS X. Many entertaining arguments about whether OS X is a true Unix can be found in programmer internet forums. Microsoft Windows offers a different ''command line interface'' than Unix, but a program called [http://www.cygwin.com/ Cygwin] provides a Unix interface for Windows.[[BR]][[BR]]
+
In this document, we use the term Unix to refer to a family of operating systems. The look and feel of the graphical interfaces of these systems, as well as the file system layout, may differ significantly from one system to the next, but they all have their (conceptual) roots in the original Unix developed at Bell Labs. This family of operating systems includes Linux (also called GNU/Linux) distributions like Debian, Red Hat, and Ubuntu, as well as Sun Solaris. Mac OS X is a curious case; many of its users never touch the command line interface, but all the commands described below are all available in OS X. Many entertaining arguments about whether OS X is a true Unix can be found in programmer internet forums. Microsoft Windows offers a different command line interface than Unix, but a program called [http://www.cygwin.com/ Cygwin] provides a Unix interface for Windows.[[BR]][[BR]]
6 We also use the abbreviation OS to mean "operating system".
7
8 = Opening a Terminal =
9 -
In a modern Unix OS (that is, one with a graphical user interface) the ''command line interface'' is accessed through a program called a terminal. Below we show how to run a terminal program in some of the more popular graphical Unix environments, including GNOME and KDE, available in most Linux distributions, as well as Mac OS X.
+
In a modern Unix OS (that is, one with a graphical user interface) the command line interface is accessed through a program called a Terminal. Below we show how to run a terminal program in some of the more popular graphical Unix environments, including GNOME and KDE, available in most Linux distributions, as well as Mac OS X.
10
11 === GNOME ===
12 In the GNOME desktop, click the Applications menu (typically in the upper left corner); in the Accessories menu, choose Terminal.[[BR]]
13 [[image(gnome_terminal.png)]]
14
15 === KDE ===
16 In the KDE desktop, click the main menu (with K superimposed over a gear), typically in the lower left corner; in the System menu, choose Konsole (the KDE terminal program).[[BR]]
17 [[image(kde_konsole.png)]]
18
19 === Mac OS X ===
20 In a Finder window, choose the Utilities folder.[[BR]]
21 -
[[image(osx_utilities.png)]][[BR]]
+
[[image(osx_utilities.png)]][[BR]][[BR]]
22 In the Utilities folder, select the Terminal application.[[BR]]
23 [[image(osx_terminal.png)]][[BR]]
24 -
[[BR]]
+
25 -
You should see a new window on your screen, with a prompt and possibly a cursor. The prompt might be a simple character, like '''$''' or '''%''', or it
+
== A new window ==
26 -
might be the name of your computer, like '''coates\[1\]'''. Regardless of the color of your window or cursor or the wording of your prompt, this terminal
+
The steps listed above should produce a new window on the screen, with a prompt and possibly a cursor. The prompt might be a simple character, like '''$''' or '''%''', or it might be the name of the computer and a number showing how many commands have been entered, like {{{coates[1]}}}. Regardless of the color of the window or cursor or the wording of the prompt, this terminal window is a Unix command line interface.
27 -
is a Unix ''command line interface''.
+
28
29 = Shell =
30 -
The prompt you see in the terminal window is initial output of a program called the shell. The shell is the Unix command interpreter (it is actually a separate program from the terminal window program, but the shell starts automatically when a terminal window opens). Unix offers several different shells, but for most of what we cover in this document, the differences between the shells are negligible.[[BR]][[BR]]
+
The prompt in a terminal window is the output of a program called the shell. The shell is the Unix command interpreter. (It is actually a separate program from the terminal window program, but the shell starts automatically when a terminal window opens and exits when the terminal window closes). Unix offers several different shells, but for most of what we cover in this document, the differences between the shells are negligible.
31 -
The shell reads the characters typed next to the prompt in the terminal, until Enter or Return is typed. After Enter or Return, the shell interprets all the input characters as a command, runs the command, and returns any text output from the command to be displayed in the terminal.[[BR]][[BR]]
+
[[BR]][[BR]]
32 -
What kind of commands can be typed at a shell prompt? Read on...
+
The shell reads the characters typed next to the prompt in the terminal, until the user hits either the Enter key or the Return key. After Enter or Return, the shell interprets all the input characters as a command, runs the command, returns any text output from the command to be displayed in the terminal, and, finally, displays a new prompt, waiting for the next command.
33 +
34 +
= File names and directories =
35 +
Since most of the commands below involve files and directories, we should consider the rules for naming files and directories. Unix file names may contain letters (a-z, A-Z), numbers (0-9), and a few other characters, like the dot (.) and underscore (_). Certain characters should ''not'' be used in a file name, like slash (/), asterisk (*), ampersand (&), percent (%), and space ( ). These characters have special meaning in the Unix file system, or to the shell, and should be avoided in file names. Files whose names begin with a dot (.) are handled specially by some of the commands considered below.
36 +
[[BR]][[BR]]
37 +
Unix files live in directories, which are analogous to folders in Microsoft Windows. A directory is simply a collection of files. All directories exist in a hierarchy, which begins at the ''root directory'', which is denoted by a slash (/). The ''full path'' of a file indicates its position in the directory hierarchy. For example, the full paths {{{/file1}}} and {{{/file2}}} refer to two separate files, {{{file1}}} and {{{file2}}}, which both reside in the root directory (hence the {{{/}}} in the full path). The full path always begins with {{{/}}}. As another example, consider the full paths {{{/dir1/file1}}} and {{{/dir1/dir2/file2}}}. These paths refer, again, to two separate files, but in this case the files exist in different directories. The first file, {{{file1}}} lives in the directory {{{dir1}}}, which lives in the root directory {{{/}}}. The second file, {{{file2}}}, lives in the directory {{{dir2}}}, which is a subdirectory of {{{dir1}}}. Note that the slash (/) separates directory names in the full path, as well as representing the root directory.
38 +
[[BR]][[BR]]
39 +
Directory names follow the same rules as file names.
40 +
41 +
= Basic commands =
42 +
This section introduces the basic Unix command line ''utilities'', programs designed to do one specific task. The example images show a grey terminal window with black text and a prompt of the form {{{coates[N]}}}, where {{{N}}} is the number of commands which have been entered already.
43 +
[[BR]]
44 +
[[image(ex_terminal.png)]]
45 +
46 +
== ls - list files ==
47 +
Perhaps the most basic of all Unix commands, ''ls'' lists the files in a directory. If no argument is given to ls, as in this example
48 +
[[BR]]
49 +
[[image(ex_ls.png)]]
50 +
[[BR]]
51 +
certain files in the current directory are shown. Which files are shown and which are not shown? Read on...
52 +
53 +
=== ls -a ===
54 +
Files whose name begins with dot (.) are ''not'' shown by default in the output of ls. Such files are called ''hidden files'' or ''dot files''. To see the hidden files in a directory, use the -a argument to ls. This example shows the additional output from ls, listing the hidden files which were not reported by the first invocation of ls.
55 +
[[BR]]
56 +
[[image(ex_ls-a.png)]]
57 +
[[BR]]
58 +
Two entries always appear in the output of ls -a, even in empty directories which contain no files: {{{.}}} and {{{..}}}, which are shortcuts to the current directory and the parent directory, respectively.
59 +
60 +
=== ls -F ===
61 +
Some of the results listed in the output of ls are subdirectories, while others are simply files. In order to distinguish between files and directories we can use the -F argument (also called a ''flag'') to ls.
62 +
[[BR]]
63 +
[[image(ex_ls-f.png)]]
64 +
[[BR]][[BR]]
65 +
Arguments can be grouped together. For example, {{{ls -a -F}}} and {{{ls -F -a}}} both list all files, including hidden files, and add a trailing slash (/) to
66 +
directory names. The order of arguments (e.g. {{{-a -F}}} vs {{{-F -a}}}) does not matter.
67 +
[[BR]]
68 +
[[image(ex_ls-a-f.png)]]
69 +
70 +
=== ls ''path'' ===
71 +
The ls command also accepts a directory argument. This example shows a listing of the files in specific subdirectories of the current directory. Note that the -F flag adds a trailing slash (/) to the names of directories.
72 +
[[BR]]
73 +
[[image(ex_ls-f-path.png)]]
74 +
[[BR]]
75 +
[[BR]]
76 +
The shortcut {{{..}}} can be used to see the files in the parent directory.
77 +
[[BR]]
78 +
[[image(ex_ls-f-parent.png)]]
79 +
[[BR]]
80 +
In the parent directory listing we can see the name of the current directory (bhaley, in this case) because the current directory is a subdirectory of the parent directory.
81 +
82 +
== pwd - print working directory ==
83 +
The ''pwd'' command shows the full path of the current (working) directory. In the example above we saw the name of the current directory in the listing of the contents of the parent directory. In this example we see the full path of the current directory, bhaley.
84 +
[[BR]]
85 +
[[image(ex_pwd.png)]]
86 +
[[BR]]
87 +
The name of the parent directory, then, is u101.
88 +
89 +
== cd - change directory ==
90 +
The ''cd'' command changes the current (working) directory. In this example we use the parent directory shortcut {{{..}}} as the argument to cd, which changes the working directory one level up in the directory hierarchy. This can be seen by the output of the pwd commands before and after the cd command.
91 +
[[BR]]
92 +
[[image(ex_cd_pwd.png)]]
93 +
[[BR]]
94 +
[[BR]]
95 +
If no argument is passed to cd, the working directory reverts back to the user's home directory. The home directory is where a Unix shell begins by default.
96 +
[[BR]]
97 +
[[image(ex_cd_pwd_2.png)]]
98 +
[[BR]]
99 +
A tilde (~) is used as a shortcut to the home directory; thus {{{cd}}} and {{{cd ~}}} will both change the working directory to the home directory and
100 +
the path {{{~/mydir/myfile}}} indicates a file {{{myfile}}} in a subdirectory {{{mydir}}} of the home directory.
101 +
102 +
== mkdir - create new directory ==
103 +
104 +
== cp - copy ==
105 +
106 +
== mv - move (rename) ==
107 +
108 +
== rm - remove (delete) ==
109 +
110 +
== rmdir - remove directory ==
111 +
112 +
== clear - clear screen ==
113 +
114 +
== cat - display file ==
115 +
116 +
== man - manual ==
117
118 -
= Basic file commands =
+
== Slightly more advanced topics ==
119 +
=== grep - search ===
120 +
=== | - pipe output ===
121 +
=== {{{<<}}}, {{{>>}}} - redirect ===
122 +
=== *, ? - wildcards ===
123 +
=== ssh - remote access ===
124
125 -
= Links =
+
= Links =
126 +
== Other useful Topics ==
127 +
* [/topics/SoftwareDevelopment Software Development]
128 +
* [/resources/5923 Using Subversion], a source code version management system
129 +
== Other websites ==
130 +
* [http://jura.wi.mit.edu/bio/education/bioinfo/pages/scripts/unix_cheat_sheet.html Unix cheat sheet] short summaries of key commands
131 +
* [http://www.ee.surrey.ac.uk/Teaching/Unix/ Unix tutorial]