4. Command-Line Shortcuts
If ever there were an installment of Linux Explorers to print out and tack up on the wall beside your Linux machine, it's this one. Linux is nowhere near to escaping its Unix roots (and many Linux users feel that's a good thing). This week we'll tackle the Linux Console and the many commands you use in it. Windows users who date back to the DOS days will find all this strangely familiar, since many DOS commands typed at the C:> prompt bear more than a passing resemblance to their Unix/Linux counterparts. On the other hand, this can also be deceiving. DOS and Linux are very different.

The Console or Terminal
Windows XP's Command Prompt window or the "DOS Box" of earlier versions of Windows has a parallel under Linux. The actual name of this command-line-entry object depends on the Linux distro you're running, but it's frequently known as the "Console" or "Terminal." In Mandrake, for example, it's called the Console. In Red Hat, it's called the Terminal. You'll find your Console or Terminal somewhere on the main menu of your desktop graphical user interface. When you open it, you'll be presented with a text prompt that will end with: $. That means you're logged in as a normal user.

After typing the "su" command and providing your password, the $ prompt will change to a # prompt, indicating that you're logged in as root (roughly equivalent to an Administrator under Windows, but it carries more weight in Linux):

su

Ctrl-D logs you out as root and brings back the $ prompt. And if you use the Ctrl-D keyboard combination again, the Console will close. A normal prompt looks something like:

[localhost@localdomain:~]$

(Note: The words localhost and localdomain may appear as something different on your Linux PC.)

For nearly every program or command there are "man"-ual pages stored on your computer. Man pages are built-in Linux documentation files. You can read them by typing this in the Console:

man

Here's a specific example. The following command elicits the man pages for the cp command (which handles file copies).

man cp

Use the spacebar to scroll the page, and press the q key to close the man pages.

More information on individual Console commands can often be found by typing " --help" or " --info" after any command, like this:

man --help
cp --info

Navigating the File System and Simple Commands
These are the most often used navigational or directory-related Linux Console/Terminal commands:

Command  Function
cdChanges the current directory.
cd ..Changes the current directory up to the parent directory.
cd /Changes to the root directory.
mkdirMakes a new directory.
rmdirRemoves, or deletes, the named directory.
rmRemoves, or deletes, named file.
cpCopies the named files or directories.
mvMoves or renames the named files or directories.
lsLists the contents of a container, such as a directory.
ls -aLists the contents of a container, including hidden objects.*
catShows the contents of a file.
touchMakes an empty text file.

* Note: Hidden files begin with a period, like this: .Test1

The following are some actual commands you might use and how they would work on your Linux system. These examples assume you've opened a console window on desktop 1 and the home directory in desktop 2, which lets you see and verify the commands you enter in desktop 1.

Command        Function
mkdir OneMakes the directory One in your home directory.
mkdir oneMakes the directory one in your home directory. Note: Linux is case sensitive so One and one are not the same.
touch Test1Makes an empty text file named Test1
touch Test2Makes an empty text file named Test2
cp Test1 OneCopies the Test1 file into the One directory.
mv Test2 oneMoves the Test2 file into the one directory.
mv one OneMoves the one directory into the One directory.
cd OneSwitches you to the One directory, and places that directory name in the prompt.
lsLists the file contents of the One directory. If you've done everything right so far, you should see one and Test1.
rm Test1Removes (deletes) the Test1 file from the One directory.
cd ..Changes the current directory to the home directory.
rm Test1Removes the Test1 file from the home directory.
rm -rf OneDeletes all the files and directories used for the above examples.

The following are a couple of common Console command combinations followed by an explanation of what they do.

First Combination:
cd /
ls

Changes to the / (root) directory and lists the files and directories there, such as: /boot, /etc, /opt, /root, /tmp, /var, /bin, /dev, /home, /lib, /mnt, and /usr.

Second Combination:
cd /mnt
ls

Shows the mounted and unmounted devices that can be found in the /mnt directory, such as: cdrom, cdrom2, floppy, and (win_c).

To learn more about the above commands and the arguments you can use with them, use the man command followed by the command name, like this:

man cp
man mv

A Few Handy Console Commands

For IP Networking:
su
ifconfig  (shows assigned IP addresses)
route  (shows traffic)

Display Disk Performance:
hdparm -t /dev/hda

Space Usage:
df -h
du -s /var/log/*  (space usage of all the files in /var/log)

Showing Available Memory:
free -b  (in bytes)
free -k  (in Kilobytes)
free -m  (in Megabytes)
free -o  (without buffers)
free -t  (totals)

Take and Store Screenshot:
import -w root screen.jpg  (Stores the file in your /home directory)

See also The One Page Linux Manual in .PDF format.