Sie sind auf Seite 1von 51

Linux

Essentials
Getting to Know the Command Line

Abel Damtew
Sunday, July 23, 2017
Contents to be Discussed
§ Understanding the Linux shell
§ Starting a command line
§ Running Programs
§ Using shell features
§ Getting Help using man pages
§ Getting Help using info pages
§ Finding additional documentation
Understanding the Linux shell
§ Before icons and windows took over computer screens, you typed commands to
interact with most computers.
§ On UNIX systems, from which Linux was derived, the program used to interpret
and manage commands was referred to as the shell.
§ The shell provides a way to create executable script files, run programs, work with
le systems, compile computer code, and manage the computer.
§ Although the shell is less intuitive than common graphic user interfaces (GUIs),
most Linux experts consider the shell to be much more powerful than GUIs.
§ Shells have been around a long time, and many advanced features have been built
into them.
Understanding the Linux shell (continued)
§ The default shell in most Linux distributions is the Bourne-Again Shell (Bash or
bash), which is based on an older shell called the Bourne Shell.
§ Other shells are available. Most of these are similar to Bash in broad strokes,
although some details differ.
§ Each account specifies its own default shell, so individual users can change their
shells if they like.
Starting a Command Line
§ There are several ways to get to a shell interface in Linux.
§ Three methods are commonly used for this:
§ starting a shell in a GUI window called a terminal,
§ logging into the computer in a text-mode console, and
§ logging into the computer remotely using a text-mode login protocol.
§ In either case, you should log in with a regular user account.
Launching a Terminal
§ Most Linux distributions provide several terminal programs.
§ Most desktop environments provide their own terminals, so your choices depend
on the desktop environments you installed.
§ Most terminal programs include the word terminal in their names, although some
don’t, such as the K Desktop Environment’s (KDE’s) Konsole and the generic
xterm.
§ The details of how to launch a terminal program differ from one desktop
environment to another.
§ Here are two common ways to launch a Terminal window from a Linux desktop:
§ Right-click the desktop
§ Click on the panel menu
Launching a Terminal (continued)
§ For example, in CentOS 7, you can select Applications ➪ System Tools ➪ Terminal to
open a Terminal window.
§ Right-Click the desktop ➪ Open Terminal
§ The terminal program shows a prompt, for example, [abel@localhost ~]$.
§ This example shows the default CentOS prompt, which includes your username, your
computer’s hostname, your current directory (a tilde, ~, refers to your home directory),
and a dollar sign ($).
§ Some of these features are likely to change as you use the shell.
Launching a Terminal (continued)
Launching a Terminal (continued)
§ Most terminal programs provide common features of X programs—you can resize them,
close them, select options from menus, and so on.
§ For example, in GNOME Terminal you can cut and paste text to or from a GNOME
Terminal window, change fonts, set a title, choose colors or images to use as background,
and set how much text to save when text scrolls off the screen.
§ To change the features of your GNOME Terminal follow this procedure:
§ Select Edit ➪ Profile preferences
Launching a Terminal (continued)
§ Most terminal programs support tabs, which are similar to the tabs in a Web
browser.
§ In most cases, you can open a tab by selecting File ➪ Open Tab in the terminal’s
menu.
§ Having multiple tabs open is handy because it enables you to run multiple
programs simultaneously, work easily in multiple directories, or run programs
both as yourself and as root.
§ When you’re done with a terminal, you can close it like other programs,
§ by selecting File ➪ Close Window from its menu.
§ Alternatively, you can type exit at its shell prompt.
Logging Into a Text-Mode Console
§ Linux supports virtual terminals (VTs), which are virtual screens that can hold
different types of information—textual or graphical.
§ Most Linux distributions run with six or seven VTs. In CentOS, Fedora, and Red
Hat, the first VT typically runs the X Window System, which is Linux’s GUI.
§ In most other distributions, X runs in VT 7 or VT 8, leaving VT 1 as a text-mode
display.
§ You can switch between VTs by pressing Ctrl+Alt+Fn, where Fn is a function key.
(When switching between text-mode VTs, Alt+Fn is suf cient.)
§ Thus, to enable a text-mode console, follow these steps:
§ Press Ctrl+Alt+F2. You’ll see a text-mode prompt that reads some- thing like this:
§ Type your username at this prompt and it will respond with a new prompt:
§ Type your password here and you’ll see a Bash prompt
Logging Into a Text-Mode Console (continued)
§ Linux supports virtual terminals (VTs), which are virtual screens that can hold
different types of information—textual or graphical.
§ You can switch back and forth between your text-mode login and your X session
by using Ctrl+Alt+Fn keystrokes.
§ You can also initiate multiple text-mode logins and switch between them in the
same way.
§ When you’re done with your text-mode session, type exit or logout to terminate
it.
Logging In Remotely
§ Remote logins must be enabled by running a server program such as the Secure
Shell (SSH), and you may find that you can’t initiate such a connection by default.
§ Once you’ve configured SSH to accept remote logins, though, you can use an SSH
client program, such as ssh, to do the job.
§ Other text-mode remote-login programs, such as telnet and rlogin, exist; however, most
of these are older protocols that don’t employ encryption and so should be avoided.
§ GUI remote login tools, such as Virtual Network Computing (VNC), are also available.
Most of these tools also lack encryption features, although they can be tunneled through
SSH to add encryption.
§ Once you’ve logged in remotely, you can use the computer much as if it were local. Be
sure to log out when you’re finished by typing exit or logout.
Running Programs
§ Once you’ve opened a terminal or logged in using a text-mode tool, you should
know how to use the shell.
§ Bash includes a few built-in commands, but much of what you’ll do in a shell
involves running other programs.
§ As described in the following sections, you can run text-mode and GUI programs.
§ Sometimes you may want to run a program in the background and yet retain use
of the shell for yourself.
§ Another trick in running programs is redirecting input and output, which can be
useful in many situations.
Running Text-Mode Programs
§ Linux stores programs in several locations, including /bin, /usr/bin, and /usr /
local/bin.
§ Programs that are used mainly by root appear in /sbin, /usr/sbin, and
/usr/local/sbin as well.
§ If an executable program appears in one of these directories (which make up the
path), you can run it simply by typing its name:
free
§ This example command displays information on the computer’s use of memory.
Running Text-Mode Programs (continued)
§ You can learn what directories make up the path by typing the following
command:
echo $PATH
§ The result will be a colon-delimited set of directory names, which the shell
searches in sequence whenever you type a command that it doesn’t handle
directly.
Understanding command syntax
§ You must know a command’s syntax to enter it properly.
§ Syntax refers to a command’s format and wording, as well as the options and arguments
you can use to extend and modify its functions.
§ Most commands are single words, such as the command clear.
§ If you enter a command using correct syntax, Linux execute the command. Otherwise,
you receive a message that Linux cannot interpret your command.
§ Commands use the following syntax:
command_name [-option] [argument]
Understanding command syntax (continued)
§ The command_name specifies what operation to perform.
§ Most commands have one or more options you can add to change the command’s
behavior.
§ Options follow command names, separated by a space. They typically consist of a
single letter, preceded by a hyphen.
§ Options are also case sensitive. For example, -R differs from -r.
§ You do not need to type an option after every command; however, some
commands do not work unless you specify an option.
Understanding command syntax (continued)
§ 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:
ls -l -a –t
ls -lat
§ 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 (--).
§ Without the double hyphen, the letters 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 will use double hyphens for word options.
Understanding command syntax (continued)
§ 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.
§ There are times when 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:
Understanding command syntax (continued)
§ Here are some examples:
ls --hide=Desktop
§ 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:
tar -cvf backup.tar /home/abel
§ 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/abel directory and its subdirectories and show verbose
messages as the backup is done (v).
§ Because backup.tar is an argument to the f option, backup.tar must immediately follow the
option.
Basic Linux Commands
§ Here are a few commands you can try out. See how they behave differently with
different options:
§ The date Command
§ Display the current time in the given FORMAT, or set the system date
§ Syntax
date [OPTION]... [+FORMAT]
§ FORMAT controls the output.
date +'%d/%m/%y’

date +'%A, %B %d, %Y'


§ The date command has an option, -u, which displays the time in Greenwich Mean Time (GMT) and -s
to reset the date or time.
date –u
sudo date -s '2017-07-20 21:00:00'
§ Type date --help to see different format indicators you can use.
Basic Linux Commands (continued)
§ The cal Command
§ Displays a simple calendar. If no arguments are specified, the current month is displayed.
§ Syntax
cal [OPTION]... [Argument]
§ The cal command has options

§ The cal command accepts year as an argument, to display the calendar for the year 1970
cal 1970
Basic Linux Commands (continued)
§ The who Command
§ Print information about users who are currently logged in.
§ In a multiuser system, knowing who is logged in to the system is important for the administrators.
§ Syntax
who [OPTION]
§ Commonly used options
§ am i (type who in front, as in who am i ) for information about your own session
§ whoami (type whoami as all one word) to see what account you are using,
§ -H to show column headings,
§ -u to show idle time for each user (the older -i is being retired from the who options)
§ -q for a quick list and total of users logged in,
§ -b used by system administrators and others to verify when the system was last booted.
§ 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:
who -uH
Basic Linux Commands (continued)
§ The ls Command
§ List information about the FILEs (the current directory by default).
§ Syntax
ls [OPTION]... [FILE]...
§ Commonly used options
§ -a, --all do not ignore entries starting with .
§ -l use a long listing format
§ -s, --size print the allocated size of each file, in blocks
§ -S sort by file size
§ -t sort by modification time, newest first
§ For example, to long list the contents of the / directory
ls –l /
Basic Linux Commands (continued)
§ The clear Command
§ As you continue to enter commands, your screen might become cluttered.
§ Unless you need to refer to commands you previously entered and to their output,
you can use the clear command to clear your screen.
§ It has no options or arguments.
The sudo Command
§ The sudo command stands for "superuser do".
§ It prompts you for your personal password and confirms your request to execute a
command by checking a file, called sudoers, which the system administrator configures.
§ Using the sudoers file, system administrators can give certain users or groups access to
some or all commands without those users having to know the root password.
§ It also logs all commands and arguments so there is a record of who used it for what, and
when.
§ To use the sudo command, at the command prompt, enter:
sudo command
§ Replace command with the command for which you want to use sudo.
The sudo Command
The su Command
§ The su command stands for "switch user", and allows you to become another user.
§ To use the su command on a per-command basis, enter:
su user -c command
§ Replace user with the name of the account which you'd like to run the command as,
and command with the command you need to run as another user.
§ To switch users before running many commands, enter:
su user
§ Replace user with the name of the account which you'd like to run the commands as.
§ The user feature is optional; if you don't provide a user, the su command defaults to
the root account, which in Linux is the system administrator account.
Running GUI Programs
§ You can run GUI programs from a terminal as well as text-based programs;
however, this doesn’t work if you logged in using a text-mode VT, and it might not
work with a remote login even if you’re running X locally.
§ You must also know the filename of the program to run it.
§ The filename is usually related to the name you use to launch the program from a
desktop’s menus, but it’s not always identical.
§ For instance, GNOME Terminal’s filename is gnome-terminal, so that’s what you’d need
to type to launch another GNOME Terminal in this way.
§ Some GUI programs produce text-mode output that can be useful in tracking down the
source of problems.
§ You might also want to launch programs in this way because it can be quicker than
tracking down programs in a desktop environment’s menus, or because a program
doesn’t appear in the environment’s menus.
Running Programs in the Background
§ When you launch a GUI program from a terminal window, the GUI program opens its
own window or windows. The terminal window remains open, but will normally become
unresponsive.
§ If you want to type more commands in this window, you can do so by selecting it and
pressing Ctrl+Z. This suspends the program—that is, it’s sent to sleep.
§ In its sleeping state, the GUI program won’t respond to input or do any work.
§ If you want to use both the GUI program and the terminal from which you launched it,
you can type bg (short for background) in the terminal.
§ Both programs will now be active. Typing fg returns the sleeping program to the
foreground, enabling it to run but making your shell unresponsive.
§ Note that, in this context, the terms background and foreground refer to the program’s
relationship to the shell, not to the position of the program’s windows in a “stack” of
windows on the screen.
Running Programs in the Background (continued)
§ If you know before you launch it that you want to run a program in the back- ground, you
can do so by appending an ampersand (&) to the end of the command line, as in:
gedit afile.txt &
§ This command launches the gedit GUI editor on afile.txt in the background, enabling you
to edit your file and continue to use the shell.
§ This feature is most useful for running GUI programs, but it’s sometimes used with text-
mode programs, too.
§ A complex number-crunching program, for instance, might be designed to run for several
minutes or hours and produce no output.
Using Shell Features
§ Bash includes several features that make using it much easier.
§ In the following sections we will see some of these features.
Using Command Completion
§ Command completion is the hero of everybody who hates typing: It’s a way to enter a long
command or filename with a minimal number of keystrokes.
§ To use command completion, you type part of a command or filename and then press the Tab
key.
§ If only one command on the path completes the command, Bash fills in the rest—and likewise
when using command completion to refer to files.
§ To illustrate the use of command completion, you can try it out with a few commands:
1. Launch a shell.
2. Type if followed by pressing the Tab key. The computer will probably beep or sound a tone. This indicates
that your incomplete command could be completed by multiple commands, so you must type more
characters.
3. Press the Tab key again. The shell displays a list of possible completions, such as if, ifconfig, and ifdown.
4. Type co, making your command so far ifco, and press the Tab key again. The computer will probably
complete the command: ifconfig.
5. Press the Enter key. The computer runs ifconfig, which displays information on your network connections.
Using Command Completion (continued)
§ Command completion also works with files.
§ For instance, you can type cat /etc/ser followed by the Tab key to have Bash
complete the filename, and therefore the command, as cat /etc/services. (This
command shows you the contents of a Linux configuration file.)
Using Command History
§ Bash remembers the recent commands you’ve typed, and you can use this fact to save
yourself some effort if you need to type a command that’s similar to one you’ve typed
recently.
§ In its most basic form, you can use the up arrow key to enter the previous command;
pressing the up arrow repeatedly moves backward through earlier and earlier
commands.
Using Command History (continued)
§ The following Table summarizes some other commonly used keystrokes you can use in
the command history—or even when editing new commands.
Using Command History (continued)
§ Another history feature is the history command.
§ Type history to view all the commands in your history, or add a number (as in
history 10) to view the most recent specified number of commands.
Getting Help Using Man Pages (continued)
§ Linux man pages can be an extremely helpful resource, but you must understand their
purpose and limitations.
§ Unlike the help systems in some OSs, Linux man pages are not supposed to be tutorial in
nature; they’re intended as quick references to help somebody who’s already at least
somewhat familiar with a command, configuration file, or other OS feature.
§ They’re most useful when you need to know the options to use with a command, the
name of an option in a configuration file, or similar details.
§ If you need to learn a new program from scratch, other documentation is often a better
choice.
§ In the simplest case, you can read a man page by typing man followed by the name of a
command, configuration file, system call, or other keyword.
§ To close the man page press q.
Getting Help Using Man Pages (continued)
§ Each man page falls into one of nine categories, as summarized in Table.
Section number Description
1 Executable programs and shell commands
2 System calls provided by the kernel
3 Library calls provided by program libraries
4 Device files (usually stored in /dev)
5 File formats
6 Games
7 Miscellaneous (macro packages, conventions, and so on.)

8 System administration commands (programs run mostly or


exclusively by root)
9 Kernel routines
Searching for a man Page
§ Summary search
§ The whatis command searches summary information contained in man pages for the
keyword you specify.
§ The command returns a one-line summary for every matching man page.
§ For instance, typing whatis date returns lines confirming the existence of the man page
entries for date, in various sections.
§ Thorough search
§ The apropos command performs a more thorough search, of both the Name and Description
sections of man pages.
§ The result looks much like the results of a whatis search, except that it’s likely to contain
many more results.
§ For instance, typing apropos samba returns eight entries on one of my systems, including
those for cupsaddsmb, smbpasswd, and lmhosts—all tools related to the Samba le- and
printer-sharing tool.
Reading a man Page
§ Although the man pages for some commands contain more information
than others, most man pages list the following items:
§ Name: The name of the command and a short statement describing its purpose
§ Synopsis: A syntax diagram showing the usage of the command
§ Description: A more detailed description of the command than the name item gives as well
as a list of command options and their descriptions
§ History: The information that is sometimes included to show where the command originated
§ Other Versions: The information that is sometimes included to indicate there are other
versions of the command available
§ See Also: The other commands or man pages that provide related information
§ Bugs: Many man pages provide a Bugs section in which the author describes any known bugs or
limitations, or states that no known bugs exist.
§ Author: The name or names of the author or authors who developed the command or
program (if available)
Reading a man Page (continued)
Getting Help Using Info Pages
§ The man page system is ubiquitous on Unix-like OSs, including Linux, but it’s also
§ quite old and is therefore limited. Thus, a newer documentation system, known as
info pages, is also available.
Getting Help Using Info Pages
§ The man page system is ubiquitous on Unix-like OSs, including Linux, but it’s also
Keystroke Action
? Displays help information.
N Moves to the next node in a linked series of nodes on a single hierarchical level. This action may be
required if the author intended several nodes to be read in a particular sequence.
P Moves back in a series of nodes on a single hierarchical level. This can be handy if you’ve moved forward
in such a series but find you need to review earlier material.
U Moves up one level in the node hierarchy.
Arrow keys Moves the cursor around the screen, enabling you to select node links or scroll the screen.
Page Up, Page Down These keys scroll up and down within a single node, respectively. (The standard info browser also
implements many of the more arcane commands used by less and outlined in Table 8.2.)
Enter Moves to a new node once you’ve selected it. Links are indicated by asterisks (*) to the left of their
names.
L Displays the last info page you read. This action can move you up, down, or sideways in the info tree
hierarchy.
T Displays the top page for a topic. Typically this is the page you used to enter the system.
Q Exits from the info page system.
Finding additional documentation
§ Although man pages and info pages are both very useful resources, other documentation
is available, too, and is sometimes preferable to these forms of documentation.
§ Broadly speaking, help on Linux can come in three forms:
§ additional documentation on your computer,
§ additional documentation online, and
§ help from experts.
Locating Program Documentation on Your Computer
§ Although man pages and info pages are both very useful resources, other documentation
is available, too, and is sometimes preferable to these forms of documentation.
§ The most basic and traditional form of program documentation is a le called README,
readme.txt, or something similar.
§ If you downloaded the program as a source code tarball from the package maintainer’s
site, the README file typically appears in the main build directory extracted from the
tarball.
§ If you installed the program from a binary package file, though, the README file could be
in any of several locations. The most likely places are:
§ /usr/doc/packagename,
§ /usr/share/doc/packagename, and
§ /usr/share/doc/packages/packagename
Locating Program Documentation Online
§ HOWTOs
§ Guides
§ FAQs
Consulting Experts
§ Local experts
§ Paid consultants
§ Program authors
§ Web forums, mailing lists, and Usenet newsgroups
§ IRC
§ Web searches
Exercise
§ Display the current date/time in the following format ‘07:29:49‘
§ Display the Julian dates for the year 2020
§ Determine the time of last system boot
§ Display a quick list and total of users logged in the system
§ List all files in the /root directory.
End of Getting to Know the
Command Line

51

Das könnte Ihnen auch gefallen