Article

Fire Up your own Linux Server

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Next

Chapter 3. The Command Line

So far, our interactions with Linux have been through a pretty desktop environment, and it's been reasonably easy for experienced Windows users to get their heads around the system. Things haven't always been this way, though: the roots of the Linux user interface are firmly stuck in its command line.

What is the Command Line?

Unlike Windows, the GUI is completely optional in Linux. If you're feeling particularly competent, or you have certain requirements (or very old hardware), you can run your Linux machine with no graphics at all. As Linux has evolved, a variety of GUIs have been built on top of it, but the command line remains the administrator's best friend: a quick, easy, powerful way to perform actions that aren't always easily available from the desktop.

Figure 3.1. Accessing the GNOME Terminal.
1505_3.1

Throughout this book, we'll use the GNOME Terminal application to gain access to the command line. You can access this application from Applications > System Tools > Terminal, as shown in Figure 3.1. The terminal itself is depicted in Figure 3.2.

Tweaking the Terminal
The GNOME desktop environment is fully customizable—and that includes the appearance of the terminal. You can determine the font that's used, change the foreground and background colors of the window—you can even set the level of transparency of the window itself, so that it's possible to see the desktop image behind the window! Within the terminal, you can find these settings in Edit > Current Profile...

Figure 3.2. The GNOME Terminal application.
1505_3.2

Let's look at a real-life example that uses the command line to meet a specific need. Once you understand the components of the command (and the complexity of the graphical alternative), you'll begin to understand the practical utility of the terminal and the command line.

[kermit@swinetrek ~]$ find /var/backups/* -ctime +5 -exec rm {} \;

I use this command regularly to remove from my system backups that are more than five days old. The command uses the find tool to search for files in the /var/backups directory that are more than five days old. For each file that the find tool locates, it runs the rm command: rm is the standard "remove file" command. Running this command regularly helps to save system storage space by deleting backups that, after five days, have become unnecessary. Of course, this command should be run in conjunction with an automated process that creates these backups every day or so.

Let's compare the use of this command to the process of utilizing a graphical tool to accomplish the same task.

  1. Reach from the keyboard for the mouse.

  2. Double-click on the Computer icon on the desktop.

  3. Double-click on Filesystem to get to the root directory.

  4. Double-click the var folder to open it.

  5. Double-click the backups folder.

  6. Ensure that the resulting window displays the creation time of the files by selecting View > View as List.

  7. Holding down Ctrl, select the files with a creation date older than five days.

  8. Right-click and select Move to Trash from the context menu.

  9. To really delete the file, return to the desktop and empty the trash by right-clicking on the Trash icon and selecting Empty Trash.

You don't need to understand the intricacies of the Linux filesystem to see the advantage of the command line. The graphical approach involves at least nine—likely many more—mouse clicks. Rather than viewing the process of finding and deleting these files as single operation, you're forced to break the process down into its individual components. Add to that the inefficiency of using the mouse to locate, point at, and click targets on the screen, as compared to the swift simplicity of entering commands via the keyboard, and you start to understand how the command line can be a valuable and efficient tool for system administration.

This, however, doesn't even address the real beauty and power of the command line. You don't believe that I actually run the rm command three times a week, do you? That would also be terribly inefficient: I'd be relying on my own porous memory to make sure that the backups were removed! In fact, most of the commands you'll use in Linux are fully scriptable, with minimal modification. In reality, I've asked my system, via the cron utility (which we'll cover in depth in Chapter 4, System Administration) to execute the command three times each week. This is much like the Scheduled Tasks feature in Windows, but it's more powerful: it can do just about anything. As you can see, an understanding of Linux commands and command line concepts is essential, even if you only open a terminal window occasionally.

Using the Command Line

When you use the Linux command line, you're opening what can be thought of as an alternative window to the operating system—an alternative window through which you have access to much more powerful, lower level operating system functions. Graphical tools don't provide that power. A graphical environment is merely a high-level abstraction of an operating system environment, and the icons and menus to which we're all so accustomed provide only limited access into that environment. Those icons and menus represent operating system functions, but they're not the actual functions. In other words, graphical interfaces provide a limited, additional layer between operator and operating system, in which a restricted range of functions are represented by pretty pictures. To administer your Web server with maximum efficiency, it's useful to be able to work at the same level as your system.

High and Mighty, or Down and Dirty
When we talk about "high level" and "low level" functions, we imply a hierarchy of functionality. Functions of the lowest level communicate with the kernel directly, and therefore have great power and flexibility; higher level tools do more work themselves, offering more functionality and increased safety. Compare, for example, the low level rm command with the higher level Trash functionality in Nautilus. rm simply deletes a file: it's quick and easy, but there's no easy or guaranteed way to recover that file. Nautilus doesn't provide you with such functionality. It insists that you move files to the Trash, effectively marking the file for potential deletion. The deletion itself doesn't occur until you go to the desktop and empty the trash.

Logging in as root

We've already talked about logging into Linux as root. To do so from the GNOME terminal, you'll enter the su (switch user) command.

[kermit@swinetrek ~]$ su        
Password:

You'll be prompted for the root password. Once you've entered it, you'll gain full access to the system: computing omnipotence! Use it wisely. Although there are certain things that can only be done as root, including many of your system administration tasks, it's important to observe good Linux hygiene: don't remain logged in as root any longer than you need to. Every Linux administrator has a horror story of a time when, logged in as root, he or she mistyped a command and deleted everything on the machine (or some similar calamity). You want to avoid such disasters. Be careful, and don't stay logged in as root if you don't need to be. To switch back to your standard user account, use the exit command.

Some Practical Examples

Let's look at a few examples of the command line in action.

Orienting yourself with the pwd Command

In Nautilus, we can have multiple folder windows open at once. However, when using the command line, we can only work within one directory at a time. That active directory is called the working directory. To find out which directory you're currently in, use the pwd (print working directory) command.

[kermit@swinetrek ~]$ pwd        
/home/kermit

Home, Sweet Home
Your home directory, /home/username, is often referred to as ~. Did you notice the ~ in the command prompt? It indicates that you're currently in your home directory. As we begin to navigate the filesystem, we'll see this command prompt change to reflect the name of the directory in which we're working. Thus, we won't have to constantly enter pwd to find out where we are.

Listing Files with the ls Command

The ls (which stands for list) command, used by itself, lists the contents of the working directory.

[kermit@swinetrek ~]$ ls        
Desktop  Hello World.txt

You can retrieve a list of the files in another directory by adding the directory name to the command line. For example, ls / will return a listing of everything in the root directory.

[kermit@swinetrek ~]$ ls /        
bin   dev  home  lost+found misc  net  proc  sbin    srv  tmp  var        
boot  etc  lib   media      mnt   opt  root  selinux sys  usr

Using ls by itself will show you the names of the files contained in a folder, but little more. To view all of the files' details, we can add the -l option to the command, which returns a longer listing.

[kermit@swinetrek ~]$ ls -l        
total 16        
drwxr-xr-x  2 kermit kermit 4096 Sep  5 14:21 Desktop        
-rw-rw-r--  1 kermit kermit   13 Sep  8 07:30 Hello World.txt

There's plenty of information here.

  • The first column presents the permissions for each file in the format we discussed in Chapter 2, Day-to-day Usage. In summary, the first character tells us whether the file is a directory; the next nine characters show whether or not read, write, and execute permissions have been granted to the owner, group, and others.
  • The next column is really only useful for directories; it reflects the number of files inside a directory. For files, this number will be 1.
  • The next two columns identify the owner and the group assigned to the file. In this case, they're both kermit.
  • The size of the file in bytes is displayed next. Here, we see that the file Hello World.txt is 13 bytes.
  • Next, we see the date and time at which the file was last modified. In the example above, the Desktop directory was last modified on the September 5 at 2:21 p.m.
  • Finally, we're given the name of the file.

ls -l is so useful that Fedora Core includes a built-in shortcut to it: ll.

Your home directory will contain a number of hidden files. In Linux, we can hide a file by starting its filename with a period: if we changed the name of Hello World.txt to .Hello World.txt, it would become hidden. It's in these hidden files that programs store user-specific configuration information. To see hidden files, use the all option (-a), which can be use in conjunction with the -l option, or as an option to ll.

[kermit@swinetrek ~]$ ll -a        
total 212        
drwx------  13 kermit kermit 4096 Sep  8 09:16 .        
drwxr-xr-x   5 root   root   4096 Sep  6 13:48 ..        
-rw-------   1 kermit kermit    5 Sep  8 06:21 .bash_history        
-rw-r--r--   1 kermit kermit   24 May 10 10:15 .bash_logout        
-rw-r--r--   1 kermit kermit  191 May 10 10:15 .bash_profile        
-rw-r--r--   1 kermit kermit  124 May 10 10:15 .bashrc        
drwxr-xr-x   2 kermit kermit 4096 Sep  5 14:21 Desktop        
-rw-------   1 kermit kermit   26 Sep  6 14:20 .dmrc        
drwxr-x---   2 kermit kermit 4096 Sep  6 14:21 .eggcups        
-rw-r--r--   1 kermit kermit  438 May 18 01:23 .emacs        
-rw-------   1 kermit kermit   16 Sep  6 14:28 .esd_auth        
drwx------   4 kermit kermit 4096 Sep  6 14:31 .gconf        
drwx------   2 kermit kermit 4096 Sep  6 14:31 .gconfd        
drwxrwxr-x   3 kermit kermit 4096 Sep  6 14:21 .gnome        
drwx------   7 kermit kermit 4096 Sep  6 14:31 .gnome2        
drwx------   2 kermit kermit 4096 Sep  6 14:20 .gnome2_private        
drwxr-xr-x   2 kermit kermit 4096 Sep  6 14:21 .gstreamer-0.8        
-rw-r--r--   1 kermit kermit  120 May 22 15:18 .gtkrc        
-rw-rw-r--   1 kermit kermit  134 Sep  6 14:20 .gtkrc-1.2-gnome2        
-rw-rw-r--   1 kermit kermit   13 Sep  8 07:30 Hello World.txt        
-rw-------   1 kermit kermit    0 Sep  6 14:31 .ICEauthority        
drwx------   3 kermit kermit 4096 Sep  6 14:21 .metacity        
drwx------   2 kermit kermit 4096 Sep  6 14:22 .mozilla        
drwxr-xr-x   3 kermit kermit 4096 Sep  6 14:21 .nautilus        
-rw-------   1 kermit kermit   50 Sep  6 14:26 .recently-used        
-rw-------   1 kermit kermit  497 Sep  6 14:21 .rhn-applet.conf        
-rw-------   1 kermit kermit   66 Sep  8 09:16 .xauth3R8EvP        
-rw-r--r--   1 kermit kermit  658 Jan 16  2005 .zshrc

At the top of this file listing appear two directories, named . and ... These are shortcuts to the current directory and the parent directory, respectively. We'll look at these in the next section.

Moving around the Filesystem with the cd Command

The cd command stands for change directory. It changes the current working directory to the one specified immediately after the command.

[kermit@swinetrek ~]$ cd /etc/httpd/        
[kermit@swinetrek httpd]$

Used by itself, the cd command returns you to your home directory.

[kermit@swinetrek httpd]$ cd        
[kermit@swinetrek ~]$

The commands cd /home/kermit and cd ~ do the same thing. You can move to the working directory's parent directory using the cd .. command.

[kermit@swinetrek ~]$ cd ..        
[kermit@swinetrek home]$

Printing with the echo and cat Commands

The echo command simply sends output to the screen.

[kermit@swinetrek ~]$ echo "Hello, World\!"        
Hello, World!

Escaping Special Characters
Certain characters are reserved for special use on the command line, such as !. However, you'll often want to use these characters in command options or in filenames. To use these characters, you must escape them, that is, prefix them with a backslash (\) character. If you use spaces in filenames, you'll need to escape the spaces when dealing with those filenames on the command line: our file Hello World.txt needs to be accessed as Hello\ World.txt.

The cat command opens a file and writes its contents to the screen.

[kermit@swinetrek ~]$ cat Hello\ World.txt        
Hello, World!

If you liked this article, share the love:
Print-Friendly Version Suggest an Article