Copyright © 2008, 2009 Joshua Glatt
License
This document is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.
Legal Notice
This document is distributed in the hope that it will be useful, but it is provided “as is” without express or implied warranty of any kind; without even the implied warranties of merchantability or fitness for a particular purpose.
Although the author makes every effort to make this document as complete and as accurate as possible, the author assumes no responsibility for errors or omissions, nor does the author assume any liability for incidental or consequential damages in connection with or arising out of the use of the information contained in this document.
The author provides links to external websites for informational purposes only and is not responsible for the content of those websites. The inclusion of a link to an external website from anywhere in this document does not imply an endorsement of that site nor of the information, products, or services offered there.
Trademarks
Linux is a registered trademark of Linus Torvalds. Microsoft and Windows are registered trademarks of Microsoft Corporation. Mac OS is a registered trademark of Apple Inc. UNIX is a registered trademark of The Open Group. Google is a trademark of Google Inc. Wikipedia is a registered trademark of the Wikimedia Foundation, Inc. Eclipse is a trademark of the Eclipse Foundation, Inc. GNOME is a trademark of the GNOME Foundation. KDE and K Desktop Environment are registered trademarks of KDE e.V. O'Reilly is a registered trademark of O'Reilly Media, Inc. All other trademarks and registered trademarks are the property of their respective owners. Use of a term in this document should not be regarded as affecting the validity of any trademark or service mark.
Revision History | ||
---|---|---|
Revision Snapshot | 02 Aug 2009 | jg |
Revision 1.31 | 14 Sept 2008 | jg |
Various small but useful changes, preparing to revise section on vi | ||
Revision 1.30 | 10 Sept 2008 | jg |
Revised further reading and suggestions, other revisions | ||
Revision 1.20 | 27 Aug 2008 | jg |
Revised first chapter, other revisions | ||
Revision 1.10 | 20 Aug 2008 | jg |
First major revision | ||
Revision 1.00 | 11 Aug 2008 | jg |
First official release (w00t) | ||
Revision 0.95 | 06 Aug 2008 | jg |
Second beta release | ||
Revision 0.90 | 01 Aug 2008 | jg |
First beta release |
Table of Contents
List of Tables
List of Examples
Table of Contents
This guide is intended to introduce computer users, particularly students, to the command line interface (CLI) and the command line programming tools of Linux-based operating systems.
You may find this guide to be helpful if you are:
Taking a Linux-based course (particularly a programming course, especially one that uses C or C++)
Working on a project that requires the use of the Linux command line interface
Accessing a Linux server via secure shell (SSH)[2]
You probably would not find this guide to be helpful if you are looking for information on:
Computer science or programming (such as how to program in language X)
Linux desktop environments
Linux installation or system administration
The reader is assumed to be proficient in general computer use with an operating system whose primary means of interaction with the user is through a graphical user interface (GUI), such as Microsoft® Windows® or Mac OS® X. No previous experience with UNIX® or Linux is assumed.[1]
For the discussion in Chapter 6, Programming tools, the reader is assumed to be familiar with programming and with standard programming tools, such as compilers and debuggers.
[1] A historical note: as described in Randal Bryant and David O'Hallaron's Computer Systems: A Programmer's Perspective (Upper Saddle River: Prentice Hall, 2003), the Unix operating system was first developed at Bell Laboratories from 1969 through 1974 (14). The GNU Project[18] (GNU is short for GNU's Not Unix) was established in 1984 with the “goal of developing a complete Unix-like system whose source code is unencumbered by restrictions on how it can be modified or distributed” (5) - that is, made of free software. Linux was created from the combination of GNU software with the Linux kernel, a “Unix-like operating system kernel” first developed in the early 1990s under the direction of Linus Torvalds, who at the time was a graduate student at the University of Helsinki (18).
To get any real benefit from the guide, the reader must have access (whether by a machine in front of them or via an SSH connection)[2] to a computer running Linux. Similarly, for the reader to benefit from sections in the guide that cover specific programs (such as Section 5.2, “GNU Emacs”), the software under discussion must be installed.
[2] Wikipedia's article on SSH (also called Secure Shell) describes it as “a network protocol that allows data to be exchanged using a secure channel between two networked devices.” Secure connections made by SSH form the basis for the secure file transfer protocols SFTP and SCP, both of which are are discussed in Chapter 4, Secure file transfer.
Topics covered in this guide include:
The man and info pages
The shell
Secure file transfer
Text editors
Programming and debugging tools
References to further reading on many of the above topics
For a more detailed description, see Section 10, “Structure of this guide”.
This guide does not cover the following topics:
Any material related to computer science or programming
Linux installation or system administration
Obtaining or using a Secure Shell (SSH) client[2]
Using applications that are only available through a Linux desktop environment, with the exception of Appendix B, Graphical alternatives
Note | |
---|---|
Feedback and errata reports are greatly appreciated and can be sent by e-mail to me (the author). Thank you for your help. |
The most recent version of this guide is available at http://students.cec.wustl.edu/~jg18/guide/.
This guide was prepared as DocBook V4.5 XML source using Aquamacs Emacs's nXML Mode.
The XSLT and FO processing was done with xsltproc and Apache FOP, respectively.
I found the book Version Control with Subversion to be helpful with managing the guide as a Subversion repository.
I found DocBook: The Definitive Guide, the DocBook Wiki, DocBook XSL: The Definitive Guide, and DocBook XSL Stylesheets: Reference Documentation to be extremely helpful with preparing and publishing the DocBook source.
This guide was adapted from “A Guide to the Linux Command Line Interface for Computer Science Students at Washington University,” my final paper for EP 310 (Technical Writing) from Summer 2008 at Washington University.
The layout of this guide was inspired in part by that of the guides at The Linux Documentation Project (TLDP).
Note | |
---|---|
The text in the HTML and PDF versions of the guide is not formatted in the same way (for example, text that appears in Although I will work on fixing these discrepancies at some point, I've decided that adding and improving content should be given priority over fine-tuning the presentation, and adjusting the HTML format should be given priority over adjusting the PDF format, particularly given the inherent limitations of the PDF publishing system. |
Commands and other user input to be typed at the prompt appear in bold, as in ls (for a simple command) and ./lab0
(for more complicated input).
Text that would appear on the user's screen looks like this:
foo: Command not found.
Also see Example 1.1, “How to read a table entry”.
This guide is organized as a collection of short introductions to selected topics related to the use of the Linux CLI and programming tools:
Linux and its user interfaces, discussed in Chapter 1, Linux and its user interfaces, with references for further reading in Section A.2, “More on Linux”.
The built-in help system, discussed in Chapter 2, Built-in help system.
The shell, discussed in Chapter 3, Basic shell commands and related utilities, with references for further reading in Section A.3, “More on the shell and related utilities”.
Secure file transfer, discussed in Chapter 4, Secure file transfer.
Text editors, discussed in Chapter 5, Text editors, with references for further reading in Section A.4, “More on text editors”.
Programming tools, discussed in Chapter 6, Programming tools, with references for further reading in Section A.5, “More on programming on Linux machines”.
There are also general suggestions for finding information in Section A.1, “Suggested resources for finding information”.
Finally, for your convenience, I have included references to GUI-based versions of these tools in Appendix B, Graphical alternatives.
Tip | |
---|---|
The information in Section 1.5, “Key differences between Windows/Mac OS X and Linux” and Section 3.1, “Features of the shell” can save you a lot of time and frustration when you are working with the CLI. The resources mentioned in Section A.1, “Suggested resources for finding information” can help you find specific information quickly. |
Although you could read this guide from start to finish, you may find it useful to read whichever sections interest you, or you might want to try a strategy such as one of the following:
If you just want to get started immediately and would rather learn the details later:
Open a terminal (check Section 1.3, “Accessing the Linux CLI through a GUI's terminal window” if necessary) if you don't have one open already.
Start with Section 3.2, “Navigating the file system”.
Read any other sections that interest you.
Then consider trying the suggestions listed below.
If you're taking a course or doing work that involves programming:
Start with Section 1.3, “Accessing the Linux CLI through a GUI's terminal window” if you don't know how to open a terminal.
Then look at Section 1.4, “Shells, the shell prompt, and your home directory” if you've never worked with a shell before.
Look over Section 9, “Conventions used in this guide” and Section 1.6, “Understanding the tables of commands and options” if you haven't done so already.
Then take a quick look at Chapter 2, Built-in help system so that you at least know about the man and info pages.
Read Section 3.2, “Navigating the file system” and the sections following it to learn how to work with the shell, using the file managers discussed in Section B.1, “GUI-based file managers” if you get stuck.
Choose one of the editors discussed in Chapter 5, Text editors and learn how to use it, resorting to one of the graphical text editors discussed in Section B.3, “GUI-based text editors” if you get stuck.
Then read Section 6.1, “Compiling programs” and the two sections that follow it to learn how to compile, run, and debug your programs using Linux. I suggest that you wait to try the graphical programming tools mentioned in Section B.4, “GUI-based programming tools” until after you've had some experience with the command line tools.
If you need to transfer files over a network, read Chapter 4, Secure file transfer (or Section B.2, “GUI-based file transfer programs”).
After you have the basics down, check Appendix A, Further reading to learn more about Linux.
If you're taking a course or doing work that doesn't involve programming:
First complete the steps above for programming-related courses/work, skipping the step on programming tools.
Then take a look at Appendix A, Further reading and Appendix B, Graphical alternatives for information related to your course or work.
If you're connecting to a Linux machine via SSH:[2]
Clearly, the information in Appendix B, Graphical alternatives is irrelevant, unless you want to try X tunneling.[3]
So take a look at the steps above for programming-related courses/work and complete them as is relevant to your objectives. Skip the step about accessing the Linux CLI (that is, opening a terminal): it's irrelevant, since SSH already provides you with a terminal.
Since job control is an important part of using SSH effectively, be sure to read Section 3.7, “Using job control”.
If you need to transfer files between your machine and the server, you'll need a separate file transfer program, which might be SCP or SFTP (either the command line versions discussed in Chapter 4, Secure file transfer or graphical versions such as those mentioned in Section B.2, “GUI-based file transfer programs”) or a different program altogether.
If you're absolutely stuck and you're working with a Linux machine in front of you (that is, you're not connecting via SSH), start with the Linux GUI applications discussed in Appendix B, Graphical alternatives and then start again at the top of this section when you're ready to give the CLI another try.
Table of Contents
This chapter provides some background information on user interfaces in Linux.
The most important information is the orientation to the command line in Section 1.3, “Accessing the Linux CLI through a GUI's terminal window” and the sections that follow.
References to further reading on Linux can be found in Section A.2, “More on Linux”.
Tip | |
---|---|
If you're using SSH,[2] you can skip to Section 1.4, “Shells, the shell prompt, and your home directory”. Recall that graphical applications are unavailable while you're using SSH, unless you're using X tunneling.[3] |
Fundamentally, there are two different ways to work with Linux:
As suggested by the title, this guide focuses on the use of the CLI.
Discussion of GUIs is restricted to Section 1.2, “Graphical user interfaces (GUIs) for Linux”, Section 1.3, “Accessing the Linux CLI through a GUI's terminal window”, and Appendix B, Graphical alternatives.
If your primary use of Linux is not via an SSH connection,[2] your first experience with Linux will probably be with a graphical user interface called a desktop environment,[7] which is often used in place of the lower-level command line-based approach.
There are two particularly popular desktop environments for Linux:[8]
As explained in the next section, even if you're using a GUI, you can still access the CLI.
[6] All of these GUIs use the X Window System (also called X) as their underlying windowing system. Wikipedia has an article that surveys the window managers that are compatible with X, as well as an article about X itself.
[7] Instead of a desktop environment, the GUI in use could be a window manager, such as Fluxbox or Enlightenment. A discussion of window managers is beyond the scope of this guide, although Wikipedia has an article about them. Note that one component of a desktop environment is a window manager, as Wikipedia's article on desktop environments explains.
When you're using a GUI (in our case, a desktop environment), you can access the CLI through a terminal window, or terminal.
The procedure for bringing up a terminal varies depending on which desktop environment you are using.[9]
If you're using GNOME, select the Applications menu from the top of the screen, then the Accessories submenu, then the Terminal application.
You can also press Alt-F2 and type gnome-terminal
at the prompt.
If you're using KDE, select the K menu from the lower-left corner of the screen, then the System submenu, then the Konsole application.
You can also press Alt-F2 and type konsole
at the prompt.
If you're using a desktop environment other than GNOME or KDE, you'll need to consult the documentation for that environment.
[9] Desktop environments, including GNOME and KDE, are discussed in Section 1.2, “Graphical user interfaces (GUIs) for Linux”.
Once you open a terminal (or connect to a server via secure shell [SSH]),[2] you type in commands to interact with a program known as a shell.[10] Although GNU Bash (the Bourne Again Shell) is the most popular shell for Linux, some Linux servers and the computers in some Linux computer labs use tcsh as the default shell.[11] To learn more about which shell you're using and about how to switch to Bash if it isn't your default shell, see Section A.3.2, “Changing your shell temporarily to Bash”.
Once the shell starts up, you’ll see the shell prompt, which indicates that the shell is ready for you to type a command.
Here is the prompt that I see after I log in to Washington University Engineering's grid
server: [jg18@grid ~]$
Thus, the format being used is: [username@host_machine current_directory]$
In addition, your current directory will initially be your home directory,[12] as is indicated by the tilde (~
) in the prompt above.[13]
[10] The shell is covered in depth in Chapter 3, Basic shell commands and related utilities.
[11] For learning more about tcsh, Wikipedia's article might be informative. You can also learn more by typing man tcsh
, using the manual (man) pages that you'll learn about in Section 2.1, “Manual (man) pages”.
[12] Like the C:\Documents and Settings\Your Username
directory in Windows or the /Users/Your_Username
directory in Mac OS X, your home directory in Linux stores your personal files and settings. If you're a student, your home directory is probably the top-level directory for the server disk space that your school allocates for your use.
[13] The symbol ~
is listed in Table 3.2, “Common symbols from the file system”.
Before you start working with the Linux CLI, you should be aware of some important differences between Linux and operating systems with which you're more familiar.
Unlike Windows or Mac OS X, Linux has a case-sensitive file system. This means that home
, Home
, and HOME
would all be names for different directories. Similarly, as you'll learn in Section 3.2, “Navigating the file system”, you type cd to change directories: typing CD
or Cd
instead will not work.
When you're working with the CLI, you’ll soon discover what Eric Raymond calls the “Rule of Silence: When a program has nothing surprising to say, it should say nothing.”[14]
When a command finishes running (say, if you create a directory foo
using mkdir), there will be no confirmation message of something like
directory ‘foo’ created
Rather, there will only be a message if there’s a problem, as in
mkdir: cannot create directory `foo': File exists
Although you might use spaces in file and directory names in Windows or Mac OS X (such as Paper due tomorrow.doc
), don't use spaces in file or directory names in Linux. Use underscores (the _
character, usually on the same key as -
) in a file or directory name (such as Paper_due_tomorrow.doc
) instead of spaces. In fact, it's best if the only characters that you use in file and directory names are letters, numbers, hyphens (-
), underscores (_
), and periods (.
), although you shouldn't start a file or directory name with a period, since, as noted in Section 3.2, “Navigating the file system”, doing so makes the file or directory hidden.
When you use a command that would make some permanent change in the file system (such as deleting files or renaming a file such that it would replace a pre-existing file), the system will simply execute your command without asking you for confirmation. You will only be prompted for confirmation if you explicitly ask for it, such as by using the -i
command option that is mentioned in Table 3.3, “Commands for manipulating the file system”.
Similarly, the Linux CLI has no Trash or Recycle Bin for files that you decide to delete but might want to restore later. Deleted files are simply deleted and can't be recovered.
[14] From Eric Raymond's The Art of Unix Programming (Boston: Addison-Wesley, 2003). Found online at http://www.catb.org/~esr/writings/taoup/html/ch01s06.html#id2878450.
Throughout this guide are tables summarizing many of the most commonly used Linux commands and command options. Most table entries, like the one in the example below, follow a format similar to that used by Linux's built-in manual pages.[15] Some table entries, such as those found in Section 5.2, “GNU Emacs”, use a different format, although the differences are explained where relevant.
Since most commands have many options available, only a few options will be listed. To view all options for a given command, check the command's manual page.[15] Square brackets (that is, []
) around an item indicate that it's an optional argument for the command.
Example 1.1. How to read a table entry
Given the command synopsis
ls
[-a
] [-l
] [path]
ls is the command, -a
and -l
are some of the options available for ls, and path
is an optional argument for ls.
Table of Contents
Before you try searching the Web for information on a command, consider checking Linux's extensive built-in help system first. In addition to the information in Section 2.1, “Manual (man) pages” and Section 2.2, “GNU info pages”, many (but not all) commands will provide basic usage information if they are invoked with the --help
option. As an example, try typing man --help
at the prompt.
Table 2.1. Commands for the built-in help system
Command | Action |
---|---|
| Manual pages |
| GNU info pages |
--help option (on many commands) | Provides usage (how-to-use) information |
You can start learning about the man pages by typing man intro
or man man
at the shell prompt.[17] The man pages’ help (which you can find by typing h while reading a man page) will give you the full list of navigation commands, but to start, you can scroll along a man page using b for back, f for forward, < to jump to the start of the man page, and > to jump to the end, typing q to quit. It is always worth checking the man pages when you are faced with an unfamiliar command or when you want to learn more about a command than you already know.
As mentioned in Section A.2.1, “General Linux resources”, a list of Web-based repositories of Linux manual pages can be found in the Wikipedia article on Unix manual pages.
Info pages, which are another component of the built-in help system, are available for many software products from the GNU Project.[18] To get started, try typing info
or info info
or info some_program
(such as info gcc
) at the prompt. The navigation commands are different from those for the man pages: ? provides a list of commands and h starts a tutorial (but q still quits). It is worth checking the info pages for information on specific GNU programs.[19] You can also find the built-in documentation for the GNU C library (or glibc
) by typing info libc
at the prompt.[20]
[16] A historical note: as explained in an article from Wikipedia, the idea of built-in manual pages dates back to the original Unix operating system from Bell Labs.
[17] The shell prompt is discussed in Section 1.4, “Shells, the shell prompt, and your home directory”.
Table of Contents
Described by Joe Barr as a “web browser to the kernel,”[21] the shell is always running in a terminal, no matter what program may be in the terminal's foreground at any given time. As mentioned in Section 1.4, “Shells, the shell prompt, and your home directory”, the shell in use is typically either bash or tcsh. You can determine the default shell for your system by typing echo $SHELL
at the prompt.
This chapter covers many of the critical commands for working with Linux, including those for using the file system and handling compressed files. The last two sections, Section 3.5, “Working with text streams” and Section 3.7, “Using job control”, are less crucial than the others, but they may prove useful as you become more experienced with using the shell. For SSH users, however, since job control is an important part of making effective use of SSH, they would most likely benefit from reading that section.
Many of the commands discussed in this chapter belong to the GNU Core Utilities (or coreutils
), a collection of basic utilities from the GNU Project.[18] You can learn more about coreutils
by typing info coreutils
or from the resources listed in Section A.3.5, “The GNU Core Utilities (coreutils)”.
References to further reading about the shell can be found in Section A.3, “More on the shell and related utilities”.
There are several useful features of the shell:
You can access your command history (previously typed commands) using the up and down arrows.
By pressing the Tab key, you can make the shell auto-complete command names and file/directory names until it encounters ambiguity.
For example, assuming that you have two text files, foobar
and foobaz
, in some directory with no other files in it, if you type emacs f
at the prompt and then press Tab without pressing Enter, you will see the name partially completed to
emacs fooba
on the screen. The shell can’t complete any further, however, since the last character could be r
or z
.
If you type !some_string
at the prompt and press Enter, the shell will check your command history for the most recently typed command beginning with some_string
and then automatically run that command. This can be useful if you typed a long command a while back and would rather not retype it or look through the command history to find it.[22] If you'd like to just see the most recently typed command beginning with some_string
without executing it, type echo !some_string
instead.
The history command will allow you to see up to the last few hundred commands you've typed. Since this command can print hundreds of lines of text to the screen, it's best to either filter the history by searching with grep, as in history | grep ssh
. You can also view the whole list with the less reader by typing history | less
. Both grep and less are discusssed in Section 3.5, “Working with text streams”.
If you mistype or omit something as you type a command, you can edit what you've typed (assuming that you haven't pressed Enter yet) without having to retype everything.
The commands for command line editing are similar to those listed in Table 5.12, “Moving the cursor in Emacs” (with the exceptions that M-<, M->, M-v, and C-v are unavailable) and Table 5.13, “Manipulating text in Emacs” (but only the commands under “Deleting Text” are available), which are for the text editor GNU Emacs.[23] You can also edit what you've typed with the left and right arrow keys and the Backspace and Delete keys.
To type a command like C-a, hold the Ctrl/Control key while typing a
.[24]
To type a command like M-f, hold the Alt (or the Esc) key while typing f
.[24]
In addition to the features listed above, GNU Bash (the Bourne-Again Shell) has other features[25] that you can learn more about from the resources listed in Section A.3.1, “General information on the Bash shell” and the sections that follow it.
You can find your way around the Linux file system using the commands listed in Table 3.1, “Commands for navigating the file system” and the symbols listed in Table 3.2, “Common symbols from the file system”. Note that ls will not display hidden files or directories (whose names start with a period, such as .history
) unless you include the -a
option by typing ls -a
at the prompt.
The command find is particularly powerful, although I think that the example below illustrates the most common use you'll have for it. To learn about all that find can do, consult man find
.
Example 3.1. Using cd
cd
(which is equivalent to cd ~
) will take you to your home directory.[12]
cd foo
will change your current directory to the subdirectory foo
, displaying an error if foo
doesn't exist.
Example 3.2. Using du
The command du -ch ~
will tell you how much disk space each directory in your home directory[12] takes up. Since this command can produce a lot of output to the screen, try viewing the result with less[26] by typing du -ch ~ | less
(the |
, or “pipe,” is typically found on the same key used for the backslash (\
)).
If you just want to see the total disk space usage only, type du -ch ~ | tac | sed 1q
at the prompt (the next-to-last character is the number 1
, not the letter l
).[27] Alternatively, you can type du -ch ~ | tail -n 1
at the prompt. Both commands will print only the last line of the output from du.
Example 3.3. Using find
find . -name "*.java"
will print the locations of all files whose names end with .java
in the current directory and in all of its subdirectories.
Example 3.4. Using ls
ls
(which is equivalent to ls .
) will list files and directories in the current directory.
ls -al ..
will list all files and directories (including hidden ones) in the directory that is one directory above .
(the current directory) using a long-listing format.
Table 3.1. Commands for navigating the file system
Command | Action |
---|---|
| Change to home directory [to specified directory] |
| Estimate file space usage, human-readable format with a grand total[a] |
| Determine file type[b] |
| Search for files in a directory hierarchy[c] |
| List contents of current directory
[of the provided path instead]
[include hidden files] [long listing format][d] |
| Print name of current/working directory[e] |
| Show the full path of a (shell) command[f] |
[a] Linux manual page for du. [b] Linux manual page for file. [c] Linux manual page for find. [d] Linux manual page for ls. [e] Linux manual page for pwd. [f] Linux manual page for which. |
Table 3.2. Common symbols from the file system
Symbol | Meaning |
---|---|
~ (tilde) | User's home directory[12] |
. (period) | Current (working) directory |
.. (double period) | One directory up from current directory |
/ (front slash) | Root (top-level) directory |
* (asterisk) | Wildcard (can be used in many commands) |
[26] The command less is discussed in Section 3.5, “Working with text streams”.[a] You can always check man less
as well.
[27] For your enlightenment, tac is from the GNU Core Utilities (or coreutils
; see Section A.3.5, “The GNU Core Utilities (coreutils)”), and sed (short for stream editor) filters text, as explained on GNU's page for its version of sed. In the example above, sed prints only the first line of the output. You can learn more about tac and sed by reading their man or info pages (such as man tac
or info sed
).
While being able to examine the file system is important, you'll also need to be able to make changes to it, by creating, copying, moving, and removing files and directories, as well as making symbolic links. Relevant commands can be found in Table 3.3, “Commands for manipulating the file system”. Note that mv can be used either to rename a file/directory or to move it, depending on the arguments passed to mv.
Caution | |
---|---|
If mv is used to move a file to a place where a file with the name already exists or to rename a file such that a file with the new name already exists in that directory, the old file will be silently replaced, unless you specify the Similarly, cp can silently replace files in the copying process. As with mv, you can specify the |
Caution | |
---|---|
Although you can use rm with the wildcard ( |
Example 3.5. Using ln
ln -s ~/classes/cse332/lab5 332lab5
will create a symbolic link (that is, an alias or shortcut) to ~/classes/cse332/lab5
called 332lab5
, placing the symbolic link in the current directory.
Example 3.6. Using mv
Assuming that foo
is a directory and that bar
is a file in foo
:
mv bar baz
will rename bar
to baz
, silently replacing any pre-existing file in foo
with the name baz
.
mv bar ..
will move bar
to one directory above foo
, silently replacing any pre-existing file in that directory with the name bar
.
mv bar ../baz
will move bar
to one directory above foo
and rename bar
to baz
, silently replacing any pre-existing file in that directory with the name baz
.
Example 3.7. Using rm
rm *.txt
will delete all files whose names end in .txt
from the current directory only.
rm * .txt
, however, will delete ALL files (but not directories) in the current directory and will then try to delete a file named .txt
.
Therefore, when using rm, double-check your typing before you press Enter!
rm -rf foo
will delete foo
regardless of whether it is a file or directory; if foo
is a directory, then all of foo
's contents (including subdirectories) will also be deleted.
Table 3.3. Commands for manipulating the file system
Command | Action |
---|---|
| Copy files and directories[a] [prompt before overwrite] [include all subdirectories and their contents] |
| Make symbolic link to target |
| Create directory |
| Rename file or directory [prompt before overwrite][b] |
| Move file or directory [prompt before overwrite][b] |
| Remove files or directories[c] [include all subdirectories and their contents, with no prompt for confirmation] [prompt before any removal] |
| Remove empty directory |
[a] Linux manual page for cp. [b] Linux manual page for mv. [c] Linux manual page for rm. |
The tar utility (tar
is short for tape archive) allows you to compress files and directories into a single file called a tarball. Note that compressing, listing, and extracting all use the same command (tar), but the options used differ depending on which action you wish to perform.
Tarballs are typically compressed using GNU zip, or gzip, resulting in their file extension of .tar.gz
or .tgz
. However, some tarballs are created with the newer bzip2 compression, which results in a smaller file size. Their file extension then becomes .tar.bz2
or .tbz
. The commands in the table below use gzip. If you'd prefer to use bzip2, you can use the commands below, replacing z
with j
among the command options (so that, for example, -czf
becomes -cjf
) and replacing .tar.gz
with .tar.bz2
.
Like with all commands, you can find more information by checking man tar
, but since the version of tar used on Linux is a GNU product, you can also find information by typing info tar
.
Table 3.4. Commands for handling compressed files (tarballs)[28]
Command | Action |
---|---|
| Creates a tarball called foo.tar.gz from the list of files and directories (separated by spaces) in file_list |
| Lists the files and directories in foo.tar.gz (does not extract them) |
| Extracts the contents of foo.tar.gz and places them in the current directory |
[28] Although, when used with the options above, tar will be silent (that is, it won't provide any message to the user unless there's a problem, as mentioned in Section 1.5, “Key differences between Windows/Mac OS X and Linux”), you can use the |
Caution | |
---|---|
As with the commands mv and cp from Section 3.3, “Manipulating the file system”, if you direct the I/O redirection operator |
The Linux CLI depends on programs communicating with each other (and with the user) through text streams,[29] including through the commands that you type at the keyboard (called the standard input stream, or stdin
) and the results or errors displayed on the terminal screen (called the standard output and standard error streams, or stdout
and stderr
). As such, you may benefit from having some ability to work with text streams.
The command grep and its variants (such as egrep) are particularly powerful and complex commands, since they're designed to search for textual patterns called regular expressions.[30] Thus, consulting man grep
and info grep
, possibly in addition to checking sources such as those mentioned in Section A.1, “Suggested resources for finding information” for information on grep, is highly recommended.
More information on text processing with the shell can be found in Section A.3.3, “Text processing with the shell”.
More information on I/O redirection can be found in Barr, CLI for Noobies, Chapter 5 (“Everything's a File”) and Garrels, Introduction to Linux, Chapter 5 (“I/O redirection”).
Example 3.9. Using /dev/null
./some_verbose_program > /dev/null
will run some_verbose_program but discard its output by redirecting it to /dev/null
(the “bit bucket”) instead of stdout
(the terminal screen). If some_verbose_program has output that is directed to stderr
, that output will still be displayed on the terminal screen.
Example 3.10. Using diff
diff file1 file2 > differences.txt
will find all textual differences between file1
and file2
and store the results in a file (that will be created if it does not exist; if it does exist, it will be silently replaced) titled differences.txt
without sending any of the results to stdout
(the screen).
Example 3.11. Using echo
echo $SHELL
will display the value of the SHELL
environment variable (that is, the full path of the default shell) on stdout
(the terminal screen).
Example 3.12. Using xargs
find dir -name "*.cpp" | xargs grep boost
will search all files ending in .cpp
in the directory dir
and all of its subdirectories recursively, listing every line where the string boost
is found in those files.
By contrast, find dir -name "*.cpp" | grep boost
will search the full filenames (absolute path from /
) of the files ending in .cpp
in the directory dir
and all of its subdirectories for the string boost
, listing every matching line.[32] It will not search the contents of the files, just their names (full paths).
Table 3.5. Standard streams
Name | Description | Default Location |
---|---|---|
stdin | Standard input | Keyboard |
stdout | Standard output | Terminal screen |
stderr | Standard error | Terminal screen |
Table 3.6. I/O redirection operators
Operator in Context | Action |
---|---|
command1 | command2 | Direct the output from command1 to the input of command2 |
command1 < file1 | Direct the contents of file1 to the input of command1 |
command2 > file2 | Direct the output from command2 to be stored as file2 |
command3 >> file3 | Direct the output from command3 to be appended to the contents of file3 |
Table 3.7. Commands for working with text streams
Command | Action |
---|---|
| Concatenates the contents of the given list of files and sends the results to stdout |
| Performs line-by-line comparison of file1 and file2 , reporting any differences |
| Takes some_text from stdin and displays it on stdout |
| Searches stdin by default
[or the files in file_list instead]
for lines matching pattern ,
then prints the results to stdout |
| Allows read-only viewing of file [a] |
| Passes stdin as arguments to some command cmd , which is itself an argument to xargs |
[a] less has a diverse set of keyboard shortcuts available that seems to be designed to cover a wide range of common conventions for keyboard shortcuts, including those for the man pages and the vi and Emacs text editors. The commands mentioned in Section 2.1, “Manual (man) pages” ought to be enough to get by, but you can get the full listing of commands by typing h while you are using less (and typing q still quits). |
[29] Eric Raymond calls this the “Rule of Composition: Design programs to be connected with other programs” in The Art of Unix Programming (Boston: Addison-Wesley, 2003). Found online at http://www.catb.org/~esr/writings/taoup/html/ch01s06.html#id2877684.
[30] Wikipedia has an article on regular expressions; the references in Section A.3.3, “Text processing with the shell” may also be helpful.
[31] This type of search will not indicate which files the lines containing printf
came from, though. One way to obtain that information is to use find (discussed in Section 3.2, “Navigating the file system”), by typing find . -name "*.c" | xargs grep printf
at the prompt. Be aware that while cat searches for files in the current directory only, find searches for files in the current directory and all of its subdirectories (and their subdirectories and so on).
Note | |
---|---|
This is a new section and is still under development. |
TODO groups, chmod, chgrp, chown, rwx, etc. Examples!
Table 3.8. Commands for adjusting file permissions
Command | Action |
---|---|
| Changes the group ownership of path to grp [if path is a directory, include all subdirectories] |
| Changes the permissions of path to perms [if path is a directory, include all subdirectories] TODO: Explain how to change permissions! |
| Changes the ownership of path to user [if path is a directory, include all subdirectories] |
| Lists the groups of which the current user is a member [for the given user instead] |
Note | |
---|---|
Since job control is an important component of making effective use of the Linux CLI over SSH, this section is undergoing revisions to better accommodate the needs of SSH users. If you absolutely require authoritative information on job control at this time, or if you need to know more than just how to kill a process, I suggest that you try Machtelt Garrels's Introduction to Linux, specifically Section 1 (“Processes Inside Out”). |
[will need rewrites!]For users who are using a computer, since it's easy to open multiple terminals, all the job control skills you really need are to terminate a process. For those using SSH, however, TODO
More information on processes and job control can be found in Barr, CLI for Noobies, Chapter 10 (“background, foreground, suspend”), and Garrels, Introduction to Linux, Chapter 4 (“Processes”), particularly Section 1 (“Processes Inside Out”).
Example 3.13. Using ps and kill[33]
[jg18@grid ~]$
ps -u jg18
PID TTY TIME CMD 21947 ? 00:00:00 sshd 21948 pts/6 00:00:00 tcsh 23482 pts/6 00:00:00 vim 23484 pts/6 00:00:00 ps
[jg18@grid ~]$
kill -9 23482
[1] Killed vim
Table 3.9. Commands for terminating a process
Command | Action |
---|---|
| List running processes with their process IDs [only those from the specified user ] |
| Kill (end) the specified process with id of process_ID |
[33] I could also use grep (discussed in Section 3.5, “Working with text streams”) here: If I know the name (or part of the name) of the process that I want to terminate (in this case, vim
), I could find its PID quickly by typing ps -u jg18 | grep vim
.
[21] Barr, CLI for Noobies, 12.
If you aren't familiar with the concept of an operating system kernel, try the Wikipedia article.
[22] It's possible, though, that you typed the command so many commands ago that it's not in your command history, in which case the shell would respond with
some_string: Event not found.
[23] Emacs is discussed in Section 5.2, “GNU Emacs”.
[24] From the Emacs built-in tutorial.
[25] For example, if you're using Bash, try typing a single letter or a short string of letters (such as fi
) and then pressing Tab twice.
Table of Contents
Secure file transfer allows you to transfer files over a secure connection, that is, one that is encrypted and in which the identity of the remote machine (the computer you're connecting to) has been validated. One common use for secure file transfer is for backing up your work on your computer to the server for safekeeping. Another use might be for retrieving files from the server while you're working from your computer at home.
The Linux command line interface offers two methods of secure file transfer: secure copy (SCP) and SSH file transfer (SFTP). Both technologies are based on secure connections provided by SSH.
Caution | |
---|---|
SCP is a powerful command: it allows you to easily and quickly send a file, directory, or set of files, but it doesn't prevent you from making such mistakes as sending files to (or receiving files from) the wrong location on the remote machine. It also does nothing to stop you from overwriting files (without notification that you're doing so, of course) if you specify the wrong path on the destination machine - the author managed to do this with his home page! If you use SCP with the |
If you want to transfer a single file (such as a tarball),[36] a directory in its entirety, or all of the files (but not subdirectories) in a given directory, and if you know the exact path of the source and destination, you can use SCP (a secure version of cp[34]) to transfer that file/directory or those files.
To upload a file from the current directory on your machine (say, a file called my_file
) to your home directory[12] on the server, type:
scp ./my_file your_username@server_name:
To download a file from the server (say, a file called my_file
from the path ~/classes/cse332
[35]) to the current directory on your machine, type:
scp your_username@server_name:classes/cse332/my_file .
To upload a directory (including all of its subdirectories), type:
scp -r /path_to_source_directory/ your_username@server_name:path_to_destination
To download a directory (including all of its subdirectories), type:
scp -r your_username@server_name:path_to_source_directory /path_to_destination/
To upload a single directory's files (but not its subdirectories), type:
scp /path_to_source_directory/* your_username@server_name:path_to_destination
To download a single directory's files (but not its subdirectories), type:
scp your_username@server_name:path_to_source_directory/* /path_to_destination/
As opposed to SCP, which only allows for single-command transfers, SFTP provides the user with a simple interactive shell for transferring files between your machine and another computer (most likely a server). In addition to providing support for uploading and downloading files, the SFTP shell allows for limited navigation through and manipulation of the local and remote file systems.
Start SFTP by typing sftp username@server_name
at the prompt, filling in the appropriate values for your username and the server's name. The commands that you'll most likely need are listed in Table 4.1, “SSH file transfer (SFTP) commands”.
Note | |
---|---|
SFTP commands look a lot like those discussed in Section 3.2, “Navigating the file system” and Section 3.3, “Manipulating the file system”, but commands to be run on the local machine (the one in front of you) have an |
As far as I can tell, command history and command completion (discussed in Section 3.1, “Features of the shell”) are not available in SFTP. In addition, you cannot transfer entire directories through SFTP; you must compress them first into a tarball[36] and then transfer the tarball.
Example 4.1. Using SSH file transfer (SFTP)
sftp jg18@grid.cec.wustl.edu
[Note that not even asterisks will be displayed on the screen as you type your password.]
You'll then have a prompt like sftp>
at which you can type commands.
Table 4.1. SSH file transfer (SFTP) commands
Category | Command | Action |
---|---|---|
Basic SFTP Commands | --- | |
~ | help or ? | View list of SFTP commands |
~ | exit or quit | Exit SFTP |
Working with File Systems | --- | |
~ |
| Change directory on remote (local) machine |
~ |
| List remote (local) directory contents |
~ |
| Create remote (local) directory |
~ |
| Print name of working remote (local) directory |
~ |
| Rename remote file or directory |
~ |
lrm ) | Remove remote file |
~ |
lrmdir ) | Remove remote empty directory |
Downloading and Uploading | --- | |
~ |
| Download from remote_path to working local directory [to local_path instead] |
~ |
| Upload from local_path to working remote directory [to remote_path instead] |
Other Commands | --- | |
~ |
| Execute some_command on local machine |
[34] The command cp (copy) is discussed in Section 3.3, “Manipulating the file system”.
[35] The symbol ~
indicates your home directory, as noted in Table 3.2, “Common symbols from the file system”.
Table of Contents
To work on programming projects, you will need to use a text editor. There are two editors available through the Linux CLI that are suitable for programming: vi and GNU Emacs. As discussed in the next section, vi typically exists on Linux machines in the form of Vim, a modern vi clone.
Further reading on text editors can be found in Section A.4, “More on text editors”.
Note | |
---|---|
This section is nearly finished but is still under development. |
Tip | |
---|---|
Only the commands that start with a colon ( In addition, all vi and Vim commands are case-sensitive. |
Vi (pronounced VEE-EYE)[37] “was the first real screen-based editor for Unix systems.”[38] It is present on all Unix and Unix-like operating systems (including Linux). Another frequently used text editor, GNU Emacs (discussed in Section 5.2, “GNU Emacs”), is usually (but not always) installed as well. You can start vi by typing vi or vi file_to_edit
(such as vi lab0.c
) at the prompt.
Vi is closely connected to the even older line-based text editor ex, whose commands begin with a colon (:
). [reference! Also something (very) short about age/origin of vi.] In fact, you can start either editor from the command line and can switch between the two while using them. As can be seen from the tables below, vi users must know a few essential ex commands. However, a thorough coverage of ex is beyond the scope of this guide.
Since the original vi was a closed-source project,[39] developers created their own clones of vi, adding new features along the way. One of the most popular of these clones is Vim (short for vi improved), created by Bram Moolenaar. Vim seems to be the standard version of vi on Linux machines, although other vi clones, such as elvis and vile, exist. However, the rest of this section will focus exclusively on Vim. Although Vim has much in common with the original vi and with the various vi clones, there are differences among them, including incompatibilities between Vim and vi. This guide will not cover those distinctions; anyone who is interested can check the references listed in the further reading section.
Vim is a modal editor that fundamentally has two modes: a normal (or command) mode for entering commands and an insert mode for entering and editing text. By comparison, modeless editors such as Emacs are always in insert mode, so that the characters you type always appear as text on the screen. As listed in Table 5.10, “Other modes in Vim”, Vim also includes a replace mode, which differs from insert mode in that any characters typed will overwrite existing characters in the buffer, as well as three types of visual modes, which allow for selecting text for cutting or copying.
Tip | |
---|---|
You can return to normal mode at any time by pressing the Escape key. |
[TODO - Importance of normal/command mode, operators/commands and motions/text-objects, delete vs. change vs. yank (and put?), etc.]
When vi was invented, the H, J, K, and L keys doubled as arrow keys, since there was no separate set of arrow keys on the keyboard. Thus vi and its clones still use those keys as arrow keys in the format shown in Table 5.1, “The Arrow Keys in Vim” below. Although the now-standard arrow keys work in Vim as expected, you may wish to try using the HJKL keys in their place, as you may find it faster to use them rather than having to reach to the arrow keys to use them.
To type a command like C-r, press the R key while holding down the Ctrl/Control key.
References to further reading on vi and Vim can be found in Section A.4.1, “More on vi and Vim”.
Table 5.2. Basic Vim commands, Part 1
Category | Command | Action |
---|---|---|
Opening, Saving, and Exiting | --- | |
~ |
| Open (or create) file |
~ |
| Save current file |
~ |
| Save current file as filename (will not overwrite) |
~ |
| Save current file as filename (will overwrite if necessary) |
~ | :wq or ZZ | Save file and quit Vim |
~ | :q! or ZQ | Quit Vim without saving |
Getting Help | --- | |
~ |
| Vim help |
~ | vimtutor from command line or :help tutor while in Vim | Vim tutor |
Keys Found in Other Editors | --- | |
~ |
| Move cursor by one character/line |
~ | Delete (but not Backspace, except in insert mode) | Delete one character at a time |
Table 5.3. Basic Vim commands, Part 2
Category | Command | Action |
---|---|---|
Commands on Commands | --- | |
~ |
| Run <command> (for example, ls) in shell |
~ |
| Enter normal mode (also quits any command being typed) |
Undo and Redo | --- | |
~ |
| Undo previously typed command |
~ |
| Undo changes to most recently edited line |
~ |
| Revert file to last saved version |
~ |
| Redo most recently performed undo |
~ |
| Redo most recently typed non-movement command |
Table 5.4. Moving through the buffer in Vim
Command | Action |
---|---|
| Indicates your location in the file and other information |
gg or 1G | Move to start of buffer |
| Move to end of buffer |
| Move to line <number> |
| Move up by one screen |
| Move down by one screen |
Table 5.5. Moving through screens and lines in Vim
Category | Command | Action |
---|---|---|
Moving Within the Screen | --- | |
~ |
| Move to top of screen |
~ |
| Move to middle of screen |
~ |
| Move to bottom of screen |
Moving Along a Line | --- | |
~ |
| Move to start of line |
~ |
| Move to first non-whitespace character of line |
~ |
| Move to end of line |
Moving Word by Word | --- | |
~ |
| Move to start of (previous) word |
~ |
| Same as b, but ignore punctuation |
~ |
| Move to end of (next) word |
~ |
| Same as e, but ignore punctuation |
~ |
| Move to start of next word |
~ |
| Same as w, but ignore punctuation |
Table 5.7. Special text manipulation commands in Vim
Category | Command | Action |
---|---|---|
On a Single Line | --- | |
~ | dd or D | Delete current line |
~ | cc or C | Change current line |
~ | yy | Yank (copy) current line |
On a Single Character | --- | |
~ |
| Delete current character |
~ |
| Replace current character with <char> |
~ |
| Change case of current character |
~ |
| Find matching parenthesis, bracket, or brace |
Table 5.8. Searching and substituting in Vim
Category | Command | Action |
---|---|---|
Searching Text | --- | |
~ |
| Search forward |
~ |
| Repeat search forwards |
~ |
| Search backward |
~ |
| Repeat search backwards |
Substituting Text | --- | |
~ |
| Substitute new for old for the first occurrence of old on the current line |
~ |
| Substitute new for old throughout the current line |
~ |
| Substitute new for old between lines numbered <line1> and <line2> |
~ |
| Substitute new for old throughout the entire buffer |
~ |
| Substitute new for old throughout the entire buffer, with a prompt for each substitution |
Table 5.9. Ways to enter insert mode in Vim
Command | Method of Entering Insert Mode |
---|---|
i | Insert text just before cursor |
a | Insert text just after cursor |
I | Insert text at start of current line |
A | Append text to end of current line |
O | Open a line just above current line |
o | Open a line just below current line |
Table 5.10. Other modes in Vim
Command | Mode |
---|---|
R | Replace mode |
v | Visual mode |
V | Visual line mode |
C-v | Visual block mode |
Described as “a complete working environment,” GNU Emacs has all of the functionality that you could ever want from a text editor.[40] It is also, in my opinion, considerably easier to use than vi. You may wish to try both vi and Emacs and see which one you prefer. You can start Emacs by typing emacs or emacs file_to_edit
(such as emacs lab0.c
) at the prompt.
Emacs makes frequent use of the Control (Ctrl) and Meta keys, but don’t bother looking for the Meta key on your keyboard: I don’t know of any modern keyboards that have one. Instead, the Escape (Esc) key functions as the Meta key. In most configurations of Emacs, the Alt key also works as a Meta key. To type a command such as C-x C-c
, press Ctrl and X, let go of the Ctrl and X keys, then press Ctrl and C.
Instead of working directly with files, when you use Emacs, you work with temporary buffers that affect the file on disk only when you save the buffer. When you create a file with C-x C-f
, that file isn't saved to disk (that is, actually created) until you make a change in the initially blank buffer and then save it with C-x C-s
.
Any text that you cut, copy, or delete (using the commands in Table 5.13, “Manipulating text in Emacs” below) is moved to the kill ring, where Emacs stores such text until you paste it (or yank it, as it is also called) using C-y
. Do not confuse the kill ring with the clipboard that exists in Windows or in Mac OS X: text in the kill ring can only be used within Emacs. Graphical versions of Emacs, however, provide mechanisms for working with the system clipboard via the pull-down menu.
The C-u
shortcut allows you to repeat a command for a given number of times. For example, to scroll down to the 50th line of a buffer when the cursor is initially at the top of the buffer, type C-u 5 0 C-n
(without pressing Enter).
If you accidentally type the start of some command in Emacs, you can use the C-g
shortcut to discard the partially typed command. C-g
can also be used to interrupt a running Emacs command.[41]
You can use M-x
to type in longer commands. For example, to switch into C++ editing mode, type M-x c++-mode
and then press Enter. As with the shell, however, you can use Tab completion by typing M-x c+
and then pressing the Tab key. You can also use Tab completion with file names when you are trying to open files with C-x C-f
.
References to further reading on Emacs can be found in Section A.4.2, “More on GNU Emacs”.
Table 5.11. Basic Emacs commands
Category | Command | Action |
---|---|---|
Opening, Saving, and Exiting | --- | |
~ |
| Open (or create) file |
~ |
| Save current buffer |
~ |
| Save current buffer as... |
~ |
| Exit Emacs |
Getting Help | --- | |
~ |
| Emacs help |
~ |
| Emacs tutorial |
Keys Found in Other Editors | --- | |
~ |
| Move cursor by one character/line |
~ |
| Delete one character at a time |
Commands on Commands | --- | |
~ |
| Quit command being typed (may need to press repeatedly) |
~ |
| Undo previously typed command |
~ |
| Repeat command to follow (default value is 4) |
Table 5.12. Moving the cursor in Emacs
Category | Command | Action |
---|---|---|
Moving to Start and to End | --- | |
~ |
| Move to start of buffer |
~ |
| Move to end of buffer |
~ |
| Move to start of line |
~ |
| Move to end of line |
Moving Up and Down (Across Lines) | --- | |
~ |
| Move up by one screen |
~ |
| Move down by one screen |
~ |
| Move to previous line |
~ |
| Move to next line |
Moving Forward and Backward (Within a Line) | --- | |
~ |
| Move backward by one word |
~ |
| Move forward by one word |
~ |
| Move backward by one character |
~ |
| Move forward by one character |
Table 5.13. Manipulating text in Emacs
Category | Command | Action |
---|---|---|
Deleting Text | --- | |
~ |
| Delete from cursor to end of line |
~ |
| Delete from cursor to end of current word |
~ |
| Delete character |
Selecting, Cutting, and Pasting | --- | |
~ |
| Select all |
~ |
| Set mark (for text selection) |
~ |
| Cut selected text (within Emacs) |
~ |
| Copy selected text (within Emacs) |
~ |
| Paste text |
Table 5.14. Additional Emacs commands
Category | Command | Action |
---|---|---|
Search and Replace | --- | |
~ |
| Incremental search |
~ |
| Find and replace |
Windows and Buffers | --- | |
~ |
| Split screen in two |
~ |
| Recombine screen into one |
~ |
| Switch to another buffer |
~ |
| Bring up buffer list |
~ |
| Kill (close) buffer |
Just For Fun | --- | |
~ |
| Play Tetris |
~ |
| Emacs psychotherapist |
[37] Arnold Robbins, Elbert Hannah, and Linda Lamb, Learning the vi and Vim Editors, 7th ed (Sebastopol: O'Reilly, 2008), Chapter 1.
[38] Matthias Kalle Dalheimer and Matt Welsh, Running Linux, 5th ed., Section 19.1: “Editing Files Using vi.”
[39] An open-source version of the original vi is now available on SourceForge. Details can be found in Section A.4.1, “More on vi and Vim”.
Table of Contents
Tip | |
---|---|
Although the examples with this chapter are for programs written in C or C++, much of the information provided is still valid for programs written in other languages. If you want to be certain, though, you should check GCC and GDB's documentation, references to which are provided in Section A.5, “More on programming on Linux machines”. |
To compile and debug your programs, you'll need to use the programming tools that come with Linux, which are collectively known as the GNU toolchain. This chapter covers how to use some of the most commonly used components of the GNU toolchain, as well as how to run your compiled program in Linux.
References to further reading on programming in Linux and the GNU toolchain can be found in Section A.5, “More on programming on Linux machines”.
Programmers using Linux compile programs with the GNU Compiler Collection (GCC), a “compiler driver that invokes the language preprocessor, compiler, assembler, and linker, as needed on behalf of the user.”[42] A list of commonly used compiler flags (which are options to adjust the compiler’s behavior) is provided in Table 6.1, “Commonly used GCC flags”. For a listing of all of the available flags, see man gcc
or info gcc
under the section “Invoking GCC.”
To compile C source code or assembly programs with GCC, type: gcc [flags] file_list
.
Note | |
---|---|
When you are compiling C++ source code, type g++ in place of gcc. |
If you don't specify a file name for the resulting executable file using the -o
compiler flag, GCC will use the default name of a.out
.
Although you can use GCC directly to compile your programs, programmers typically use other software, such as GNU Make, for automating the build process. A discussion of Make is beyond the scope of this guide, but references to more information on Make are provided in Section A.5.3, “GNU Make and Makefiles”.
References to further reading on GCC can be found in Section A.5.2, “More on GCC”.
Example 6.1. Using GCC
gcc -Wall -o foo foo.c
will attempt to compile foo.c
to an executable, turning on (nearly) all compiler warnings, producing the executable foo
if it is successful.
Table 6.1. Commonly used GCC flags
Compiler Flag | Action |
---|---|
-c | Compile to object code and then stop |
-g | Include debugging information |
-ggdb | Include extra debugging information for GDB[a] |
-I dir | Check first for header files in directory dir [b] |
-L dir | Check first for libraries in directory dir [c] |
-o name | Set the (file) name of the compiler's output |
-O2 | Use the optimizer at level 2 |
-S | Compile to assembly code and then stop |
-Wall | Turn on all compiler warnings[d] |
[a] GDB is discussed in Section 6.3, “Debugging programs”. [b] (can use this flag repeatedly to specify multiple directories) [c] (can use this flag repeatedly to specify multiple directories) [d] Well, |
If you try to run your compiled program (which we’ll call lab0
) by typing lab0
at the prompt, you’ll receive this message:
lab0: Command not found.
When you type a command at the prompt, the shell checks all directories listed in the PATH
environment variable (which you can view by typing echo $PATH
) to see if it is present there. If the shell can’t find it, it will print the message listed above.
By default, the current directory (.
) is not included in the PATH
, and although you can add it,[43]
it is much easier (and strongly recommended) to simply type ./lab0
at the prompt instead,[44] specifying that the shell should check the current directory for lab0
.
[43] But see “Shell Startup File Elements” under “User Environments” in Brian Ward's How Linux Works (San Francisco: No Starch Press, 2004) for why you shouldn't add .
to the PATH
.
[44] Recall from Table 3.2, “Common symbols from the file system” that .
(a period) stands for the current directory.
Note | |
---|---|
To use the GNU Debugger (GDB) effectively, you need to direct the compiler to include debugging information when it compiles your program. You can do this by using either the |
To debug a program using the GNU Debugger (GDB), type gdb program_to_debug
(such as gdb lab2
) at the prompt. You can then use GDB’s commands, some of which are listed in Table 6.2, “Commonly used GDB commands, Part 1” and Table 6.3, “Commonly used GDB commands, Part 2” below, to debug your program. GDB has many more commands than what is listed here; consult GDB’s built-in help for a complete listing. Also note that many commands have shortened versions: for example, you can type r in place of run. As with the shell (discussed in Section 3.1, “Features of the shell”), command history and command completion are available.
Finally, the command x/ (listing the contents of memory) is a particularly powerful command, but it's a fairly complex one, and describing it in detail is beyond the scope of this guide. Therefore, consulting help x while in GDB is recommended.
References to further reading on GDB can be found in Section A.5.4, “More on GDB”.
Table 6.2. Commonly used GDB commands, Part 1
Category | Command | Action |
---|---|---|
Basic GDB Commands | --- | |
~ |
| GDB's built-in help |
~ |
| Quit GDB |
Controlling Program Flow | --- | |
~ |
| Continue running the program (can use when a breakpoint is reached) |
~ |
| Run the program being debugged [with the supplied command line arguments (args )] |
~ |
| Step through program, over subroutines [repeat for specified number of times] |
~ |
| Step through by one instruction [repeat for specified number of times] |
~ |
| Step through program, into subroutines [repeat for specified number of times] |
~ |
| Step by one instruction exactly [repeat for specified number of times] |
~ |
| Halt a program that's currently running in GDB |
Table 6.3. Commonly used GDB commands, Part 2
Category | Command | Action |
---|---|---|
Breakpoints and Watchpoints | --- | |
~ |
| Set a breakpoint at section |
~ |
| Delete all breakpoints [or just those specified, listed by breakpoint number] |
~ |
| Set a watchpoint for expression |
Displaying Information | --- | |
~ |
| Disassemble function in current frame [the given function instead] |
~ |
| Display the state of the current stack frame |
~ |
| Display the contents of the CPU registers |
~ |
| Show 10 lines of source code centered around the currently executing line [centered around the start of function ] |
~ |
| Display the value of variable [the memory address of variable ] |
~ |
| Display 16 words of data from memory starting at the provided address |
You can inspect and modify object files (including executables) using the GNU Binary Utilities (binutils
), a set of tools that also includes the GNU assembler (as) and linker (ld).
Example 6.2. Using objdump
objdump -d lab0 > lab0_dump.txt
will disassemble the executable file lab0
and send the results to the file lab0_dump.txt
, creating or overwriting the file as necessary.
References to further reading on binutils
can be found in Section A.5.5, “More on the GNU Binary Utilities (binutils)”.
Table 6.4. Selected commands from GNU Binary Utilities (binutils
)
Command | Action |
---|---|
| Display the symbol table of the given object file |
| Disassemble the given object file, sending the results to stdout |
| List all strings found in the given object file |
| Remove part or all of a given object file's symbol table |
[42] Randal Bryant and David O'Hallaron, Computer Systems: A Programmer's Perspective (Upper Saddle River: Prentice Hall, 2003), 541.
Table of Contents
Note | |
---|---|
This is a new chapter and is still under development. In the meantime, check the further reading section, Section A.6, “More on version control”, and the listing of graphical alternatives at Section B.5, “GUI-based version control tools”. |
Table of Contents
For finding information quickly, searching with Google is often the best method. You can look up commands, symbols or messages that you don’t recognize, and generic (that is, not specific to your program) compiler errors/warnings with Google. Wikipedia can also be a valuable resource, as can be seen by the numerous links to Wikipedia articles throughout this appendix and the guide as a whole. In addition, consider making the man and info pages (which are discussed in Chapter 2, Built-in help system) the first place you look for information about commands, given that they often have the information you need and are immediately available.
If you would prefer to read books, those published by O’Reilly are often useful sources of information about Linux and other open source software products. Many of those books would be available through a public or university library, either on the shelf or electronically through Safari Books Online if your library is a subscriber. Selected relevant titles from O'Reilly and other publishers are listed in the sections that follow.
This section provides further reading for the topics covered in Chapter 1, Linux and its user interfaces.
The Linux manual pages (discussed in Section 2.1, “Manual (man) pages”) can also be found on the Web; the Wikipedia article on Unix manual pages has a list of repositories.
GNU has a list of manuals for all of its software.
The Linux Documentation Project (TLDP) is a massive source of information, with many guides, HOWTOs, and other documents.
Prof. Norman Matloff at UC-Davis has a Unix and Linux Tutorial Center, with numerous articles on Unix/Linux and programming in C.
LinuxCommand.org has a short tutorial and some information on shell scripting and other topics.
Machtelt Garrels has written an Introduction to Linux.[45]
Google offers a Linux-only special search.
As for books:
Barr, Joe. CLI for Noobies: A Primer on the Linux Command Line. Boston: Prentice Hall, 2008.
Barrett, Daniel J. Linux Pocket Guide. Sebastopol: O'Reilly Media, 2004.
Bovet, Daniel, and Marco Cesati. Understanding the Linux Kernel. 3rd ed. Sebastopol: O'Reilly Media, 2005.
Dalheimer, Matthias Kalle, and Matt Welsh. Running Linux. 5th ed. Sebastopol: O'Reilly, 2006.
Raymond, Eric Steven. The Art of Unix Programming. Boston: Addison-Wesley, 2003. This book can also be found online at the author's website.
Siever, Ellen, et al. Linux in a Nutshell. 5th ed. Sebastopol: O'Reilly, 2005.
Sobell, Mark G. A Practical Guide to Linux Commands, Editors, and Shell Programming. Upper Saddle River: Prentice Hall, 2005.
Stutz, Michael. The Linux Cookbook: Tips and Techniques for Everyday Use. 2nd ed. San Francisco: No Starch Press, 2004.
Binh Nguyen has a TLDP guide on the Linux Filesystem Hierarchy.[45]
You can also learn quite a bit about the file system from man hier
.[46]
[45] The direct link provided is for the “chunked” HTML version of that guide. If you want to pick a different format, check the TLDP's guides section.
[46] Barr, CLI for Noobies, 32.
Tip | |
---|---|
You may also want to check the resources listed in Section A.2.1, “General Linux resources” for information on the Linux CLI. |
This section provides further reading for the topics covered in Chapter 3, Basic shell commands and related utilities.
GNU Bash has an online manual.
Machtelt Garrels (mentioned in Section A.2.1, “General Linux resources”) has a Bash Guide for Beginners.[45]
Of course, you can also check info bash
.
As for books:
Albing, Carl, and JP Vossen and Cameron Newham. bash Cookbook: Solutions and Examples for bash Users. Sebastopol: O'Reilly, 2007.
Kiddle, Oliver, and Jerry Peek and Peter Stephenson. From Bash to Z Shell: Conquering the Command Line. Berkeley: Apress, 2004.
Newham, Cameron. Learning the bash Shell. 3rd ed. Sebastopol: O'Reilly, 2005.
If your default shell (which you can check by typing echo $SHELL
) is not Bash and you'd like to try using Bash, here's one way to do so:
First, go to your home directory (in case you are not already there) by typing cd
at the prompt.[48]
Next, check to see if the .bashrc
file[47]
already exists by typing ls .bashrc
at the prompt.[48]
If .bashrc
does exist, then back it up by typing mv .bashrc .bashrc.old
at the prompt,[49] which will rename .bashrc
to .bashrc.old
.
Next, type the following two lines exactly as they appear (including all of the quotation marks but without any footnote references), pressing Enter after each line, to create your own .bashrc
file:
Now, to switch to Bash when you're using your default shell, just type bash
at the prompt.
Compared to how my prompt looked in Section 1.4, “Shells, the shell prompt, and your home directory”, it now looks like this: [bash jg18@grid ~]$
Note | |
---|---|
If nothing happens once you complete these steps, try completing them again but using |
To exit Bash and return to your default, type exit
at the prompt. Note that if you want to exit the terminal when you're using Bash in this way, you'll need to type exit
twice: once to exit Bash and then again to exit your default shell.
The best article on text processing with the shell that I know of is “Unix for Poets”, and since I'm not sure whether any given link to this article (in .pdf) will last, the best way to find it is through a Google search.
A good companion to “Unix for Poets” is “egrep
for Linguists”.
As for books:
Dougherty, Dale, and Arnold Robbins. sed & awk. 2nd ed. Sebastopol: O'Reilly Media, 1997.
Friedl, Jeffrey. Mastering Regular Expressions. 3rd ed. Sebastopol: O'Reilly Media, 2006.
Tip | |
---|---|
The resources mentioned in Section A.3.1, “General information on the Bash shell” may also be helpful in learning about shell scripting. |
Mendel Cooper has written an Advanced Bash-Scripting Guide.[45]
As for books:
Robbins, Arnold, and Nelson H.F. Beebe. Classic Shell Scripting. Sebastopol: O'Reilly, 2005.
GNU has an online manual for coreutils
.
Wikipedia has an article as well.
The blog Command Line Warriors has a post entitled Ten Cool Coreutils Commands.
You can also find a great deal of built-in information by typing info coreutils
.
[47] The .bashrc
file contains your personal settings for Bash. Most likely, this file does not exist.
[48] The commands cd and ls are discussed in Section 3.2, “Navigating the file system”.
[49] The command mv is discussed in Section 3.3, “Manipulating the file system”.
[50] This will set up your Bash prompt to look like the one shown in Section 1.4, “Shells, the shell prompt, and your home directory” but with the word bash
after the [
character to remind you that you're using Bash.
[51] Note that there is a space after the $
character.
[52] The command echo and the I/O redirection operators >
and >>
are discussed in Section 3.5, “Working with text streams”.
[53] This line is optional but recommended, as it will add color to the output of ls,[48] so that when you get the listing of a directory's contents with ls, different types of files (including directories, executables, etc.) will appear in different colors, which makes it easier to tell them apart.
However, as far as I know, this line will only work when your terminal supports color (which it probably does), which you can check by typing echo $TERM
and confirming that the following is the output:
xterm-color
Finally, this line was taken from a discussion on a Brandeis University bulletin board in a thread called “SSH Colors”.
This section provides further reading for the topics covered in Chapter 5, Text editors.
Vim has its own website and documentation.
A ~575-page book on Vim is available on the Web (in .pdf), with errata for the book listed here.
Prof. Norm Matloff (mentioned in Section A.2.1, “General Linux resources”) has An Extremely Quick and Simple Introduction to the Vi Text Editor.
NGEDIT Software has a graphical vi-vim cheat sheet and tutorial in GIF and SVG formats.
If you'd like to try the original vi, check out the traditional ex/vi project.
Perhaps the best way to learn how to use Vim is through its built-in tutorial, which you can reach by typing vimtutor
at the shell prompt in a terminal. Since you can't modify the read-only “master copy” of the tutor, vimtutor makes a temporary copy of it that you can modify, allowing you to learn by doing rather than by reading and trying to memorize.
As for books:
Robbins, Arnold. vi Editor Pocket Reference. Sebastopol: O'Reilly, 1998.
Robbins, Arnold, and Linda Lamb. Learning the vi Editor. 6th ed. Sebastopol: O'Reilly, 1998.
Robbins, Arnold, and Elbert Hannah and Linda Lamb. Learning the vi and Vim Editors. 7th ed. Sebastopol: O'Reilly, 2008.
GNU has a web page for Emacs, online Emacs manuals, and An Introduction to Emacs Lisp Programming.
Prof. Norm Matloff (mentioned in Section A.2.1, “General Linux resources”) has a tutorial entitled Emacs: The Software Engineer's ``Swiss Army Knife''.
Don't forget about Emacs's built-in tutorial, which you can find by typing C-h t
while in Emacs.
There's also the Emacs info page at info emacs
.
As for books:
Cameron, Debra. GNU Emacs Pocket Reference. Sebastopol: O'Reilly, 1998.
Cameron, Debra, et al. Learning GNU Emacs. 3rd ed. Sebastopol: O'Reilly, 2005.
Chassell, Robert J. An Introduction to Programming in Emacs Lisp. 2nd ed. Boston: GNU Press, 2004. This book can also be found online at the author's website.
Tip | |
---|---|
As with all GNU software products, more information about the components of the GNU toolchain is available from their respective info pages (for example, by typing |
This section provides further reading for the topics covered in Chapter 6, Programming tools.
Gareth Anderson's GNU/Linux Command-Line Tools Summary is available as a TLDP guide.[45]
Peter Jay Salzman, Michael Burian, and Ori Pomerantz have written Linux Kernel Module Programming Guides for version 2.4 and version 2.6 of the Linux kernel.[45]
Wikipedia has an article on the GNU toolchain.
As for books:
Bryant, Randal, and David O'Hallaron. Computer Systems: A Programmer's Perspective. Upper Saddle River: Prentice Hall, 2003.
Fusco, John. The Linux Programmer's Toolbox. Upper Saddle River: Prentice Hall, 2007.
Jones, M. Tim. GNU/Linux Application Programming. Hingham: Charles River Media, 2005.
Stevens, W. Richard, and Stephen A. Rago. Advanced Programming in the UNIX Environment. 2nd ed. Upper Saddle River: Addison-Wesley, 2005.
There's GCC's official homepage, along with a set of the official manuals.
As for books:
Gough, Brian J., and Richard M. Stallman. An Introduction to GCC. Bristol: Network Theory Ltd., 2004.
von Hagen, William. The Definitive Guide to GCC. 2nd ed. Berkeley: Apress, 2006.
GNU Make has its own page and an accompanying manual.
As for books:
Mecklenburg, Robert. Managing Projects with GNU Make. 3rd ed. Sebastopol: O'Reilly Media, 2004.
GNU's website has documentation for GDB.
There's a “GDB Quick Reference” floating around the Internet that you can find with a Google search.
Peter Jay Salzman has a GDB Tutorial.
As for books:
Matloff, Norman, and Peter Jay Salzman. The Art of Debugging with GDB, DDD, and Eclipse. San Francisco: No Starch Press, 2008.
The official page for binutils
is from sourceware.org.
Wikipedia has an article on binutils
as well.
This section provides further reading for the topics covered in Chapter 7, Version control.
The CVS website has a link to the CVS Information Page, which includes a FAQ, wiki, and manual.
Subversion's website has some information.
Perhaps the best source of information is a book by Ben Collins-Sussman, Brian W. Fitzpatrick, and C. Michael Pilato that is published by O'Reilly but can also be found online: Version Control with Subversion.
Table of Contents
Okay, I'll admit it: when I first took Linux-based programming courses in C and C++, I used graphical versions of some of these tools sometimes, and sometimes I even found myself more productive when I used the two together as needed. Therefore, I think that I should at least mention the existence of graphical alternatives to the tools I've presented.
As noted in Section 1.2, “Graphical user interfaces (GUIs) for Linux”, the two most popular desktop environments for Linux are GNOME and KDE. Thus, most of the software discussed in this appendix will be related to one of the two.
A commonly used file manager for KDE is called Konqueror; it can also be used as, among other things, a web browser.
In KDE 4, however, Konqueror has been replaced with Dolphin as the default file manager, although Konqueror can still be used as a file manager in KDE 4.
The file manager for GNOME is called Nautilus.
Note | |
---|---|
Be aware, though, that you will be able to work much more efficiently with the file system through the shell. Nonetheless, the file manager can be useful sometimes. |
Options for graphical file transfer programs include gFTP and KFTPgrabber.
There are graphical versions of vi (such as gVim) and of Emacs in which pull-down menus and icons are available for use with the mouse. Be aware, however, that the more you use the keyboard instead of the mouse, the faster you can work, with the exception that selecting text for cutting, copying, or deleting may be faster with the mouse.
The Eclipse IDE, which is particularly popular for Java development, has an extension called the C/C++ Development Tools (CDT). The Eclipse downloads page includes a specialized version of Eclipse for C/C++ development.
KDE has an IDE called KDevelop.
One option for a GUI-based debugger is the Data Display Debugger (DDD).
A list of some other options can be found at Wikipedia's article on debugger front-ends.