Shell Prompt Basics
11.1. Why Use a Shell Prompt
Graphical environments for Linux have come a long way in the past few years. You can be perfectly productive in the X Window System, and only have to open a shell prompt to complete a few tasks.
However, many Red Hat Linux functions can be completed faster from the shell prompt than from a GUI. In less time than it might take you to open a file manager, locate a directory, and then create, delete, or modify files from a GUI, you could have finished your work with just a few commands at a shell prompt.
A shell prompt looks similar to other command-line interfaces you might be familiar with. Users type commands at a shell prompt, the shell interprets these commands, and then the shell tells the OS what to do. Experienced users can write shell scripts to expand their capabilities even further.
Figure 11-1. A Shell Prompt
This section explains how to navigate, manipulate files, perform simple administration tasks, and other shell prompt basics.
11.2. The History of the Shell
When AT&T software engineers Dennis Ritchie and Ken Thompson were designing UNIX, they wanted to create a way for people to interact with their new system. Operating systems at that time came with command interpreters, which could take commands from the user and interpret them so that computers could use them. But Ritchie and Thompson wanted something more, something that offered better features than the command interpreters available at that time. This lead to the development of the Bourne shell (known simply as sh), created by S.R. Bourne. Since the creation of the Bourne shell, other shells have been developed, such as the C shell (csh) and the Korn shell (ksh).
When the Free Software Foundation sought a royalty-free shell, developers began to work on the language behind the Bourne shell as well as some of the popular features from other shells available at the time.
The result was the Bourne Again Shell, or bash. Although your Red Hat Linux includes several different shells, bash is the default shell for interactive users. You can learn more about bash by reading the bash man page (type man bash at a shell prompt).
11.3. Determining Your Current Directory with pwd
Once you start looking through directories, it is easy to get lost or forget the name of your current directory. By default, bash shows just your current directory, not the entire path.
Figure 11-2. The Command pwd Shows You Where You Are
To determine the exact location of your current directory within the file system, go to a shell prompt and type the command pwd.
/home/sam
This tells you that you are in the user sam's directory, which is in the /home directory. The command pwd stands for print working directory. When you typed pwd, you asked your Linux system to display your current location. Your system responded by printing the name of the current directory in the terminal window, also known as the standard output. You will find that using pwd is very helpful as you learn to navigate your new Linux system.
11.4. Changing Directories with cd
Changing directories is easy as long as you know where you are (your current directory) and how that relates to where you want to go.
To change directories, use the cd command. Typing this command by itself will always return you to your home directory; moving to any other directory requires a pathname.
You can use absolute or relative pathnames. Absolute paths start at the top with / (referred to as root) and then look down for the requested directory; relative paths look down from your current directory, wherever that may be. The following directory tree illustrates how cd operates.
/
/directory1
/directory1/directory2
/directory1/directory2/directory3
If you are currently in directory3 and you want to switch to directory1, you need to move up in the directory tree.
Executing the command
cd directory1
while you are in directory3, will present you with an error message explaining that there is no such directory. This is because there is no directory1 below directory3.
To move up to directory1, type:
cd /directory1
This is an example of an absolute path. It tells Linux to start at the top (/) and look down until it finds directory1. A path is absolute if the first character is a /. Otherwise, it is a relative path.
Absolute paths will take you to any directory, from any directory. Relative paths will only take you to directories below your current one.
Use the following exercise to test what you have learned so far regarding absolute and relative paths.
From your home directory, type the relative path:
cd ../../etc/X11
The command cd .. tells your system to go up to the directory immediately above the one in which you are currently working. To go up two directories, type the following:
cd ../..
After using the full command in the example, you should be in the directory X11, which is where you will find configuration files and directories related to the X Window System.
Take a look at your last cd command. You told your system to:
1. Go up one level to your login directory's parent directory (probably /home)
2. Then go up to that directory's parent (which is the root, or /, directory)
3. Then go down to the etc directory
4. Finally, go to the X11 directory
Conversely, using an absolute path would get you to the /etc/X11 directory more quickly.
For example:
cd /etc/X11
Absolute paths start from the root directory (/) and move down to the directory you specify.
Note
Always make sure you know which working directory you are in before you state the relative path to the directory or file you want to get to. You do not have to worry about your position in the file system, though, when you state the absolute path to another directory or file. If you are not sure, type pwd and your current working directory will be displayed, which can be your guide for moving up and down directories using relative pathnames.
Command Function
cd returns you to your login directory
cd ~ also returns you to your login directory
cd / takes you to the entire system's root directory
cd /root takes you to the home directory of the root, or superuser, account created at installation; you must be the root user to access
this directory
cd /home takes you to the home directory, where user login directories are usually stored
cd.. moves you up one directory
cd ~otheruser takes you to otheruser's login directory, if otheruser has granted you permission
cd /dir1/subdirfoo regardless of which directory you are in, this absolute path would take you staight to subdirfoo, a subdirectory of dir1
cd ../../dir3/X11 this relative path would take you up two directories to root, then to dir3, then to the X11 directory.
Table 11-1. cd Options
Now that you are starting to understand how to change directories, see what happens when you change to root's login directory (the superuser account).
Type:
cd /root
If you are not logged in as root, you are denied permission to access that directory. Denying access to the root and other users' accounts (or login directories) is one way your Linux system prevents accidental or malicious tampering. See Section 11.14. To change to the root login and root directory, use the su command.
For example:
su
Password:your root password
cd /root
[root@halloween /root]#
Tip
The command su means substitute users and it allows you to temporarily log in as another user. When you type su by itself and press [Enter], you become root (also called the superuser) while still inside your login shell (your user's home directory). Typing su - makes you become root with root's login shell -- it is as if you had logged in as root originally.
As soon as you give the root password, you will see the changes in your command prompt to show your new, superuser status, the root account designation at the front of the prompt and "#" at the end (as shown in the prior example). When you are done working as root, type exit at the prompt and you will return to your user account.
exit
exit
[sam@halloween sam]$
No comments:
Post a Comment