You are on page 1of 10

Appendix A

Common Linux Commands


In This Appendix
▶ Managing storage with archiving and compression commands
▶ Keeping in touch with communication commands
▶ Organizing data with file commands
▶ Bossing machines around with system-control commands

C omputing novices often marvel at the keyboard dance that Linux


experts typically perform. Sure, these experts know about modern
advances like the mouse and the graphical interface, but these keyboard
musicians prefer the home keys and find that they can work faster that way.
It takes some time to reach this level of proficiency, but every expert was
a novice at one time, and any novice can become an expert with plenty of
practice.

In this appendix, you find the commands listed by themes, according to what
they can actually do for you. So read on — and dazzle your friends with your
command-prompt finesse. When they ask you how and where you figured out
all those commands, just smile and mumble something about the voices in
your head — and, of course, keep this section dog-eared and within reach of
your computer.

Linux Commands by Function


Because every command serves a specific purpose, organizing these tools
into groups according to their individual functions isn’t difficult. If you know
what you need to do but don’t know which command does the job, flip
through this section to start your search. From here, you can dig further by
referencing man pages. To access a man page, type man command at a com-
mand prompt. For example, man ls shows you the help information for the
file-listing command.
410 Part VI: Appendixes

Getting help
When you’re digging around for help on a command, you can call on an inter-
esting range of shell commands for assistance, as shown in Table A-1.

Table A-1 Shell Help Commands


Command Purpose
apropos Looks for commands that contain a keyword in their man
page descriptions.
info One way of finding help information. You can find instruc-
tions for this tool at www.gnu.org/software/
texinfo/manual/info/, or you can use the built-in
tutorial by starting the info tool and pressing the H key
when inside it.
man The primary way of getting help in Linux and UNIX.
whatis Gets and displays a one-line description of a command.

Locating details about the command-prompt options of a command is a


never-ending pursuit. The man page system provides some helpful guides at
your fingertips for rapidly finding this detailed information.

Archiving and compressing


Although disk space isn’t as hard to come by as it once was, bandwidth and
backup media are still at a premium. Subsequently, this group of commands
provides a potpourri of tools for compacting and organizing data for storage,
as shown in Table A-2.

Table A-2 Archiving and Compressing Tools


Command Purpose
bzip2 Compresses files into .bz2 format. Used mostly for
incredibly large sets of text files (which is what source
code actually is).
bunzip2 Uncompresses .bz2 files.
Appendix A: Common Linux Commands 411
Command Purpose
compress Compresses files into .Z format. Pretty old and not used
much in the Linux world anymore.
gunzip Uncompresses .gz files and .tgz files.
gzip Compresses files into .gz format.
tar Packages files together in a group called a tarball. The
most common way of using this command is tar xvf
filename, as in tar xvf download.tar.
uncompress Uncompresses files from .Z format.
unzip Uncompresses files from .zip format.
zip Compresses files into .zip format.

Built-in bash commands


Some commands don’t even seem to exist if you try to look up their help
information in the man pages, and the commands themselves don’t show up
as files on your system. Remember, as you type commands at the prompt,
you’re communicating with a type of program called a shell. (The bash is
the default Linux shell.) The shell has a set of commands that you can use to
communicate with it, as shown in Table A-3.

Table A-3 Shell Commands


Command Purpose
alias Creates or lists command shortcuts.
env Lists your current environment variables and their settings.
export Whenever you’re told to set an environment variable, create
the variable and then use this command so that the variable
will be remembered properly.
history Lists the last 1,000 commands you’ve typed.
unalias Removes command shortcuts.

If you try to view the man page entries for some of these commands, you find
instead that the help information for BASH BUILTINS appears. To search
through this manual, press the forward slash (/) key to open the man search
interface — and then type the name of the command you want to search for.
412 Part VI: Appendixes

Press Enter to start the search. The interface stops in the first spot where the
term is found. If you want to try again, press the N key to proceed to the next
occurrence of the word.

For example, you might be reading the massive bash man page (type man
bash to access this page), but perhaps you’re only interested in items
related to prompts, which are the bits of text that appear to the left of your
cursor in a text window. An example prompt is

rich@textbox:~$

You might type /prompt and press Enter to jump to the first instance of this
word. If the text around the word doesn’t reflect what you’re looking for,
press the N key to jump to the next one, and so on.

Files and the Filesystem


No matter which operating system you’re using, it’s hard to do anything with-
out being able to find your way through and work with the filesystem. The
utilities in the following subsections help you find your way.

File organization
Boxing, packing, sorting, shipping — you’re probably always shuffling files
around on your system. File organization commands provide tools for
moving files and filesystem units around, as shown in Table A-4.

Table A-4 File Organization Tools


Command Purpose
cd Changes directories.
cp Copies a file.
df Shows partitions and how much space they have.
du Shows how much disk space is being used in the current
directory and below.
ln Creates a shortcut.
ls Lists the contents of a directory or information about a file.
Appendix A: Common Linux Commands 413
Command Purpose
mkdir Creates a directory.
mv Moves or renames a file.
pwd Shows the path for the directory you’re currently in.
rm Deletes a file.
rmdir Deletes an empty directory.

File attributes
Files are much like candy bars: The wrappers provide information about
the ingredients, size, and package date — all descriptive of the tasty nugget
inside. (Perhaps the wrapper is even childproof.) Files keep all this wrapper
information in an inode. Along with the capability to change the file inode
information, file-attribute commands can return data about the content of the
file, as shown in Table A-5.

Table A-5 File-Attribute Commands


Command Purpose
chgrp Changes the group associated with a file.
chmod Changes a file’s permissions.
chown Changes who owns a file.
file Shows what type of file you’re dealing with.
stat Shows some statistics about the file.
touch Creates an empty file of this name.
wc Shows how many words, lines, and so on are in this file.

File locators
Where, oh, where can your file be? File-locator commands, shown in Table
A-6, help you locate files in Linux’s monster tree-structure filesystem.
414 Part VI: Appendixes

Table A-6 File-Locator Commands


Command Purpose
find Heavy-duty filesystem search tool.
locate Lighter-weight filesystem search tool.
which Tells you the path for the program that would run if you
typed this command.

File viewers
Text-file browsing is a favorite pastime of many a system user. These tools
provide a variety of utilities for viewing the contents of readable text files
of all sizes. Unlike the commands in a full-screen editor, none of these com-
mands (shown in Table A-7) can damage the contents of a file. That’s because
they’re just viewers, not editors.

Table A-7 File-Viewer Commands


Command Purpose
cat Dumps the contents of the file to your screen.
head Shows the first ten lines of a file.
less Shows the file a screen at a time.
more Shows the file a screen at a time.
tail Shows the last ten lines of a file.

Filesystem commands
Filesystem commands, listed in Table A-8, provide information or perform
actions on the entire filesystem, from creation and tuning to repair and
recovery. Some of these commands return data only, whereas others also
provide you with surgical instruments for serious filesystem hacking.
Appendix A: Common Linux Commands 415
Table A-8 Filesystem Commands
Command Purpose
badblocks Searches a partition for bad blocks.
e2fsck Checks and repairs an ext2 or ext3 filesystem.
e2label Applies a filesystem label to an ext2 or ext3 partition.
eject Ejects a CD or DVD.
fsck Can check and repair many types of filesystems.
mkfs Creates a filesystem (format a partition).
mount Loads a partition into your filesystem.
sync Saves all information out of buffers onto disks.
tune2fs Adjusts ext2 and ext3 filesystem parameters.
umount Removes a partition from the filesystem.

mtools
The mtools suite of utilities provides a nice way to transfer information
to your Microsoft friends. Although Linux has native support for Microsoft
Windows and DOS filesystems, your Microsoft cohorts don’t have access to
Linux (ext2 and ext3) filesystems. To keep everyone happy, you can buy
preformatted MS-DOS discs and use them with the mtools commands (see
Table A-9) so you can swap files with your friends who are using Windows.

Table A-9 mtools Commands


Command Purpose
mcd Changes directory in DOS format on a DOS disk.
mcopy Copies DOS files to and from Linux.
mdel Deletes a DOS file.
mdeltree Deletes a DOS directory and its contents.
mdir Lists a DOS directory’s contents.
(continued)
416 Part VI: Appendixes

Table A-9 (continued)


Command Purpose
mdu Shows how much space is taken and available for a DOS
partition.
mformat Formats a partition for DOS.
mlabel Applies a DOS volume label.
mmd Creates a DOS directory.
mmount Mounts a DOS disk or partition.
mmove Moves or renames a DOS file or directory.

System Control
These commands provide system-wide information and control. Normal
users can run many commands to obtain system information; however, com-
mands that actively change the configuration of the system need to run while
you are logged in as root — or have utilized the su command to become the
superuser temporarily.

Administration
Some administration commands (shown in Table A-10) don’t fall neatly into a
category.

Table A-10 Administration Commands


Command Purpose
passwd Changes a particular user’s password. Any user can run this
command to change their own password. Only root can use
it to change someone else’s.
su Switches to another user account without logging out of this
one. The best way to use this command is su - so your
filesystem path and other information are loaded.
Appendix A: Common Linux Commands 417
Kernel-module handling
You may sometimes need to add kernel support for an additional device
(software or hardware). When you do, you have a limited number of choices:
You can rebuild the kernel or install a loadable kernel module. Although
rebuilding a kernel doesn’t exactly require a PhD in nuclear science, consider
it a time-consuming nuisance that’s best to avoid. The commands in Table
A-11 enable you to include the kernel support you need while the system is
running, without having to rebuild the entire thing from scratch.

Table A-11 Kernel-Support Commands


Command Purpose
depmod Regenerates your module dependencies.
insmod Loads a module by hand.
lsmod Lists the modules your kernel has loaded.
modprobe Loads a module by hand, along with its dependencies and
settings.
rmmod Unloads a module by hand.

Processes
Most of your system activity requires processes. Even when your system
appears idle, a dozen or so processes (programs) are running in the back-
ground. The process commands, shown in Table A-12, enable you to check
under the hood to make sure that everything that should be running is run-
ning — and that you’re not overheating or overtaxing your computer’s
resources.

Table A-12 Process Commands


Command Purpose
crontab Sets up commands to run at regular intervals.
kill Stops a process by its number. Often used as kill -9 for
a harsh stop if a process just won’t die.
killall Stops a process by name rather than number.
(continued)
418 Part VI: Appendixes

Table A-12 (continued)


Command Purpose
nice Assigns a CPU use priority to a process.
pidof Gets a program’s ID number.
ps Gets a lot of programs’ ID numbers; normally you’ll see it
used as ps aux.
pstree Shows the relationships between programs.
renice Changes a program’s CPU use priority.
top Shows resource use over time.

You might also like