Читать книгу Linux Bible - Christopher Negus - Страница 19
Part II
Becoming a Linux Power User
Chapter 3
Using the Shell
Running Commands
ОглавлениеThe simplest way to run a command is to type the name of the command from a shell. From your desktop, open a Terminal window. Then type the following command:
Typing the date
command, with no options or arguments, causes the current day, month, date, time, time zone, and year to be displayed as just shown. Here are a few other commands you can try:
The pwd
command shows your current working directory. Typing hostname
shows your computer's hostname. The ls
command lists the files and directories in your current directory. Although many commands can be run by just typing command names, it's more common to type more after the command to modify its behavior. The characters and words you can type after a command are called options and arguments.
Understanding command syntax
Most commands have one or more options you can add to change the command's behavior. Options typically consist of a single letter, preceded by a hyphen. However, you can group single-letter options together or precede each with a hyphen, to use more than one option at a time. For example, the following two uses of options for the ls
command are the same:
In both cases, the ls
command is run with the -l
(long listing), -a
(show hidden dot files), and -t
options (list by time).
Some commands include options that are represented by a whole word. To tell a command to use a whole word as an option, you typically precede it with a double hyphen (-
). For example, to use the help option on many commands, you enter -help
on the command line. Without the double hyphen, the letters h
, e
, l
, and p
would be interpreted as separate options. (There are some commands that don't follow the double hyphen convention, using a single hyphen before a word, but most commands use double hyphens for word options.)
NOTE
You can use the
-help
option with most commands to see the options and arguments that they support: for example, try typing hostname – help.
Many commands also accept arguments after certain options are entered or at the end of the entire command line. An argument is an extra piece of information, such as a filename, directory, username, device, or other item that tells the command what to act on. For example, cat /etc/passwd
displays the contents of the /etc/passwd
file on your screen. In this case, /etc/passwd
is the argument. Usually, you can have as many arguments as you want on the command line, limited only by the total number of characters allowed on a command line.
Sometimes, an argument is associated with an option. In that case, the argument must immediately follow the option. With single-letter options, the argument typically follows after a space. For full-word options, the argument often follows an equal sign (=
). Here are some examples:
In the previous example, the -hide
option tells the ls
command to not display the file or directory named Desktop
when listing the contents of the directory. Notice that the equal sign immediately follows the option (no space) and then the argument (again, no space).
Here's an example of a single-letter option that is followed by an argument:
In the tar
example just shown, the options say to create (c
) a file (f
) named backup.tar
that includes all the contents of the /home/chris
directory and its subdirectories and show verbose messages as the backup is created (v
). Because backup.tar
is an argument to the f
option, backup.tar
must immediately follow the option.
Here are a few commands you can try out. See how they behave differently with different options:
The ls
command, by itself, shows all regular files and directories in the current directory. By adding the -a
, you can also see the hidden files in the directory (those beginning with a dot). The uname
command shows the type of system you are running (Linux). When you add -a
, you also can see the hostname, kernel release, and kernel version.
The date
command has some special types of options. By itself, date
simply prints the current day, date, and time as shown above. But the date
command supports a special +
format option, which lets you display the date in different formats. Type date – help to see different format indicators you can use.
Try the id
and who
commands to get a feel for your current Linux environment, as described in the following paragraphs.
When you log in to a Linux system, Linux views you as having a particular identity, which includes your username, group name, user ID, and group ID. Linux also keeps track of your login session: It knows when you logged in, how long you have been idle, and where you logged in from.
To find out information about your identity, use the id
command as follows:
In this example, the username is chris
, which is represented by the numeric user ID (uid) 501
. The primary group for chris also is called chris
, which has a group ID (gid) of 501
. It is normal for Fedora and Red Hat Enterprise Linux users to have the same primary group name as their username. The user chris
also belongs to other groups called sales
(gid 105
) and lp
(gid 7
). These names and numbers represent the permissions that chris
has to access computer resources.
NOTE
Linux distributions that have Security Enhanced Linux (SELinux) enabled, such as Fedora and RHEL, show additional information at the end of the
id
output. That output might look something like the following:
SELinux provides a means of tightly locking down the security of a Linux system. See Chapter 24, “Enhancing Linux Security with SELinux,” if you want to learn about SELinux.
You can see information about your current login session by using the who
command. In the following example, the -u
option says to add information about idle time and the process ID and -H
asks that a header be printed:
The output from this who
command shows that the user chris
is logged in on tty1
(which is the first virtual console on the monitor connected to the computer), and his login session began at 20:57 on January 13. The IDLE
time shows how long the shell has been open without any command being typed (the dot indicates that it is currently active). PID
shows the process ID of the user's login shell. COMMENT
would show the name of the remote computer the user had logged in from, if that user had logged in from another computer on the network, or the name of the local X display if that user were using a Terminal window (such as :0.0).
Locating commands
Now that you have typed a few commands, you may wonder where those commands are located and how the shell finds the commands you type. To find commands you type, the shell looks in what is referred to as your path. For commands that are not in your path, you can type the complete identity of the location of the command.
If you know the directory that contains the command you want to run, one way to run it is to type the full, or absolute, path to that command. For example, you run the date
command from the /bin
directory by typing
Of course, this can be inconvenient, especially if the command resides in a directory with a long pathname. The better way is to have commands stored in well-known directories and then add those directories to your shell's PATH
environment variable. The path consists of a list of directories that are checked sequentially for the commands you enter. To see your current path, type the following:
The results show a common default path for a regular Linux user. Directories in the path list are separated by colons. Most user commands that come with Linux are stored in the /bin
, /usr/bin
, or /usr/local/bin
directories. The /sbin
and /usr/sbin
directories contain administrative commands (some Linux systems don't put those directories in regular users' paths). The last directory shown is the bin
directory in the user's home
directory (/home/chris/bin
).
TIP
If you want to add your own commands or shell scripts, place them in the
bin
directory in your home directory (such as/home/chris/bin
for the user namedchris
). This directory is automatically added to your path in some Linux systems, although you may need to create that directory or add it to yourPATH
on other Linux systems. So, as long as you add the command to yourbin
with execute permission, you can begin using it by simply typing the command name at your shell prompt. To make commands available to all users, add them to/usr/local/bin
.
Unlike some other operating systems, Linux does not, by default, check the current directory for an executable before searching the path. It immediately begins searching the path, and executables in the current directory are run only if they are in the PATH
variable or you give their absolute (such as /home/chris/scriptx.sh
) or relative (for example, ./scriptx.sh
) address.
The path directory order is important. Directories are checked from left to right. So, in this example, if there is a command called foo
located in both the /bin
and /usr/bin
directories, the one in /bin
is executed. To have the other foo
command run, you either type the full path to the command or change your PATH
variable. (Changing your PATH
and adding directories to it are described later in this chapter.)
Not all the commands you run are located in directories in your PATH
variable. Some commands are built into the shell. Other commands can be overridden by creating aliases that define any commands and options that you want the command to run. There are also ways of defining a function that consists of a stored series of commands. Here is the order in which the shell checks for the commands you type:
Конец ознакомительного фрагмента. Купить книгу