Introducing the Shell


A shell is any Linux program that allows you to give instructions in some way and that then carries out those instructions using the underlying facilities of the Linux operating system and your computer system. The instructions in question can be anything you might want to do, such as printing a file, starting a program, or opening a network connection. When veteran Linux users talk about using "the shell" in everyday conversation, however, they are usually referring to a command interpreter, a special kind of program that understands words and alphanumeric charactersin short, language, rather than actions like mouse clicksand that then passes these language-based instructions to Linux.

Understanding the Shell's Role as Command Interpreter

The term shell is a metaphor; you can think of the Linux kernel, which is the core of Linux, as an organism that lives beneath or inside the shell. The Linux kernel is a very technical program that is not user friendly; it manages and coordinates everything that is going on inside your Fedora Core 4 computer system as you use it.

In almost any normal circumstance, talking directly to the Linux kernel is the hardest way to accomplish a task, because the Linux kernel is designed to communicate with electronic equipment and with other computers, rather than with humans. At the same time, you must communicate with the Linux kernel somehow in order to be able to give Linux instructions. One way to do this is to point and click on your desktop, but this method is somewhat constrained; the realm of possibilities is limited by the space for icons, menus, and other items on your desktop, and only a certain limited amount of information can be conveyed by a mouse click.

As a text-based command interpreter, the shell accepts a wide lexicon of text commands that are designed to make sense to humans and that can be combined with each other in various ways, just like words in human languages, to make new meanings. The shell's job is to interpret the meanings of these human-style instructions and then to pass them on to the Linux kernel in the technical format that the kernel can understand.

There Are Actually Several Common Shells

You can choose to use one of several command-line shells on most any Linux or Unix system; each is slightly different in terms of behavior and features, although most of the actual commands remain the same. The most common shell by far is the default shell, bash, which stands for the Bourne-Again SHell, so named because it is a re-creation (with many new features) of the old Bourne Shell, sh, from traditional Unix systems.

From now on in this book, when we talk about the shell, we mean the Bourne-Again Shell (or bash) that Linux uses by default.


Launching a Terminal Window

The easiest way to access the shell from your Linux desktop is to start a terminal emulator program. A terminal emulator is an application that provides you and the shell with a text-oriented window in which to communicate. You can type commands to the shell, and the shell can respond by displaying informative text about what it is doing and what Linux is doing.

To start a terminal window in Fedora Core 4, choose Applications, System Tools, Terminal. The terminal application appears, as shown in Figure 6.1.

Figure 6.1. The terminal application helps you to interact with the Linux command line via the shell.


The last line of text in a waiting terminal window is always a command prompt. This command prompt signals you that the shell is ready to accept your commands and translate them into tasks for the Linux kernel to perform. When you use the shell, you type whatever command(s) and option(s) you want to supply at the command prompt and then press Enter, which signals the shell to process and carry out the instructions you've just provided. When the command prompt reappears, your instructions have been carried out.

Learning Fundamentals of Shell Use

The first thing to notice about the Linux shell is that the command prompt contains useful information about your computing environment. Right now, in your terminal window, you probably see a command prompt something like this:

 [you@workstation20 ~]$ 

The first part of the prompt, you@workstation20, varies depending on your username and the network name of the machine you are using; its format is user@machine. So, for example, if you are logged in as kensmith, and you are using a computer called netstation52, the first part of your prompt would read

 kensmith@netstation52 

The second part of the prompt displays the last part of the current paththe directory or folder you and the shell are currently working in. By default, when you start a new shell, this is your home directory. For the user kensmith, this would be /home/kensmith; for the user you, it's /home/you. In either case, the final directory in the path is the default current working directory of the shell.

A special shorthand, the tilde (~) character is used at the Linux command line to represent your home directory. When the shell shows you a tilde, it is referring to your home directory; when you send a tilde to the shell as part of a command, it assumes that you are talking about your home directory.

What Is a Home Directory Again?

If you've forgotten what the term home directory means, feel free to review the section called "Understanding the Linux File System" in Chapter 5.


The default prompt for the user you, which incorporates the username, the machine name, and the current working directory, is therefore

 [you@workstation20 ~]$ 

If you're not sure why all these details are useful at the moment, don't worry; we're going to get our hands dirty soon, and you'll begin to see in a more direct way how the command prompt relates to what you are doing with the shell.

Talking to the Shell

As you work with the shell, you will soon notice that the text of every set of instructions you give it follows the same basic pattern. The following description of the contents of a shell command might seem obtuse at first, but hang in there. As you gain experience with the shell, these details will become less confusing to you.

In general, you type the following when you give an instruction to the shell:

  • A one-word command chosen from the massive list of available commands on your Linux system, each of which performs some specific task. There are literally thousands of commands.

  • If needed, an option or flag, which modifies or directs the command's behavior, and is usually preceded by a dash. Each command has its own list of understood options and flags that are used to alter the behavior of the command to suit your needs.

  • If needed, arguments, which provide additional data that a command needs to function or to employ a specific option or flag.

  • If needed, additional options and flags or arguments in whatever number is necessary to suitably direct the command's behavior.

This list of elements is common in the Linux and Unix world; you will see it appear each time you read the documentation for a specific command. However, in online documentation, you usually see it written in shorthand or synopsis form, like this:

 mycommand -opt1 arg1 [-opt2 arg2 ...] 

This line of text roughly represents what you just learned: that when you type the fictional command mycommand at the shell, you should provide at least one option or flag (-opt1) that the command understands, followed by one argument (arg1). If necessary, you can enter an optional second flag (-opt2), followed by a second argument (arg2), and so on. Although this brief notation seems cryptic at first, it appears everywhere in the command-line world and soon becomes second nature to shell users.

Entering Your First Command

It's time to introduce your first command. The ls command is the command you use to display the contents of a directory in the Linux file system. It is in many ways akin to opening a directory in the file manager and looking at the icons that it contains. To see what is in your Linux system's root directory, enter the ls command now, supplying the slash (for the root directory) as an argument. The output of this command is shown in Figure 6.2.

Figure 6.2. After you enter the ls command and provide the root directory (/) as an argument, the contents of the root directory are listed.


Colors Indicate File Types

At the Fedora Core 4 command line, the output of the ls command is color-coded to help you differentiate between files, directories, and other types of data, the same way that icons help you on the desktop. Though you can't see it in the text, you can on your screendirectories appear in blue.


Here, the ls command provided you with two lines of output. Each of the names you see is a directory stored in the root directory of your Linux system. Now list the contents of the usr directory by using the same command with /usr as an argument, as shown in Figure 6.3.

Figure 6.3. A second invocation of the ls command displays the contents of /usr.


The results of the first command you typed are still visible. When you use the ls command, each argument provides the path to a directory whose contents you want to display. When referring to the specific location of a file or directory (its path) at the Linux command line, you will generally begin with the root directory (/) and proceed to provide a kind of roadmap for arriving at the desired file or directory, separating each step along the way with an additional slash.

Now list the contents of one more directory, /usr/X11R6, just for practice. The results are shown in Figure 6.4.

Figure 6.4. The results of one more invocation of the ls command, this time listing the contents of the path /usr/X11R6.


Linux Is Case Sensitive

Having trouble? If you've been faithfully trying to follow along but are getting error messages such as Command not found or No such file or directory, check to make sure that you are not using capital letters when typing these commands.

Linux, like most Unix-like operating systems, is case sensitive. This means that ls is not the same as LS to Linux; similarly, x11r6 is not the same as X11R6.


In these commands, you listed the contents of the root directory, the directory called usr inside the root directory, and the directory called X11R6 inside the usr directory. The roadmap you supplied to the ls command in each case is exactly the same kind of path you learned about in Chapter 5. If you need a refresher on the structure of the Linux file system and paths in particular, you can refer to Chapter 5 and Table 5.1 for details.

Using the Current Working Directory

Now that you've seen directories in action at the shell prompt and are already familiar with the root directory and the home directory, it's time to really navigate the Linux file system at the command line. Let's begin by creating a file. You can create an empty text file at the command line using the touch command and giving the filename you want to use as an argument. First, create a file called empty.txt:

 [you@workstation20 ~]$ touch empty.txt [you@workstation20 ~]$ 

Linux responds by creating for you an empty text file called empty.txt. To see that the file exists, use the ls command without arguments:

 [you@workstation20 ~]$ ls Desktop  anotherfile.txt  empty.txt [you@workstation20 ~]$ 

If you're wondering exactly where the new file was created in the file system tree, you're an astute reader. Each time you have used the ls command thus far, you have supplied it with an argumentthe path to the directory you wanted to list. This time, however, ls lists the contents of a directory without having been told which directory you wanted to see. So what's going on? And in what directory did you just create empty.txt?

As was previously mentioned, the shell always keeps track of something called your current working directory. At your request, any directory in the file system can be made your current working directory. Linux created empty.txt in your current working directory because you didn't specify an alternate location. The ls command displays the contents of your current working directory in the preceding example for the same reason. In most cases, if you provide an argument to a command without supplying a path, the command is executed as though you had actually supplied the path of your current working directory. Because you have just started this shell and issued only a few commands, your current working directory is still your home directory, as can be seen by the tilde character (~) in the prompt. Whenever you start a new shell, the default current working directory is your home directory.

From the previous output of ls, you can see that there are three files in this current working directory; if you have been following along with our previous chapters, your list of files might be the samethe file you created in the text editor called anotherfile.txt, a folder or directory called Desktop, and a file called empty.txt that you just created. To ask the shell at any time just what your current working directory is, use the pwd (print working directory) command:

 [you@workstation20 ~]$ pwd /home/you [you@workstation20 ~]$ 

As you can see, your current working directory is indeed your home directorythe directory in /home that belongs to your account and is intended to hold your files. To change your current working directory, you use the cd command, supplying your desired current working directory as an argument. Try changing your current working directory to the root directory now:

 [you@workstation20 ~]$ cd / [you@workstation20 /]$ 

Notice that the command prompt has changed to reflect your new current working directory. You can also use pwd to see that the change has taken effect:

 [you@workstation20 /]$ pwd / [you@workstation20 /]$ 

Now, using the ls command without arguments displays the contents of the root directory, rather than the contents of your home directory:

 [you@workstation20 /]$ ls bin   dev  home    lib    misc  opt   root  selinux  sys  usr boot  etc  initrd  media  mnt   proc  sbin  srv      tmp  var [you@workstation20 /]$ 

Try making /usr/X11R6 your current working directory, using pwd to verify the change and then using ls without arguments to list the contents of the directory:

 [you@workstation20 /]$ cd /usr/X11R6 [you@workstation20 X11R6]$ pwd /usr/X11R6 [you@workstation20 X11R6]$ ls bin  include  lib  man  share [you@workstation20 X11R6]$ 

Because your home directory is so fundamental to day-to-day use of Linux at the command line, you can always return to it quickly by simply issuing the cd command without arguments:

 [you@workstation20 X11R6]$ cd [you@workstation20 ~]$ pwd /home/you [you@workstation20 ~]$ 

Two Names, One Concept

In other texts or in Linux-oriented conversation, you might also encounter the term present working directory instead of current working directory; these terms are identical in meaning.


Manipulating Files and Directories

Now that you're back in your home directory, assume that you want to make a copy of the file you created earlier called empty.txt. You can do so with the cp command, supplying two argumentsfirst the name of the source (original) file and second the name of the destination (new) file:

 [you@workstation20 ~]$ cp empty.txt notfull.txt [you@workstation20 ~]$ ls Desktop  anotherfile.txt  empty.txt  notfull.txt [you@workstation20 ~]$ 

There are now three files in your home directory: anotherfile.txt, empty.txt, and notfull.txt, which is a copy of empty.txt. Suppose, however, that you don't want empty.txt hanging around in your home directory but instead want to put that particular file in its own directory called emptyfiles. To create a directory, use the mkdir command, supplying the name of the directory you want to create as an argument:

 [you@workstation20 `]$ mkdir emptyfiles [you@workstation20 ~]$ ls Desktop  anotherfile.txt  emptyfiles  empty.txt  notfull.txt [you@workstation20 ~]$ 

You have now created a directory called emptyfiles (which ls shows in blue to indicate that it is a directory) and can move empty.txt into it. You can move Linux files using the mv command, which accepts a space-separated list of the files you want to move as arguments and the destination directory as the last argument. After you are done, you can verify the effects of mv by using the now familiar ls command:

 [you@workstation20 ~]$ mv empty.txt emptyfiles [you@workstation20 ~]$ ls Desktop  anotherfile.txt  emptyfiles  notfull.txt [you@workstation20 ~]$ ls emptyfiles empty.txt [you@workstation20 ~]$ 

Notice that the path arguments in the commands you just entered don't begin with a slash character. These commands were given relative paths, which are discussed in the next section.

Using Relative Paths

In the series of commands you just entered, you gave ls only a directory name, emptyfiles, as an argument, rather than a full path beginning with a slash. A directory name without a roadmap back to the root directory is called a relative path, so called because it doesn't begin with the root directory (/) but instead specifies that the argument is relative to your current working directory. Linux is smart enough to notice that the path you gave to ls when you displayed the contents of emptyfiles didn't begin at the root directory; it therefore adds the path you supplied in your argument to the end of your current working directory, which always begins with a slash. In this case, /home/you (the current working directory) plus emptyfiles (the argument given to ls) causes the contents of /home/you/emptyfiles to appear on the console, saving you the trouble of entering the entire long path by hand.

To gain more practice with relative paths, try creating one more directory. This time, call it deeperfiles. Place it "deeper" within your home directory by creating it inside the directory you created earlier called emptyfiles. Remember that your current working directory is still your home directory; you can use pwd to verify this:

 [you@workstation20 ~]$ pwd /home/you [you@workstation20 ~]$ mkdir emptyfiles/deeperfiles [you@workstation20 ~]$ ls Desktop  anotherfile.txt  emptyfiles  notfull.txt [you@workstation20 ~]$ ls emptyfiles deeperfiles  empty.txt [you@workstation20 ~]$ 

Now, just for practice, move the file notfull.txt into the deeperfiles directory:

 [you@workstation20 ~]$ mv notfull.txt emptyfiles/deeperfiles [you@workstation20 ~]$ ls Desktop  anotherfile.txt  emptyfiles [you@workstation20 ~]$ ls emptyfiles deeperfiles  empty.txt [you@workstation20 ~]$ ls emptyfiles/deeperfiles notfull.txt [you@workstation20 ~]$ 

Before finishing with relative paths, you need to learn about two special relative paths. They are a single period (.) and a double period (..), and you can use them anywhere you would normally use the name of a directory.

The single period is a kind of shorthand that always refers to the current working directory. So, assuming that your current working directory is /home/you, the following three commands produce identical results:

 [you@workstation20 ~]$ ls Desktop  anotherfile.txt  emptyfiles [you@workstation20 ~]$ ls /home/you Desktop  anotherfile.txt  emptyfiles [you@workstation20 ~]$ ls . Desktop  anotherfile.txt  emptyfiles [you@workstation20 ~]$ 

The double period always refers to the directory one branch lower (closer to root) in your file system tree, relative to your current working directory. So, assuming that your current working directory is /home/you, the following two commands produce identical results:

 [you@workstation20 ~]$ ls /home you [you@workstation20 ~]$ ls .. you [you@workstation20 ~]$ 

You also can use relative paths to modify your current working directory with cd:

 [you@workstation20 ~]$ cd .. [you@workstation20 home]$ pwd /home [you@workstation20 home]$ 

Because the root directory is at the base of the file system tree and there is no "lower" directory in the Linux file system, in the root directory, the single period and double period are equivalent:

 [you@workstation20 home]$ cd .. [you@workstation20 /]$ pwd / [you@workstation20 /]$ ls bin   dev  home    lib    misc  opt   root  selinux  sys  usr boot  etc  initrd  media  mnt   proc  sbin  srv      tmp  var [you@workstation20 /]$ ls . bin   dev  home    lib    misc  opt   root  selinux  sys  usr boot  etc  initrd  media  mnt   proc  sbin  srv      tmp  var [you@workstation20 /]$ ls .. bin   dev  home    lib    misc  opt   root  selinux  sys  usr boot  etc  initrd  media  mnt   proc  sbin  srv      tmp  var [you@workstation20 /]$ 

Remember that you can quickly return home by issuing the cd command without arguments:

 [you@workstation20 /]$ cd [you@workstation20 ~]$ pwd /home/you [you@workstation20 ~]$ 

Parent and Child Directories

The double period (..) is often referred to as the parent directory. In the Linux file system, a file's parent directory is the directory that contains it. For example, /home is the parent directory of /home/you. You also will see the term child directory. In this example, /home/you is a child directory of /home, sometimes also called a subdirectory.


Although it's difficult to conceive at first how the two special relative paths might be useful, routine applications do arise. For example, you can use them to move empty.txt, notfull.txt, and deeperfiles all back into your home directory:

[View full width]

[you@workstation20 ~]$ mv emptyfiles/empty.txt emptyfiles/deeperfiles deeperfiles/notfull .txt . [you@workstation20 ~]$ ls Desktop anotherfile.txt deeperfiles emptyfiles empty.txt notfull.txt [you@workstation20 ~]$

Get lost? Note that the last argument to mv in the command is the single period (.), causing mv to use the current working directory as the final destination for all the files listed:

  • The first argument to mv moves the file empty.txt from the emptyfiles directory to . (the current working directory, currently your home directory).

  • The second argument moves the directory deeperfiles from the emptyfiles directory to the current working directory, illustrating that you can also use mv to move directories.

  • Finally, because the deeperfiles directory was already moved to the current working directory by the processing of the second argument, the third argument moves the file notfull.txt from the deeperfiles directory to the current working directory.

To illustrate one final use of the mv command before we proceed, let's rename the file notfull.txt to alsoempty.txt and the directory deeperfiles to moreemptyfiles. Linux does not have a "rename" command, but you don't need it because you can use the more powerful mv command to achieve the same result:

 [you@workstation20 ~]$ mv notfull.txt alsoempty.txt [you@workstation20 ~]$ mv deeperfiles moreemptyfiles [you@workstation20 ~]$ ls Desktop  alsoempty.txt  anotherfile.txt  emptyfiles  empty.txt  moreemptyfiles [you@workstation20 ~]$ 

You're almost finished learning about basic navigation. But before we go on, let's take a moment to clean up the mess we made in your home directory.

Deleting Files and Directories

The command to remove files is rm; the command to remove directories is rmdir. With two quick commands and a few arguments, you can get rid of the files and directories you created and left here and there:

 [you@workstation20 ~]$ rmdir emptyfiles moreemptyfiles [you@workstation20 ~]$ rm alsoempty.txt empty.txt [you@workstation20 ~]$ ls Desktop   anotherfile.txt [you@workstation20 ~]$ 

Removing Lots of Files and Directories at Once

You can also use the rm command to delete directories or entire sections of the file system tree. The rm command does this when you supply the -r (recursive) and -f (force) options, commonly shortened to -rf. One of the most commonly typed phrases in Unix is

 rm -rf somedirectory 

where somedirectory represents a directory full of files and other directories that are no longer needed. Although rmdir will respond with an error message if asked to delete a directory that is not empty, rm -rf will erase the directory and all of its contents (including other directories and the files that they contain) without pause.

Be careful when using rm -rf! Because rm -rf simply removes files and directories (whether or not they are empty) without asking questions, you can easily wipe out vast swaths of your home directory with this command if you're not careful. When you're logged in as the system administrator in later chapters, be doubly careful about using rm -rf, if you are brave enough to do it at all; otherwise, you could end up erasing your entire hard drive with a single command:

 rm -rf / 


Your Files Have Been Alphabetized for You

When working in directories with small numbers of files, you might not have noticed that the ls command typically displays the contents of any directory in vertically arranged alphabetical order. This ordering is evident if you use ls to get a listing of the /bin directory.




    SAMS Teach Yourself Red Hat(r) Fedora(tm) 4 Linux(r) All in One
    Cisco ASA and PIX Firewall Handbook
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 311
    Authors: David Hucaby

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net