This article is reprinted from The Official Ubuntu Book, 2nd Edition, by Benjamin Mako Hill and Jono Bacon, with permission of publisher Prentice Hall Professional, copyright 2007, all rights reserved. While instructions are specific for Ubuntu Linux, most commands will work with other Linux distributions.
Moving Around the Filesystem
Commands for moving around the filesystem include the following.
- pwd: The
pwdcommand allows you to know the directory in which you're located (pwd stands for "print working directory"). For example,
pwdin the desktop directory will show ~/Desktop. Note that the GNOME terminal also displays this information in the title bar of its window.
cd: The cd command allows you to change directories. When you open a terminal, you will be in your home directory. To move around the filesystem, use
• To navigate to your desktop directory, use
• To navigate into the root directory, use
• To navigate to your home directory, use
• To navigate up one directory level, use
• To navigate to the previous directory (or back), use• To navigate through multiple levels of directories at once, use
cd /var/www, for example, which will take you directly to the /www subdirectory of /var.
You can manipulate files and folders by using the following commands.
- cp: The
cpcommand makes a copy of a file for you. For example,
cp file foomakes an exact copy of the file whose name you entered and names the copy foo, but the first file will still exist with its original name. After you use
mv, the original file no longer exists, but after you use
cp, that file stays and a new copy is made.
- mv: The mv command moves a file to a different location or renames a file. Examples are as follows:
mv file foorenames the original file to foo.
mv foo ~/Desktopmoves the file foo to your desktop directory but does not rename it. You must specify a new filename to rename a file.
To save on typing, you can substitute ~ in place of the home directory.Note: If you are using
sudo, you will not be able to use the ~ shortcut. Instead, you will have to use the full pathnames to your files.
- rm: Use this command to remove or delete a file in your directory. It does not work on directories that contain files.
- ls: The
lscommand shows you the files in your current directory. Used with certain options, it lets you see file sizes, when files where created, and file permissions. For example,
ls ~shows you the files that are in your home directory.
- mkdir: The
mkdircommand allows you to create directories. For example,
mkdir musiccreates a music directory.
chmod: ThePermissions are based on a fairly simple model. You can set permissions for user, group, and world, and you can set whether each can read, write, and/or execute the file. For example, if a file had permission to allow everybody to read but only the user could write, the permissions would read
chmodcommand changes the permissions on the files listed.
rwxr--r--. To add or remove a permission, you append a
-in front of the specific permission. For example, to add the capability for the group to edit in the previous example, you could type
chmod g+x file.
- chown: The
chowncommand allows the user to change the user and group ownerships of a file. For example,
chown jim filechanges the ownership of the file to Jim.
System Information Commands
System information commands include the following.
- df: The
dfcommand displays filesystem disk space usage for all partitions. The command
df-his probably the most useful. It uses megabytes (M) and gigabytes (G) instead of blocks to report. (
- free: The
freecommand displays the amount of free and used memory in the system. For example,
free -mgives the information using megabytes, which is probably most useful for current computers.
- top: The
topcommand displays information on your Linux system, running processes, and system resources, including the CPU, RAM, swap usage, and total number of tasks being run. To exit
top, press Q.
- uname -a: The
unamecommand with the
-aoption prints all system information, including machine name, kernel name, version, and a few other details. This command is most useful for checking which kernel you're using.
lsb_release -a: The
lsb_releasecommand with the
-aoption prints version information for the Linux release you're running. For example:
user@computer:~$ lsb_release -a
LSB Version: n/a
Distributor ID: Ubuntu
Description: Ubuntu (The Breezy Badger Release)
- ifconfig: This reports on your system's network interfaces.
- iwconfig: The
iwconfigcommand shows you any wireless network adapters and the wireless-specific information from them, such as speed and network connected.
- ps: The
pscommand allows you to view all the processes running on the machine.
The following commands list the hardware on your computer, either of a specific type or with a specific method. They are most useful for debugging when a piece of hardware does not function correctly.
- lspci: The
lspcicommand lists all PCI buses and devices connected to them. This commonly includes network cards and sound cards.
- lsusb: The
lsusbcommand lists all USB buses and any connected USB devices, such as printers and thumb drives.
- lshal: The
lshalcommand lists all devices the hardware abstraction layer (HAL) knows about, which should be most hardware on your system.
- lshw: The
lshwcommand lists hardware on your system, including maker, type, and where it is connected.
Searching and Editing Text Files
Search and edit text files by using the following commands.
- grep: The
grepcommand allows you to search inside a number of files for a particular search pattern and then print matching lines. For example,
grep blah filewill search for the text "blah" in the file and then print any matching lines.
- sed: The
sed(or Stream EDitor) command allows search and replace of a particular string in a file. For example, if you want to find the string "cat" and replace it with "dog" in a file named pets, type
sed s/cat/dog/g pets.
Both grep and sed are extremely powerful programs. There are many excellent tutorials available on using them, but here are a few good Web sites to get you started:
Three other commands are useful for dealing with text.
- cat: The
catcommand, short for concatenate, is useful for viewing and adding to text files. The simple command
cat FILENAMEdisplays the contents of the file. Using
cat FILENAME fileadds the contents of the first file to the second.
- nano: Nano is a simple text editor for the command line. To open a file, use
nano filename. Commands listed at the bottom of the screen are accessed via pressing Ctrl followed by the letter.
- less: The
lesscommand is used for viewing text files as well as standard output. A common usage is to pipe another command through less to be able to see all the output, such as
ls | less.
- adduser: The
addusercommand creates a new user. To create a new user, simply type
sudo adduser $loginname. This creates the user's home directory and default group. It prompts for a user password and then further details about the user.
- passwd: The
passwdcommand changes the user's password. If run by a regular user, it will change his or her password. If run using sudo, it can change any user's password. For example,
sudo passwd joechanges Joe's password.
- who: The
whocommand tells you who is currently logged into the machine.
- addgroup: The
addgroupcommand adds a new group. To create a new group, type
sudo addgroup $groupname.
- deluser: The
delusercommand removes a user from the system. To remove the user's files and home directory, you need to add the
- delgroup: The
delgroupcommand removes a group from the system. You cannot remove a group that is the primary group of any users.
Getting Help on the Command Line
This section provides you with some tips for getting help on the command line. The commands
man are the two most important tools at the command line.
Virtually all commands understand the
--help) option, which produces a short usage description of the command and its options, then exits back to the command prompt. Try
man -h or
man --help to see this in action.
Every command and nearly every application in Linux has a man (manual) file, so finding such a file is as simple as typing
man command to bring up a longer manual entry for the specified command. For example,
man mv brings up the
mv (move) manual.
Some helpful tips for using the man command include the following.
- Arrow keys: Move up and down the man file by using the arrow keys.
- q: Quit back to the command prompt by typing
- man man:
man manbrings up the manual entry for the
mancommand, which is a good place to start!
- man intro:
man introis especially useful. It displays the Introduction to User Commands, which is a well-written, fairly brief introduction to the Linux command line.
There are also info pages, which are generally more in-depth than man pages. Try
info info for the introduction to info pages.
Searching for Man Files
If you aren't sure which command or application you need to use, you can try searching the man files.
- man -k foo: This searches the man files for "foo". Try
man -k nautilusto see how this works.
man -k foois the same as the apropos command.
- man -f foo: This searches only the titles of your system's man files. Try
man -f gnome, for example.
man -f foois the same as the
Sometimes you need to look at or use multiple files at the same time. For instance, you might want to delete all .rar files or move all .odt files to another directory. Thankfully, you can use a series of wildcards to accomplish such tasks.
- * matches any number of characters. For example,
*.rarmatches any file with the ending .rar.
- ? matches any single character. For example, ?
.rarmatches a.rar but not ab.rar.
- [characters] matches any of the characters within the brackets. For example,
[ab].rarmatches a.rar and b.rar but not c.rar.
- [!characters] matches any characters that are not listed. For example,
[!ab].rarmatches c.rar but not a.rar or b.rar.
Executing Multiple Commands
Often you may want to execute several commands together, either by running one after another or by passing output from one to another.
If you need to execute multiple commands in sequence but don't need to pass output between them, there are two options based on whether or not you want the subsequent commands to run only if the previous commands succeed or not. If you want the commands to run one after the other regardless of whether or not preceding commands succeed, place a
; between the commands. For example, if you want to get information about your hardware, you could run
lspci ; lsusb, which would output information on your PCI buses and USB devices in sequence.
However, if you need to conditionally run the commands based on whether the previous command has succeeded, insert
&& between commands. An example of this is building a program from source, which is traditionally done with
./configure, make, and
make install. The commands
make install require that the previous commands have completed successfully, so you would use
./configure && make && make install.
If you need to pass the output of one command so that it goes to the input of the next, after the character used between the commands, you need something called a pipe, which looks like a vertical bar or pipe (
To use the pipe, insert the
| between each command. For example, using the
| in the command
ls | less allows you to view the contents of the
ls more easily.
Moving to More Advanced Uses of the Command Line
There are a great number of good books out there for working the command line. In addition, because most of the command line has not changed in many years, a large body of information is available on the Internet. If you need help with something, often simply searching for the command will turn up what you need.
To get you started, here are some recommendations.
- A Practical Guide to Linux Commands, Editors and Shell Programming by Mark G. Sobell (Prentice Hall, 2005) is a good book for any user of the shell in Linux to have on his or her bookshelf.
- LinuxCommand.org is an excellent Web site designed to help people new to using the command line.
- The Linux Documentation Project is an excellent and free resource for many things Linux.
This content was excerpted from the new second edition of The Official Ubuntu Book, authored by Benjamin Mako Hill and Jono Bacon, with Ivan Krstic, David Murphy, Jonathan Jesse, Peter Savage and Corey Burger, published by Prentice Hall Professional, July 2007. Copyright 2007 Canonical Ltd., all rights reserved.