Test Driving Linux/The Command Line
So far, this book has explored using Linux from the KDE desktop environment and several graphical programs. But these programs represent only one way in which you can interact with a Linux computer. Underneath all of the glitter and eye candy of the graphic layer lurks another user interface—the command line.
The command line is an all-text interface to the operating system. It is the playground and work environment of Linux users everywhere. You are not considered to have truly arrived as a Linux user until you have mastered the command-line interface. And once you've done so, you will know why Linux users swear by it. The command line is simply the easiest, fastest, and most powerful way to get certain types of work done.
Linux users also use the command line to perform routine maintenance on their computer, configure hardware, or tweak the system for performance. It's a lot like being trained in auto repair and being able to perform you own routine maintenance on your car. A skilled home mechanic can fix her car quickly, with minimal cost, and with the assurance of quality work being done.
This chapter is a basic introduction to the Linux command line. It is based around simple tasks that will familiarize you with the interface and are useful for you to know. The content of this chapter is similar to that of Chapter 3, where you learned to manipulate files using Konqueror. In this chapter you'll learn how to copy, move, delete, and rename files and directories, compress files with various zip utilities, and identify runaway programs and stop them. You'll even learn how the command line itself can teach you how to use it better. Though this is just the beginning of what you can do on the command line, it should be enough to make you feel comfortable with this interface as you continue your exploration of Linux.
Understanding the Command Line
Linux users interact with Linux on the command line through a terminal program, also called a terminal emulator, xterm, shell, or console. KDE comes with a very useful terminal emulator called Konsole (Figure 11-1). To launch Konsole, click K Menu→ Administer your system→ Use a terminal emulator.
The text already visible inside the program window is called a prompt. By typing various commands at the prompt, you can control the operating system. Most commands accept options and arguments. An option , sometimes called a switch, modifies the way a command works. It usually consists of a single character prefixed with a dash. An argument tells the command which files, directories, or other targets a command should operate on. You can use the arrow keys and delete keys to correct typing mistakes. Press Enter to execute your command.
Konsole is just one of many terminal programs available on Linux, and it provides several nice features. Keep in mind that these features are properties of Konsole, not terminals in general, so don't expect to find all or even most of them in other terminal programs.
Just like Konqueror, Konsole has tabs that can represent different command lines within the same window. Create a new tab by pressing Ctrl-Shift-N. You can move between tabs by clicking the tab with your mouse or pressing Shift-Left Arrow or Shift-Right Arrow, and reorder the tabs by using Ctrl-Shift-Left Arrow and Ctrl-Shift-Right Arrow. For easy reference, give the tabs names by double-clicking on them and entering a name in the window that appears. If you need to separate a tab from the main window, just right-click the tab and choose Detach Session. When you are done with a Konsole tab or session, simply type exit and press Enter to close it.
Konsole's appearance is customizable to some degree. You can make several quick adjustments from the Settings menu, and especially from the Fonts and Schema submenus. (My personal favorite is the Green on Black Schema.) To make these quick changes stick, choose Settings→ Save as Default. If you want to make more advanced changes, go to Settings→ Configure Konsole. In this configuration window you can perform several adjustments, including making changes to existing schemas or creating new ones. When you are finished making changes, give your schema a new name and save it.
In Linux you can run hundreds of commands on the command line. When you consider all the different options and arguments each command can accept, you have millions of possibilities. Many commands are used primarily by programmers and system administrators, but there are many others that are very useful for regular desktop users.
Learning these basic commands is a great way to become comfortable with the command line. Once you are familiar with some commands, you'll see that they are often easier and faster to use than clicking around on menus and icons. As you get more experienced with Linux, you will probably find yourself using the command line more and more often.
The following examples use a test user account named jharris. Be sure to perform all the steps in order—if you don't, you might end up with different results than what's described here.
In Linux, everything you type at the command line is case sensitive. A file named README is a different file than one called Readme. A command of ls is different than a command of LS.
Getting There from Here
When you begin a car trip, knowing where you're going is only slightly more important than knowing where you are. Directions to the airport are useful only if they start from the place you're at or that you know how to get to. On the command line, it's easy to lose track of where you are. Luckily, the command-line prompt on Move is configured to give you some information. Your command prompt in your Konsole window should look something like this:
Let's go through this from left to right to understand its meaning: a user named jharris is on a machine called localhost and is inside a directory called jharris. (As mentioned in Chapter 3, directories are the same as folders.) The $ indicates that jharris is a regular user.
While a prompt that tells you who you are and where you are is useful, it doesn't tell you the whole story. Imagine there are five directories on your machine called jharris. How do you know which one you're in? It's like giving a friend directions to your house by telling them to start at McDonald's, but not telling them which one. Happily, there is a command that can tell you this information. Type the following at the prompt:
[jharris@localhost jharris]$ pwd /home/jharris
The pwd command displays your present working directory; that is, it tells you where you are. In this case, I am in my home directory, /home/jharris.
You'll notice that a lot of Linux commands are "shorthand" for the words they represent. In this case, pwd stands for print working directory. Knowing the "shorthand" makes it much easier to remember the commands.
Listing files and directories
Besides knowing where you are, it's also nice to know what that place looks like. You can use the ls (list) command to list the files in your current directory:
[jharris@localhost jharris]$ ls Desktop/ tmp/
If you haven't been saving items to a USB key, your home directory listing probably looks a lot like this one. There are several ways you can modify your listing to control how it is displayed and how much information it shows. For instance, to display your directories and files as a single column, specify the -1 (the number) option:
Note that in all commands, the option is separated from the command with a space.
[jharris@localhost jharris]$ ls -1 Desktop/ tmp/
If you want to see more information about your files, like who owns them, when they were last changed, and how big they are, use the -l (the letter) option:
[jharris@localhost jharris]$ ls -l total 0 drwx------ 3 jharris jharris 140 Dec Chapter 9 02:00 Desktop/ drwx------ 3 jharris jharris 40 Dec Chapter 8 20:59 tmp/
You may recall from Chapter 3 that there are a lot of configuration files in your home directory that are normally hidden from view. All these files begin with a period (.), which is the way that Linux hides files and directories that do not need to be seen in normal circumstances. To see these hidden files, add the -a option to the previous command:
[jharris@localhost jharris]$ ls -al total 29 drwx------ 22 jharris jharris 2560 Jan 14 02:27 ./ drwx------ Chapter 4 jharris jharris 512 Jan 13 20:12 ../ -rwx------ Chapter 1 jharris jharris 302 Jan 14 02:19 .bash_history* -rwx------ Chapter 1 jharris jharris 24 Jan 10 23:21 .bash_logout* -rwx------ Chapter 1 jharris jharris 191 Jan 10 23:21 .bash_profile* -rwx------ Chapter 1 jharris jharris 124 Jan 10 23:21 .bashrc* -rwx------ Chapter 1 jharris jharris 66 Jan 14 01:12 .DCOPserver_ localhost.localdomain_ _0* drwx------ Chapter 3 jharris jharris 1024 Jan 13 02:49 Desktop/ drwx------ Chapter 2 jharris jharris 1536 Jan 13 03:40 .figures/ drwx------ Chapter 2 jharris jharris 512 Jan 14 01:12 .gconf/ drwx------ Chapter 2 jharris jharris 512 Jan 14 01:14 .gconfd/ drwx------ Chapter 2 jharris jharris 512 Jan 11 04:21 .gnome/ drwx------ Chapter 3 jharris jharris 512 Jan 11 04:21 .gnome2/ drwx------ Chapter 2 jharris jharris 512 Jan 11 04:21 .gnome2_private/ drwx------ Chapter 4 jharris jharris 1024 Jan 11 04:21 .kde/ drwx------ Chapter 4 jharris jharris 512 Jan 13 03:40 .lgames/ -rwx------ Chapter 1 jharris jharris 1660 Jan 13 02:48 .mailcap* drwx------ Chapter 3 jharris jharris 512 Jan 11 04:21 .mcop/ -rwx------ Chapter 1 jharris jharris 31 Jan 14 01:12 .mcoprc* drwxr-xr-x Chapter 4 jharris jharris 1024 Jan 14 01:12 .mdkmove-truefs/ -rwx------ Chapter 1 jharris jharris 635 Jan 13 02:48 .mime.types* drwx------ Chapter 3 jharris jharris 512 Jan 13 02:47 .netscape/ drwx------ Chapter 3 jharris jharris 512 Jan 13 02:47 .netscape6/ drwxr-xr-x Chapter 5 jharris jharris 1024 Jan 13 02:49 .openoffice/ drwx------ Chapter 2 jharris jharris 512 Jan 14 01:12 .qt/ -rwx------ Chapter 1 jharris jharris 3729 Jan 10 23:21 .screenrc* drwx------ Chapter 2 jharris jharris 512 Jan 11 05:01 .ssh/ -rwx------ Chapter 1 jharris jharris 65 Jan 13 02:48 .sversionrc* drwx------ Chapter 3 jharris jharris 512 Jan 11 04:48 .thumbnails/ drwx------ Chapter 2 jharris jharris 512 Jan 10 23:21 tmp/
This is an example of how you can often combine options after a single hyphen to manipulate your results in more than one way.
Moving to different directories
Now that you know where you are and what's around you, you need to know how to get somewhere. Let's go someplace interesting—the .kde directory of your home directory. The .kde directory holds all the configuration files that control how KDE is set up. To move to a different directory, you use the cd (change directory) command, followed by the path to the directory. (A path is like a set of directions starting with where you are and ending with where you want to go.) So to change to the .kdedirectory, use this command:
[jharris@localhost jharris]$ cd .kde/share/config [jharris@localhost config]$
Notice that the prompt now shows the config directory instead of jharris. If you now do an ls command in this directory you will see dozens of configuration files for KDE.
To change back to your home directory, just run cd by itself. Its default action is to take you back to your home directory. If you want to go back only one directory, cd provides a special argument just for that, as shown below (the use of pwd is just to show where you are):
[jharris@localhost config]$ pwd /home/jharris/.kde/share/config [jharris@localhost config]$ cd ../ [jharris@localhost share]$ pwd /home/jharris/.kde/share
As you can see, the ../ argument moved you back exactly one directory. You can repeat this option twice to go back two directories (cd ../../), three times to go back three directories, and so on. You can also use ../ when you want to go back one directory and go forward into another one, like this:
[jharris@localhost share]$ cd config [jharris@localhost config]$ cd ../applnk-mdk-simplified [jharris@localhost applnk]$ pwd /home/jharris/.kde/share/applnk-mdk-simplified
As you can see, you are now in /home/jharris/.kde/share/applnk-mdk-simplified. If you had made customizations to your K Menu (which you can't do with Move), this is where the configuration files would be. In most versions of Linux, you would find these custom menu files in .kde/share/applnk/.
Controlling Files and Directories
Now that you know how to move around on the command line at will, you're ready to start managing your files and directories. There are several commands that you need to know to create, delete, and move files and directories.
Remember, if you get lost while you're on the command line, just type cd to go back to your home directory.
One thing you'll often need to do is create new directories to store your files. Linux provides the command mkdir (make directory) to create new directories. Here, you'll use this command to create a test directory, so that you can then practice your commands without messing anything else up. Change to your home directory (cd) and then run the following commands:
[jharris@localhost jharris]$ mkdir testing [jharris@localhost jharris]$ ls Desktop/ testing/ tmp/ [jharris@localhost jharris]$ cd testing [jharris@localhost testing]$ pwd /home/jharris/testing
These commands create a directory called testing, show you that it was created, take you into it, and then show you that you are in it. (Of course, you don't have to go through all this every time you create a directory; you're just practicing what you've learned in this chapter.)
You can delete directories with the rmdir (remove directory) command. To practice this, back out of the current directory and delete the testing directory:
[jharris@localhost testing]$ cd ../ [jharris@localhost jharris]$ rmdir testing [jharris@localhost jharris]$ ls Desktop/ tmp/
The testing directory is now gone, as you can confirm by running ls. Now, re-create the testing directory and cd to it, because you need a place to practice a few more commands.
Copying files and directories
To practice file management, first you need some files to work with. Since you've already visited the .kde/share/config directory, you know there are a lot of files in there you can use. First, though, you need to copy (cp) them to your test directory with the following command:
[jharris@localhost testing]$ cp ~/.kde/share/config/* ~/testing/ cp: omitting directory `/home/jharris/.kde/config/kresources'
This command copies all the files in the .kde/share/config directory and places them in your testing directory. The tildes (~) in the command are just a shorthand way to specify your home directory, and the asterisk (*) is known as a wildcard, and it tells the program to select everything in the config directory. Now, if you run an ls in the testing directory, you will see that all of the files have copied over.
You probably also noticed that the command reported that it was omitting the directory kresources from the copy (if the directories colors and session exist, they are also ommitted). As you can tell from the path in the error, this directory is actually inside the config directory. The regular cp command doesn't copy directories, just files. To copy this directory along with the files, you must use the -R option to tell the cp command to descend into directories and copy the files found there:
[jharris@localhost testing]$ cp -R ~/.kde/share/config/* ~/testing/ cp: overwrite `/home/jharris/testing/dummy'?
This time, instead of just copying the files, the cp command raises a question: is it okay to copy over the file called dummy that is already in the testing directory? This occurs because you already copied dummy to the testing directory with your first copy command, and now you are trying to do it again. You could answer the question by pressing y and then Enter, but there are a lot of files, and you'll have to provide an answer for each one. So in this case it's better to cancel the copy, and then rerun it to automatically answer yes to all questions. To cancel the copy, use the standard keyboard command to "break" a process:
cp: overwrite `/home/jharris/testing/dummy'? Ctrl-C [jharris@localhost testing]$
Ctrl-C can be used to cancel pretty much any command-line process. Now, to tell the copy command to ignore any filename conflicts, use the -f (force) option:
[jharris@localhost testing]$ cp -Rf ~/.kde/share/config/* ~/testing/
You will see no indication that the copy happened; you are simply returned to the prompt when it finishes. You can run an ls command to confirm that the files and directories were copied.
Moving and renaming files and directories
It's very easy to move files from one directory to another in Linux using the mv (move) command. The following command, run from the testing directory, moves the kmailrc file from the testing directory into your home directory:
[jharris@localhost testing]$ mv kmailrc ~ [jharris@localhost testing]$ ls ~ Desktop/ kmailrc testing/ tmp/
It's that simple to use the mv command—just specify the file or directory you want to move and the location you want to move it to. The mv command does not require the -R option to move directories or their contents.
You can also use mv to rename files and directories. This may seem a little odd at first, but from the standpoint of the way an operating system handles files on a disk it makes perfect sense. Here is an example of how to use the command in this way (after creating a work directory for your moves):
[jharris@localhost testing]$ mkdir moved [jharris@localhost testing]$ mv konquerorrc moved/ [jharris@localhost testing]$ ls moved/ konqrc
This command moves the configuration file for konquerorrc to the moved directory and renames it to konqrc. As you can see, it is very similar to moving a file, except that the second path ends with a new filename instead of ending with a directory. If that file already exists, then this command replaces it with the one you are moving.
Here's how to use mv to rename a directory:
[jharris@localhost testing]$ mv moved/ renamed
This is just like renaming a file, except that you are ending with a directory name that doesn't already exist (in this case, renamed). If the directory had already existed, you would have ended up moving the first directory into the second one.
If you keep up with your file management, you'll often need to delete files. The command to do this is rm (remove). It is used just like rmdir is used to delete directories. For example:
jharris@localhost testing$: rm renamed/konqrc rm: remove regular file `renamed/konqrc'? y
One you type y and press Enter, this command removes the konqrc file you created a few steps ago. You can specify multiple files by placing a space between them. Linux requires you to confirm that you want to remove the file for your own protection—unlike using Konqueror to move a file to the Trash, once you remove a file from the command line it is gone for good. Like most things in Linux, this is a configurable option, but it's beyond the scope of this chapter to tell you how to change this setting.
Linux Helps Those Who Help Themselves
So now you know some commands that will help you manage your files. As you've probably noticed, many commands, such as ls and cp, have multiple options. As you get used to using Linux, you'll quickly memorize the most useful options to common commands. But sometimes you'll forget exactly how to use an option, or you may need to use an obscure option. Linux kindly provides self-help files that tell you exactly how to use all the options of these commands. These help files are called man (manual) pages.
To bring up the man page for a command, just run man, followed by the name of the command, like this:
[jharris@localhost jharris]$ man ls
To move around in a man page, use the up and down arrows to scroll a line at a time, and the Page Up and Page Down buttons to scroll a page at a time. Press Q to quit the program.
Help files in Linux follow a standard format. Once you learn it, you can usually understand most man pages. First off is the NAME section:
NAME ls - list directory contents
This section simply lists the various names by which this command is called. In this example, you can see that it is just called ls.
Next is the SYNOPSIS section:
SYNOPSIS ls [OPTION]... [FILE]...
The SYNOPSIS listing tells you how you should format the command. For example, ls may be followed by one or more options and by a filename. Because [OPTION] and [FILE] are in brackets, it means that they are not required, and that the ls command will do something even if no options are specified. If they were not in brackets, it would mean they were required in order for the command to work. (Look at the man page for cp for an example; SOURCE and DEST are required elements to use the cp command.)
Next up is the DESCRIPTION section. This is simply a description of the command, frequently accompanied by examples of it in actual use. These descriptions are sometimes very clear and simple to follow, and other times are extremely complex with a lot of technical jargon. The DESCRIPTION also lists the alphabetic and numeric options that can be passed to the command. You'll want to read this section carefully, because it's where you learn to manipulate the command to make it even more useful.
Here's a neat trick. Open Konqueror and type man:/ls in the URL box. Konqueror will find the man page and display it in an easy-to-read format. This is a convenient way to print out man page documentation. If you just type man:/, you'll get a listing of all the man pages divided into sections.
Useful Navigation Tips
So far in this chapter, I've covered just the basic commands and some simple command-line interaction. This section presents a few easy-to-understand tips to help you get around the command line.
The command-line interface on Move is configured to remember everything you type in. To bring up the command-line history, just press the up arrow on your keyboard. This shows you the most recent command you typed in. Each additional press of the up arrow shows you the next oldest command in your history, and each press of the down arrow displays the next most recent command. The bash history is usually configured to store the 500 most recently used commands.
One obvious use of this history is to correct a command that you typed incorrectly. For example, suppose you wanted to run the date command, which displays the current date and time on the system. But instead of typing date, you typed dat instead:
[jharris@localhost jharris]$ dat bash: dat: command not found
As you can see, the dat command doesn't exist (or it can't be found). Now, press the up arrow to bring up the command you just typed and add the missing e:
[jharris@localhost jharris]$ date Thu Dec 9 18:29:26 EDT 2004
Another useful feature in Linux is called command-line completion. When you type the first few letters of a command and then press Tab, the computer attempts to display any single command that matches what you type. Try this out by typing dat at the command prompt and then pressing Tab. Linux completes the command for you by entering the final e. Of course, the computer didn't actually "guess" what command you want to enter—it simply compares what you typed against all the commands it knows about. Then, by process of elimination, it showed you the only command that starts with the letters you typed; there simply are no other commands that start with "dat".
You can also use tab completion to find commands you don't quite remember. For example, let's say you want to delete a directory. You may remember that the rm command removes files, but you can't remember how to remove directories. Try using Tab completion to find out which commands are available:
[jharris@localhost jharris]$ rm <press Tab> rm rmdir rmiregistry rmmod-24 rmmod.old rmt rman rmid rmmod rmmod-25 rmold
Now you'll see all the commands that start with rm, and can probably figure out that the one you're looking for is rmdir.
Move is configured to require just a single Tab to display available options, but most command lines require you to press Tab twice to get the same results.
You can use tab completion on more than just commands; it can be used to complete filenames and paths as well. For example, to change from your home directory to the testing/renamed directory you created earlier, type cd test → Tab → ren → Tab → Enter. Just as with command completion, if there is more than one match for what you typed, the system may beep and display the matches so far, and you'll have to enter more information before Tab completion can work.
There are many more command-line editing tricks that can come in handy. But be warned—once you start using these shortcuts, you may find it impossible to function without them. Table 11-1 summarizes some of the most useful tricks.
Table 11-1. Useful editing keystrokes
|Up arrow||Move back one entry in the history list.|
|Down arrow||Move forward one entry in the history list.|
|Left arrow||Move back one character.|
|Right arrow||Move forward one character.|
|Backspace||Delete previous character.|
|Tab||Attempt to complete the current filename command.|
|Alt-B||Move back one word.|
|Alt-D||Delete current word.|
|Alt-F||Move forward one word.|
|Ctrl-A||Move to beginning of line.|
|Ctrl-D||Delete current character.|
|Ctrl-E||Move to end of line.|
|Ctrl-K||Delete to end of line.|
|Ctrl-L||Clear the screen, placing the current line at the top of the screen.|
|Ctrl-U||Delete from beginning of line.|
|Ctrl-Y||Retrieve last item deleted.|
|Esc.||Insert last word from the previous command. (Esc is pressed before the dot, not at the same time.)|
|Esc ?||List the possible completions. (Esc is pressed before the question mark, not at the same time.)|
Finding and Stopping Runaway Programs
At any moment, there's a lot going on with your computer. Programs and processes are controlling hard drive access, determining what you see on the screen, managing what is written to event logs, and watching for input from you. If you've ever looked at the Task Manager on Windows 2000 or XP (press Ctrl-Alt-Del and click Task Manager to bring it up), you've seen a lot of programs running that you probably couldn't identify.
Sometimes programs crash or become unresponsive. I wish I could say that Linux was immune to problems like this, but it isn't. When a problem occurs, the affected program process may get hung up, and hang around, using up a lot of your computer's time and memory and slowing other programs down. You need to know how to find runaway program processes like this and force them to stop.
There are several ways to view running processes in Linux. One of the most popular is the top program. Here is the output from my laptop:
dbrick@rivendell $: top top - 19:46:56 up 1 day, Chapter 8:18, Chapter 1 user, load average: 0.02, 0.06, 0.05 Tasks: 48 total, Chapter 1 running, 47 sleeping, 0 stopped, 0 zombie Cpu(s): Chapter 2.5% user, Chapter 1.1% system, 0.9% nice, 95.5% idle Mem: 514756k total, 491716k used, 23040k free, 101568k buffers Swap: 521632k total, 196k used, 521436k free, 178524k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2013 root 14 0 70524 24m 2296 S 2.0 4.8 14:21.21 X 25020 dbrick 11 0 924 924 736 R 2.0 0.2 0:00.03 top 154 root 9 0 924 900 628 S 0.0 0.2 0:00.04 devfsd 1296 root 9 0 680 672 532 S 0.0 0.1 0:00.00 syslog-ng 1350 root 9 0 528 524 476 S 0.0 0.1 0:00.00 apmd 1641 root 9 0 652 528 488 S 0.0 0.1 0:00.00 cardmgr 1670 root 9 0 456 444 408 S 0.0 0.1 0:00.00 dhcpcd 1787 root 8 0 1188 1164 1024 S 0.0 0.2 0:00.00 sshd 1841 root 8 0 652 652 568 S 0.0 0.1 0:00.03 cron 21215 dbrick 9 0 876 872 776 S 0.0 0.2 0:00.01 kde-3.3.0 21221 dbrick 9 0 984 980 856 S 0.0 0.2 0:00.03 startkde
You may be thinking, "What is all this stuff?" Well, to start, the output is divided into two sections. The first five lines contain a lot of condensed information. You may be able to guess what some of it means—check the man page for top to see if you are right.
For now, concern yourself with the second portion of the top output, which is a listing of currently running processes. By default, the processes are sorted by how much processing power they use. In this case, the X program that controls the drawing of images on the screen is using 2 percent of my processing power and 4.8 percent of my memory.
When you have a misbehaving program, the information you need is in the first column, PID, which stands for process ID. Every running program on Linux has one or more processes, and every process has a PID; think of it as a name for your process that your computer can understand.
You can use the PID from top to identify and stop runaway programs. For instance, if you are running KWrite and it has stopped responding, you can't close it by clicking the close button on the title bar or by using a menu command or keyboard shortcut. You'll have to close it from the command line instead. To do this, you first have to identify KWrite's current PID. A runaway program is usually consuming a lot of processing power, so when you run top, the troublesome program should be right at the top. Once you know the PID (let's pretend it's 1000), you can stop the process by issuing a kill command. (Don't actually do this unless you have a real runaway process.)
[jharris@localhost jharris]$ kill 1000
If the command is successful, the program window will close. However, it's possible for a program to be so badly hung up that it doesn't respond to this command. That means you need to pass kill the -9 option, which basically means to kill with a vengeance.
Use the man command to read more about top and kill. You can quit the top program by pressing Q.
From the KDE desktop, Press Ctrl-Esc for a graphical way to view processes. This window will take several seconds to come up. It lists all processes by default, but you can use the drop-down list at the bottom to view only your processes (User Processes). To stop a process, highlight it in the list and click the Kill button.
Zipping and Unzipping Files
One of Windows' most popular utilities is the compression program WinZip. This handy program lets you group a bunch of files together and compress them into one smaller file. This smaller file size means little when you consider the large hard drive capacities most computers have today, but it can be a real blessing when you want to send a lot of files, like all the pictures of your wedding, to friends and family via email. Also, you often need to unzip programs you download from the Internet before you can install them. This is true for both Windows and Linux.
Linux has long had the ability to zip and unzip files. In fact, it has several programs, both command-line and GUI, to perform these tasks. Some of them use the same compression algorithm used by WinZip, while others use a free variant. I'll explain how to use both types of programs.
Let's pretend that you've received an email from your sister, with a zipped attachment containing pictures of your nephew's first birthday party. Though you could open this attachment and view the files using Konqueror (covered in Chapter 3), let's use the command line instead. Save the file attachment to your home directory and open a Konsole window. The command to open a zipped file archive is simply unzip :
[jharris@localhost jharris]$ unzip nephew.zip Archive: nephew.zip extracting: nephew1.jpg extracting: nephew2.jpg extracting: nephew3.gif
As you can see, the unzip program extracted three files from the zip archive and placed them in your current directory. You can run ls to confirm their location. To put the files into a new directory called birthday instead of in the current directory, modify the command by adding the -d option (destination) with an argument naming the directory you want to create:
[jharris@localhost jharris]$ unzip nephew.zip -d birthday Archive: nephew.zip extracting: nephew1.jpg extracting: nephew2.jpg extracting: nephew3.gif
Again, run ls to confirm that a directory called birthday has been created and that copies of the files are now in that directory.
If the situation were reversed and you wanted to send several photos to your sister, you would need to create a zip archive yourself. Let's pretend that you are in your home directory and want to send your sister only the .jpg images, not the .gif image. Here's the command you would use to do this:
[jharris@localhost jharris]$ zip nephew *.jpg adding: nephew1.jpg (stored 23%) adding: nephew2.jpg (stored 50%)
Let's walk through this command. First, type in the command zip. (Pretty intuitive, right?) Next, type in the name of the zip file you want to create. (The zip command automatically adds the .zip file extension when the file is created.) Finally, type in the files you want to put in the zip. In this case, using the * wildcard, you specified that you want to zip all the files in the current directory that end with .jpg. (Even if you had a directory with 50 .jpg images and 50 .gif images mixed up together, this simple command could accomplish the task in seconds. Imagine how long it would take with a GUI tool—you would have to select each JPEG file by hand!) Read the man pages for zip and unzip to find out about more options, like how to add a file to a zip archive you've already created.
As you use Linux, you'll frequently come across files that have been compressed using the gzip program, which is like an open source variant of WinZip. When sending files to Windows users, you should probably zip your files. But when sharing with Linux users, it is polite to gzip them, as gzip works better with standard Linux tools.
Files on the Internet that have been gzipped include program installers, source code, images, text files, music files, and anything else that Linux users want to share with each other. These files usually end with the file extension .tar.gz , which hints at one of the differences between a gzipped and a zipped file.
Zip programs group and compress files at the same time. Gzip programs simply compress files—they do not group them together. To group your files, you need to use an additional program called tar . This results in what is commonly referred to as a tarball of your files (cool name, huh?), which you can then compress with the gzip program.
For example, say you have downloaded a gzipped file called crystal.tar.gz from a web site. You need to first unzip this file using the gzip uncompressing program gunzip, and then unpackage the tarball using the tar program. Here's how you would do that:
[jharris@localhost jharris]$ gunzip crystal.tar.gz [jharris@localhost jharris]$ tar -xvf crystal.tar README.txt crystal1.tif crystal2.txt
The gunzip command in basic use is pretty simple. Just type in the command, and then specify the file you want to uncompress. Besides uncompressing the gzipped file, it also removes the gzipped file, leaving behind just a .tar version. You can confirm this by running ls before you use tar. The tar command requires a few options to be passed to it. The -x option means expand; the -v option means show the output (verbose); and the -f option means perform this action on the file specified. And since gunzip and tar are so often combined, tar provides a one-command way to both uncompress and untar a file by using the -z option:
bash-2.05b$ tar -xvzf crystal.tar.gz README.txt crystal1.tif crystal2.txt
To reverse the situation, you can use tar and gzip to group these files together and compress them. This is how to do it with two separate commands:
bash$: tar -cvf crystal.tar README.txt crystal1.tif crystal2.txt README.txt crystal1.tif crystal2.txt bash$: gzip crystal.tar
The -c option for tar creates a new tarball with the filename you specify. Unlike with the zip command, you need to add the .tar extension yourself. The tarball name is followed by arguments for which files you want to include. Tab completion can be a handy way to add a lot of individual files. Finally, you compress the file with the gzip command, which takes the name of the .tar file as its argument.
Once again, the -z command can do double duty by creating the gzip file automatically when the tar file is made, so you only need to use one command:
bash$: tar -cvzf crystal.tar.gz README.txt crystal1.tif crystal2.txt README.txt crystal1.tif crystal2.txt
And again, unlike zip, if you want the final filename to end with .tar.gz, you need to specify that in the command. To find out more about gzip, gunzip, and tar, check out their man pages.
This chapter just scratches the surface of the Linux command line. To continue our mechanics metaphor, it's the equivalent of high school shop class, not ASE certified mechanics certification. I've focused on file and directory management, not because that's all you can do on the command line (far from it!), but because it lays a firm foundation from which to build your knowledge. Although it's possible to use Linux without using the command line, you'd really be missing out on part of what makes Linux such a powerful operating system. To explore the command line further, pick up the Linux Pocket Guide or Linux in a Nutshell, both from O'Reilly.