July 20, 2011, 1:27 a.m.
posted by homene
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.
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.
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:
[[email protected] ~]$
The first part of the prompt, [email protected], 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
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.
The default prompt for the user you, which incorporates the username, the machine name, and the current working directory, is therefore
[[email protected] ~]$
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:
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.
Figure. After you enter the ls command and provide the root directory (/) as an argument, the contents of the root directory are listed.
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.
Figure. 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.
Figure. The results of one more invocation of the ls command, this time listing the contents of the path /usr/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 Figure 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:
[[email protected] ~]$ touch empty.txt [[email protected] ~]$
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:
[[email protected] ~]$ ls Desktop anotherfile.txt empty.txt [[email protected] ~]$
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:
[[email protected] ~]$ pwd /home/you [[email protected] ~]$
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:
[[email protected] ~]$ cd / [[email protected] /]$
[[email protected] /]$ pwd / [[email protected] /]$
Now, using the ls command without arguments displays the contents of the root directory, rather than the contents of your home directory:
[[email protected] /]$ ls bin dev home lib misc opt root selinux sys usr boot etc initrd media mnt proc sbin srv tmp var [[email protected] /]$
[[email protected] /]$ cd /usr/X11R6 [[email protected] X11R6]$ pwd /usr/X11R6 [[email protected] X11R6]$ ls bin include lib man share [[email protected] X11R6]$
[[email protected] X11R6]$ cd [[email protected] ~]$ pwd /home/you [[email protected] ~]$
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:
[[email protected] ~]$ cp empty.txt notfull.txt [[email protected] ~]$ ls Desktop anotherfile.txt empty.txt notfull.txt [[email protected] ~]$
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:
[[email protected] `]$ mkdir emptyfiles [[email protected] ~]$ ls Desktop anotherfile.txt emptyfiles empty.txt notfull.txt [[email protected] ~]$
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:
[[email protected] ~]$ mv empty.txt emptyfiles [[email protected] ~]$ ls Desktop anotherfile.txt emptyfiles notfull.txt [[email protected] ~]$ ls emptyfiles empty.txt [[email protected] ~]$
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:
[[email protected] ~]$ pwd /home/you [[email protected] ~]$ mkdir emptyfiles/deeperfiles [[email protected] ~]$ ls Desktop anotherfile.txt emptyfiles notfull.txt [[email protected] ~]$ ls emptyfiles deeperfiles empty.txt [[email protected] ~]$
[[email protected] ~]$ mv notfull.txt emptyfiles/deeperfiles [[email protected] ~]$ ls Desktop anotherfile.txt emptyfiles [[email protected] ~]$ ls emptyfiles deeperfiles empty.txt [[email protected] ~]$ ls emptyfiles/deeperfiles notfull.txt [[email protected] ~]$
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:
[[email protected] ~]$ ls Desktop anotherfile.txt emptyfiles [[email protected] ~]$ ls /home/you Desktop anotherfile.txt emptyfiles [[email protected] ~]$ ls . Desktop anotherfile.txt emptyfiles [[email protected] ~]$
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:
[[email protected] ~]$ ls /home you [[email protected] ~]$ ls .. you [[email protected] ~]$
[[email protected] ~]$ cd .. [[email protected] home]$ pwd /home [[email protected] 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:
[[email protected] home]$ cd .. [[email protected] /]$ pwd / [[email protected] /]$ ls bin dev home lib misc opt root selinux sys usr boot etc initrd media mnt proc sbin srv tmp var [[email protected] /]$ ls . bin dev home lib misc opt root selinux sys usr boot etc initrd media mnt proc sbin srv tmp var [[email protected] /]$ ls .. bin dev home lib misc opt root selinux sys usr boot etc initrd media mnt proc sbin srv tmp var [[email protected] /]$
[[email protected] /]$ cd [[email protected] ~]$ pwd /home/you [[email protected] ~]$
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:
[email protected] ~]$ mv emptyfiles/empty.txt emptyfiles/deeperfiles deeperfiles/notfull .txt . [[email protected] ~]$ ls Desktop anotherfile.txt deeperfiles emptyfiles empty.txt notfull.txt [[email protected] ~]$
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:
[[email protected] ~]$ mv notfull.txt alsoempty.txt [[email protected] ~]$ mv deeperfiles moreemptyfiles [[email protected] ~]$ ls Desktop alsoempty.txt anotherfile.txt emptyfiles empty.txt moreemptyfiles [[email protected] ~]$
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:
[[email protected] ~]$ rmdir emptyfiles moreemptyfiles [[email protected] ~]$ rm alsoempty.txt empty.txt [[email protected] ~]$ ls Desktop anotherfile.txt [[email protected] ~]$