Basic Linux/Mac OS X Commands and Capabilities
Abstract
The original Unix operating system and its successors, Linux and Mac OS X, were designed to support the common tasks and needs of software developers and computer users. This laboratory exercise reviews some basic commands and capabilities that match likely needs of students in this course.
Introduction
Both the Linux and Mac OS X operating systems have evolved from Unix, an operating system developed at AT&T Bell Labs in 1969-1972. At the time, Bell Labs was a major research and development enterprise, averaging approximately a patent a day over its first 40 years of existence. With this impressive track record, the company was delighted to encourage development of a computing environment that would support research and development; common computing tasks needed to be easy and quick. As a result, the original Unix was extremely efficient, although sometimes cryptic. Professionals found Unix wonderfully powerful, although beginners sometimes found that it took some time to master.
The current Linux and Mac OS X operating systems maintain the efficiency and power of the original Unix, with many extensions and revisions for modern needs. At the same time, Linux and Mac OS X also have interfaces that make it much easier to use than the original Unix — although it sometimes still reflects the original cryptic style.
This lab covers the following basic capabilities and commands.
Topic Category | Subtopics | Linux/Mac OS X Commands |
---|---|---|
Terminal Window | open | click on Desktop menu item |
copying between windows | left mouse button to highlight/copy; middle mouse button to paste | |
terminal utilities | sleep, arrow keys, history, cat | |
autocompletion | TAB | |
close | control d | |
Help | manual | man |
Directories and Files | paths | ., .., ~, /, pwd |
pathnames | absolute, relative, cd, ls, which, whereis | |
search paths | search path variable $PATH | |
file utilities | mkdir, rmdir, rm, cp, mv, more, head, tail | |
Printing | printing | lpr, a2ps, lpq, lprm |
The Terminal Window
When Unix was initially developed, users entered information by typing on a keyboard, and results were printed on paper or a screen. Almost all work was character based, with very little use of graphics. (Images require extensive memory resources that were beyond the capabilities of most early computers.) With this history, many of the early Unix capabilities depended upon typing commands into a terminal. On current machines, the terminal window continues to provide a wide range of powerful capabilities for many processing needs.
Opening the Terminal Window
Although you may be accustomed to a graphical user interface (GUI) from your past computer usage, you also need to become comfortable with a command-line interface. Historically, commands were typed into windows (actually, the entire terminal had just one screen — the only window). Unix, Linux, and Mac OS X provide several powerful tools for this type of interaction. To run various commands, you must invoke them by name. The computer program that reads and responds to such invocations is called the shell, and your interactions with the shell take place in a window generated by a program called a terminal emulator.
You may already have a terminal window open on your screen. If not, you can start one at any time by moving the pointer onto the small monitor icon on dock of the front panel (often found at the bottom of the screen). Click with the left mouse button for the small monitor icon. Shortly a window will appear, displaying the shell prompt — the name of the workstation on which the shell is running, followed by a percentage sign (%) or dollar sign ($). This prompt indicates that the shell is ready to receive instructions.
You enter such instructions using the keyboard. Move the mouse pointer into the terminal window to make it active. (You many need to click on the window, once your mouse is in the desired area.) The window frame may change color, indicating that the window has become active.
If you want to set the window aside for the moment, with the possibility of returning to it later, look closely at the upper right-hand corner of the window, where the frame contains a small bar or underscore character; if you move the pointer into that square and click on the left mouse button, you minimize the window, closing it up into a small rectangular icon along the bottom control panel on the screen. A minimized window can be restored by moving the pointer onto its icon and clicking the left mouse button twice in rapid succession.
Cutting and Pasting Between Windows
Both Linux and Mac OS X operating systems provide simple mechanisms to cut-and-paste or copy material from one window to another. Through this semester, this can be particularly helpful, when you want to work with material that appears on the lab directions in the Firefox viewer.
-
For Linux: To select material from Firefox, move the cursor to the beginning of a section and push down the left mouse button. Then, holding the button down, move the mouse to the end of the section. (The entire section now should be highlighted.) When the desired section is highlighted, stop pressing on the left mouse button — the section should stay highlighted. Now move the mouse to where you want to paste the material, and click the middle mouse button.
-
For Mac OS X: To select material from Firefox, move the cursor to the beginning of a section and push down the left mouse button. Then, holding the button down, move the mouse to the end of the section. Once the desired section is highlighted,
- Remove the material by pressing the "Open Apple" or "Command" key at the same time as the "X" key.
- Copy the material by pressing the "Open Apple" or "Command" key at the same time as the "C" key.
Now move the mouse to where you want to paste the material, and hold the "Open Apple" or "Command" key down while pressing the "V" key.
Terminal Utilities
Although the basic running of programs within a terminal window may seem quite straightforward, a few nifty shortcuts can simplify your work considerably.
Stopping a Long or Infinite Loop
Suppose you wrote a program that contained an infinite loop, and you ran it from the terminal window. How could you stop the program?
One way is to close the window, but a better way is to type ctrl-c (i.e., hold down the ctrl key while you type c). This should kill the program without making you re-open the window and re-navigate to the directory you were working in.
The Arrow Keys
Suppose you want to run a command that you ran a short while ago? If the command is complicated, it would be nice not to have to re-type it, and you don't have to! Within the terminal window, this is accomplished using the up-arrow key to retrieve previous commands. Once you have viewed some earlier commands, note that the down-arrow works similarly to view earlier commands— a helpful feature if you go too far.
The history Command
But what if the command you want was several commands ago, say 10? Who wants to type that many arrows? To see another option, first type the following to get a list of the commands you have used most recently.
history
Typically, a terminal window remembers the last 50 commands (although a user can change this number). At the start, the history list might look something like this.
505 sleep 10 506 history
In this listing, each command is numbered for future reference. For example, with this listing, if you want to re-issue the sleep command, you could do so by typing "!505" to repeat command number 505. Better yet (since you don't need to know the command number), you can repeat the command with "!s". This will re-issue the most recent command that begins with the prefix "s". Note that the prefix can be of any length; it is not limited to one character.
cat
It is often convenient to look at the contents of a text file without having to open it in an editor. The simplest command for this purpose is cat. For example,
cat .bashrc
would display the contents of the .bashrc file in your terminal window. Although cat works well for short files that can be viewed all on one screen, we will see shortly that other commands may work better for longer files.
As an aside, note that the command name cat stands for "concatenate" for reasons we will explore later.
Autocompletion
Another very useful keyboard shortcut is called autocompletion. Suppose you want to do something with a file called my-test-file in your home directory. That file name seems a bit long to type very often, so it's nice that you don't have to.
To use autocompletion, type the command and the first part of the file name (e.g., "cat ~/my-t"), followed by a TAB key. You will find that when you press TAB, the system completes the (unique) file name for you.
If there isn't a unique completion, the system may add a few letters and beep. This tells you that there are multiple files that have these beginning letters. On the other hand, if no files begin with the given file name, the system likely will just beep at you.
Closing the Terminal Window
One way to shut down the terminal window is to type exit.
However, a quicker (and more common) approach is just to type <Ctrl/d> — that is, hold down a key marked <Ctrl>, just below the <Shift> key, and simultaneously press the <d> key. (On many workstations' keyboards, the keys marked <Ctrl> ("control") and <Alt> ("alt" or "meta") are somewhat like <Shift> keys, in the sense that they modify the effect of other keys that are pressed simultaneously. The "Open Apple" or "Command" key on a Macintosh computer also has a similar function.) The shell program interprets <Ctrl/d> as a signal that you have no more instructions for it and halts, and the terminal emulator closes the window automatically once the shell stops running.
Although the shortcut ctrl-d is convenient, note that this key combination is used in multiple contexts in the Linux and Mac OS X operating systems to indicate the "end-of-input" character. Thus, when using a simple terminal window, pressing ctrl-d at a command prompt indicates that there will be no more commands forthcoming, and the terminal window should close. However, if you are using another application, the ctrl-d once may exit the application and a second ctrl-d will close the window.
For historical reasons, the "Open Apple" or "Command" keys on Mac OS X often are not used in conjunction with terminal-window commands.
The Manual
Both Linux and Mac OS X include an on-line help system, called the manual. There is a "man page" describing each Linux and Mac OS X command and each C library function. Don't worry if you don't understand everything you see in a man page. They are often long and cryptic, but you can learn a lot without having to understand everything.
For example, try typing "man cat" to read about cat. You should see the command name and a quick synopsis of how to use the command. For example, the synopsis "cat [OPTION] [FILE] ..." tells you that cat (optionally) takes a file as its input, and it also can take (optional) options as parameters. A list of the options follows the synopsis. To exit a man page, type 'q'.
Flags
The manual for a program will also describe all the options
available. Options are usually prefixed by a - (dash) and referred to as
a flag. If a flag is a single letter it only needs one dash. However,
longer flags are prefixed by two dashes. This convention allows multiple
single letter flags to be smashed together behind one single dash. For
example, you can type:
ls -l -a -h
to list all the files in a directory in
the long-listing human-readable format. Or, you can simply type:
ls -lah
Another handy use for the man pages is finding commands or C functions when you don't remember, or never knew, their names. Suppose you wanted to find a function for computing the square root: you could guess that such a function might exist, but you might not know its name. To print a list of man pages that include the word "square" in the name or description fields, you could use "man -k square".
Of course, keyword searches are more useful for some commands than others: "man -k print" may reap you more results than you want to deal with!
Directory and File Commands
The Linux/Mac OS X Directories and the File Hierarchy
Linux and Mac OS X maintain directories and files in a hierarchical structure, called a tree structure. This section explores this organization.
Pathnames
When you first open a terminal window, the shell expects that you are in your home directory. At the start, this is called your current "working directory" (i.e., your position within the directory tree).
A relative pathname for a file is a name that is given "relative" to your current working directory. For example, if your current working directory is csc161, then scheme/hw2.s could be a relative pathname for a file named hw2.s located in a directory named scheme that was itself inside csc161.
An absolute pathname, such as /home/username/csc161/scheme/hw2.s on Linux or /Users/username/csc161/scheme/hw2.s on Mac OS X, includes the file's complete path starting with the system's "root" directory, which is always named "/" on a Linux or Mac OS X system. Just like it sounds, the root directory is the topmost directory in the file system tree.
Each directory in a Linux or Mac OS X system contains two special files "." and ".." that can be useful when constructing relative pathnames. The file named "." means "the current directory," and the file named ".." means "the parent directory". For example, ../c could be a directory that is a sibling of your current working directory (i.e., c could be a directory that has the same parent directory your current working directory does). Then ../c/labs/hw2.c could refer to a file farther down that branch of the tree. Similarly, ../../pix/grinnell could be a directory that is a cousin of your current directory in the file system tree.
The tilde character is also useful for specifying pathnames, but it works a little differently. Used alone, it specifies your home directory, so ~/csc161/hw is a short name for /home/username/csc161/hw on Linux or for /Users/username/csc161/hw on Mac OS X.
Finally, ~username refers to the home directory belonging to username. Thus, you can print a listing of the files in my public_html directory with
ls -l ~walker/public_html
Root Directory and its Subdirectories
While we are poking around a Linux or Mac OS X file system, take a look at the files in the root directory /. You should see directories with names like /bin, /home, /lib, and /usr.

Again, list the files in each of these directories. They contain many, many files, organized as follows.
-
/bin: These are the executable programs that comprise the GNU/Linux/Mac OS X utilities. For example, there is an executable file here named ls that is run when you issue the command ls.
-
/home or Users: You might not be surprised to learn that user accounts are stored in this directory.
-
/lib: This directory is the home of several libraries that can be used by programmers.
-
/usr: The name of this directory is pronounced "user", and it generally contains application programs, libraries, and other files that are not part of the GNU/Linux/Mac OS X system (i.e., optional resources intended for and requested by users). For example, the gcc compiler is found in the subdirectory /usr/bin on many Linux and Mac OS X systems.
Sometimes locating a program can be something of a challenge, but the commands which and whereis can help you solve this problem. For example, both
whereis ls
and
which ls
will give you the absolute path name of the ls program.
Search Paths
When you type the name of a command in a terminal window, the operating system must determine the appropriate program to run. If you type a full path name, the operating simply uses that program. For example, if you type /bin/ls, the operating system will run the ls program located in the /bin directory.
Similarly, you can clarify that the operating system should use a relative path by starting a command with a period (. represents the current directory). For example, ./test tells the operating system to run the program test in the current directory.
However, if you type a command without specifying its location, the operating system must determine where to find the relevant program. To facilitate this process, the operating system maintains a variable $PATH for each terminal window. For example, the variable might be set to
/usr/bin:/bin:/usr/X11R6/bin:/usr/local/bin:/net/bin:.
This string is interpreted as a sequence of directories, separated by a colon :. For example, suppose you type pwd in a terminal window. The operating system first tries to find a program pwd in directory /usr/bin. However, no such program exists there, so the operating system looks at the next directory, /bin. In this case, a program pwd is found, so the operating system uses this program to respond to the command in the terminal window. (If file pwd was not present in /bin, the operating system would continue its search with /usr/X11R6/bin, etc.)
In this example, note that the last option is . (the abbreviation for a program in the current directory). Thus, the operating would run a program in your current directory — but only if there was not a program of the same name in one of the earlier directories listed in the $PATH variable.
File Utilities
Some common file management commands are listed in the table below. When you have the chance you should try each of these to determine just how they work.
Utility | Description |
ls | "list" files and directories |
pwd | "print working directory" |
cd | "change (your working) directory" |
mkdir | "make directory" |
rmdir | "remove directory" |
cp | "copy" a file or directory |
mv | "move" a file or directory (i.e., rename it) |
rm | "remove" a file (i.e., delete it) |
The following variants can be particularly handy. (Although some details may not be obvious now, we will see shortly how to find out more information about such commands.)
cd .. cp -p ls -l mkdir -p
Displaying Text Files
It is often convenient to look at the contents of a text file without having to open it in an editor. Previously in this lab, we saw that cat can be used for this purpose, but it is most useful for short files that can be viewed all on one screen.
GNU/Linux and Mac OS X provide several other utilities that are useful for "paging" through text files (i.e., for viewing files one page at a time). Several of these commands are outlined in the following table.
Command | Description |
---|---|
more | move through a file screen by screen (hit space for the next page, return for one more line) |
less | a new and improved version that allows backward paging as well with the up-arrow, down-arrow, Page Up, and Page Dn. |
head | show the first few lines of a file |
tail | show the last few lines of a file |
Printing
You likely have printed files already. This is frequently done from within another program, say a web browser, pdf reader, or text editor. With the following commands, you can also print files, and manage print queues, from the command line.
Utility | Description |
a2ps file a2ps -Pduerer file a2ps --sides=duplex file |
prints file to default printer (handles many standard file
formats) same, for printer named duerer same, but double-sided |
lpr file lpr -Pduerer file |
prints file to default printer (handles several standard file
formats) same, for printer named duerer/td> |
lpq lpq -Pduerer |
displays jobs in print queue on default printer same, for printer named duerer |
lprm 585 lprm -Pduerer 585 |
cancels (removes) print job number 585 from default printer queue same, for printer named duerer |
Please don't test these out now, unless you have something you really want printed, but keep them in mind for next time you do.
created 31 March 2008 by Henry M. Walker revised 18 July 2011 by David Cowden last full revision 21 September 2011 by Erik Opavsky minor editing 25 September 2011 by Henry M. Walker editing (added lpr) 25 January 2012 by Henry M. Walker editing (acroread references to evince) 2 September 2013 by Henry M. Walker editing (evince references to emacs) 15 January 2014 by Henry M. Walker extended to Mac OS X 6 May 2016 by Henry M. Walker modified for SSU CS introductory labs 28 january 2022 by Henry M. Walker |
![]() ![]() |
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |