Sie sind auf Seite 1von 15

103.

1 Work on the command line


Unix: UNIX was originally an operating system developed at AT&T Bell Labs in the 1970’s. It
was modified and forked (that is, people modified it and those modifications served as the
basis for other systems) such that now there are many different variants of UNIX

Three main important Components of an operating system

kernel:
The three main components of an operating system are the kernel, shell and filesystem. The
kernel of the operating system is like an air traffic controller at an airport. The kernel dictates
which program gets which pieces of memory, it starts and kills programs, and it handles
displaying text on a monitor. When an application needs to write to disk, it must ask the
operating system to complete the write operation.The kernel takes care of unloading one task
and loading a new task if there are more tasks than CPUs. When the current task has run a
sufficient amount of time, the CPU pauses the task so that another may run. This is
called pre-emptive multitasking. Multitasking means that the computer is doing several tasks
at once,

shell: A shell is sometimes called an interpreter because it takes the commands that a user
issues and interprets them into a form that the kernel can then execute on the hardware of
the computer. The two most common types of shells are the Graphical User Interface (GUI)
and Command Line Interface (CLI)

advantage to using a CLI

Command Repetition: In a GUI shell, there is no easy way to repeat a previous command. In
a CLI there is an easy way to repeat (and also modify) a previous command.

Command Flexibility: The GUI shell provides limited flexibility in the way the command
executes. In a CLI, options are specified with commands to provide a much more flexible and
powerful interface.

Scripting: In a GUI shell, completing multiple tasks often requires multiple mouse clicks. With
a CLI, a script can be created to execute many complex operations by typing just a single
"command": the name of the script. A script is a series of commands placed into a single file.
When executed, the script runs all of the commands in the file.

Remote Access: While it is possible to remotely execute commands in a GUI shell, this
feature isn't typically set up by default. With a CLI shell, gaining access to a remote machine
is easy and typically available by default.

Development: Normally a GUI-based program takes more time for the developers to create
when compared to CLI-based programs. As a result, there are typically thousands of CLI
programs on a typical Linux OS while only a couple hundred programs in a primarily GUI-
based OS like Microsoft Windows®. More programs means more power and flexibility.

Resources: A GUI shell typically uses a vast amount of resources (RAM, CPU, etc.). This is
because a great deal of processing power and memory is needed to display graphics. By
contrast, a CLI uses very little system resources, allowing more of these resources to be
available to other programs.
Filesystem: a filesystem is a hierarchy of directories and files with the root / directory at the
top of the directory tree. To the operating system, a filesystem is a structure created on a disk
partition consisting of tables defining the locations of directories and files

bootloader: The bootloader’s job is to load the kernel and get it started. The bootloader
loads the Linux kernel and then transfers control.

Applications: applications make requests to the kernel and receive resources, such as
memory, CPU, and disk, in return. The kernel also abstracts the complicated details away from
the application.

Process: A process is just one task that is loaded and tracked by the kernel. An application
may even need multiple processes to function, so the kernel takes care of running the
processes, starting and stopping them as requested, and handing out system resources.

Linux: Linux started out in 1991 as a hobby project by Linus Torvalds. He made the source
freely available and others joined in to shape this fledgling operating system. Linux just
happens to be written in C, which is a language that shares history with the original UNIX.

Bash shell: Linux operating system provide multiple GUI shells, multiple CLI shells are also
available. Normally, these shells are derived from one of two older UNIX shells: The Bourne
Shell and the C Shell. In fact, the bash shell derives its name from the Bourne
Shell: Bourne Again Shell. The bash shell has numerous built-in commands and features that
you will learn including:

Aliases: Give a command a different or shorter name to make working with the shell
more efficient.

Re-Executing Commands: To save retyping long command lines.

Wildcard Matching: Uses special characters like ?, *, and [] to select one or more
files as a group for processing.

Input/Output Redirection: Uses special characters for redirecting input, < or <<, and
output, >.

Pipes: Used to connect one or more simple commands to perform more complex
operations.

Background Processing: Enables programs and commands to run in the background


while the user continues to interact with the shell to complete other tasks.

Command: is a software program that when executed on the command line, performs an
action on the computer.

Sources of commands

Commands built-in to the shell itself: A good example is the cd command as it is


part of the bash shell. When a user types the cd command, the bash shell is already executing
and knows how to interpret that command, requiring no additional programs to be started.

Commands that are stored in files that are searched by the shell: If you type
a lscommand, then the shell searches through the directories that are listed in
internal commands: is a command that is a part of the shell itself. It is available
when the shell is loaded and can be executed from memory without any lookup from disk.
External commands: is a command that exists as an executable file on disk of
the computer. Because it has to be read from disk, it is a bit slower.

the PATHvariable to try to find a file named ls that it can execute. These commands can also
be executed by typing the complete path to the command.

Aliases: An alias can override a built-in command, function, or a command that is


found in a file. Aliases can be useful for creating new commands built from existing functions
and commands.

Functions: Functions can also be built using existing commands to either create new
commands, override commands built-in to the shell or commands stored in files.
Aliases and functions are normally loaded from the initialization files when the shell
first starts, discussed later in this section.

Basic command syntax


command [options] [arguments]

options: Short options are specified with a hyphen (-) followed by a single character. Multiple
single options can be either given as separate options like -a -l -r or combined like -alr.
short options can be combined with other short options in any order. The exception to this is
when an option requires an argument. If using multiple options that require arguments, then
don't combine them.

Long options for commands are preceded by a double hyphen -- and the meaning of the
option is typically the name of the option, like --all.

Commands that support long options will often also support arguments that may be specified
with or without an equal symbol (the output of both commands is the same):
ls --sort time

ls –sort=time

It is possible to use both short and long options in the same command line.

arguments: many commands will accept one or more arguments. Commands that use
arguments may require one or more of them. Arguments that contain unusual characters like
spaces or non-alphanumeric characters will usually need to be quoted, either by enclosing
them within double quotes or single quotes.

Local and environment variable:


A local variable is only available to the shell in which it was created. An environment variable
is available to the shell in which it was created and it is passed into all other
commands/programs started by the shell.

By convention, lowercase characters are used to create local variable names and uppercase
characters are used when naming an environment variable.
Path variable: The PATH variable contains a list of directories that are used to search for
commands entered by the user. When the user types a command and then presses
the Enter key, the PATH directories are searched for an executable file that matches the
command name.

PATH VARIABLE DIRECTORIES:

Directory Contents

/
A directory for the current user sysadmin to place programs. Typically used by
home/sysadmin/b
in
users who create their own scripts.

Normally empty, but may have administrative commands that have been compiled
/usr/local/sbin
from local sources.
Normally empty, but may have commands that have been compiled from local
/usr/local/bin
sources.
/usr/sbin Contains the majority of the administrative command files.
Contains the majority of the commands that are available for regular users to
/usr/bin
execute.
/sbin Contains the essential administrative commands.
Contains the most fundamental commands that are essential for the operating
/bin
system to function.

To execute commands that are not contained in the directories that are listed in
the PATH variable, several options exist:

1.The command may be executed by typing the absolute path to the command.

2.The command may be executed with a relative path to the command.

3.The PATH variable can be set to include the directory where the command is located.

4.The command can be copied to a directory that is listed in the PATH variable.

Absolute path: absolute path specifies the location of a file or directory from the top level
directory through all of the sub directories to the file or directory.

The following diagram illustrates the different files that are started with a typical login shell
versus an interactive shell
When bash is started as a login shell, the /etc/profile file is executed first. This file typically
executes all files ending in .sh that are found in the /etc/profile.d directory. The next file
that is executed is usually ~/.bash_profile but some users may
use ~/.bash_login or ~/.profile file. The ~/.bash_profile file typically also executes
the ~/.bashrc file which in turn executes the /etc/bashrc file.

When bash is started as an interactive shell, it executes the ~/.bashrc file, which may also
execute the /etc/bashrc file, if it exists. Again, since the ~/.bashrc file is owned by the user
who is logging in, the user can prevent execution of the /etc/bashrc file.

With so many initialization files, a common question at this point is "what file am I supposed
to use?" The following chart illustrates the purpose of each of these files, providing examples
of what commands you might place in each file:

File Purpose

/ This file can only be modified by the administrator and will be executed by every user
etc/profil who logs in. Administrators use this file to create key environment variables, display
e messages to users as they log in and set key system values.
Each user has their own .bash_profile file in their home directory. The purpose for this
~/.bash_pr
file is the same as the /etc/profile file, but having this file allows a user to customize
ofile
the shell to their own tastes. Normally used to create customized environment variables.
Each user has their own .bashrc file in their home directory. The purpose for this file is
~/.bashrc to generate things that need to be created for each shell, such as local variables and

aliases.
/ This file may affect every user on the system. Only the administrator can modify this file.
etc/bashrc
Like the .bashrc file, the purpose for this file is to generate things that need to be created
File Purpose

for each shell, such as local variables and aliases.

~/.bash_history: save all commands typed by the user on the bash shell.
$HISTFILESIZE: variable that stores how many commands to write to the file.
~/.bash_history
$HISTFILE: local variable that stores the path of the history command file.
$HISTSIZE: variable will determine how many commands to keep in memory for each
bashshell.
$HISTCONTROL:

The HISTCONTROL variable could be set to any of the following features:

 HISTCONTROL=ignoredups will prevent duplicate commands that are executed consecutively.


HISTCONTROL=ignorespace will not store any command that begins with a space. This provides the user with an easy
way to execute a command that won't go into the history list.
HISTCONTROL=ignoreboth will not store consecutive duplicates or any command that begins with a space.

HISTCONTROL=erasedups will not store a command that is identical to another command in your history (actually, the
previous entry of the command will be deleted from the history list).
HISTCONTROL=ignorespace:erasedups will include the benefit of "erasedups" with the advantage of "ignorespace".

Consider This

When a new user is created, the files from the /etc/skel directory are
automatically copied into the new user's home directory. As an administrator, you
can modify the files in the /etc/skel directory to provide custom features to new
users

Man sections:

Section
Purpose
name

NAME Provides the name of the command and a very brief description.
A brief summary of the command or function's interface. A summary of how
SYNOPSIS
the command line syntax of the program looks.
DESCRIPTION Provides a more detailed description of the command.
OPTIONS Lists the options for the command as well as a description of how they are
used. Often this information will be found in the DESCRIPTION section and not
Section
Purpose
name

in a separate OPTIONSsection.
Lists the files that are associated with the command as well as a description of
how they are used. These files may be used to configure the command's more
FILES
advanced features. Often this information will be found in
the DESCRIPTIONsection and not in a separate FILES section.
The name of the person who created the man page and (sometimes) how to
AUTHOR
contact the person.
REPORTING
Provides details on how to report problems with the command.
BUGS
COPYRIGHT Provides basic copyright information.
Provides you with an idea of where you can find additional information. This
SEE ALSO
also will often include other commands that are related to this command.
Search within a man page: In order to search a man page for a term, type / (forward slash)
and type the term followed by the Enter key.

Return (or Enter) Go down one line


Space Go down one page
/term Search for term
n Find next search item
1G Go to beginning
G Go to end
h Display help
q Quit man page

Additionally, there are several different types of commands (user commands, system
commands, and administration commands) as well as other features that require
documentation, such as libraries and kernel components. By default there are nine default
sections of man pages:

1.Executable programs or shell commands

2.System calls (functions provided by the kernel)

3.Library calls (functions within program libraries)

4.Special files (usually found in /dev)


5.File formats and conventions, e.g. /etc/passwd
6.Games

7.Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)


8.System administration commands (usually only for root)

9.Kernel routines [Non standard]

103.2 Process text streams using filters

cat:
file: To avoid viewing binary files, use the file command. The file command will display the
type of file based upon the file content.

split [option] [input [prefix]]: take one file and split it into multiple files. INPUT is typically a file,
but the split command is able to read from standard input as well.

nl: will show number the lines of its output. The nl command offers many formatting options for
line numbering, including separate styles for headers, footers, sections and the body of a file.

head :The purpose of the head command is to view the beginning of a file or output. By
default, the head command will display the first ten lines of a file's contents.

tail: the tail command displays contents from the end of the file and not the beginning. When
the tailcommand is executed with the -f option and a file name argument, then it will initially
output the number of lines specified. Instead of exiting after displaying the lines, the tail
command continues to run, following any changes in the file and displaying new content as it
is added to the end of the file. One of the main applications for this follow feature is for
administrators to watch log files change while they are troubleshooting problems.

paste: The paste command will merge the lines of one or more files, line by line, separating
them with a tab as delimiter by default.

join: the join command is able to combine two files. Instead of simply going line by line
through the files, the join command matches the values of fields to determine which lines to
combine.

Expand: The expand command will convert tab characters into spaces
unexpand: will convert space characters into tabs.

cut: extracts portions of input lines and displays them on standard output.
Sort: is used to display a file sorted on a specific field of data.

Uniq: removes duplicate lines.

od: performs an octal dump of data by default. One use of this command is to display the
contents of a file when it contains non-printable characters. The point, however, is not to
use od to send secret messages. It also supports hexadecimal and decimal output. This can
be very useful when analyzing a precompiled binary, such as a virus:
Single Option Option with Argument Meaning

-a -t a Named characters, ignoring high bit


-b -t o1 Octal bytes
-c -t c ASCII characters or backslash escapes
-d -t u2 Unsigned decimal 2-byte units
-f -t fF Floats
-i -t dI Decimal integers
-l -t dL Decimal longs
-o -t o2 Octal 2 byte units
-s -t d2 Decimal 2 byte units
-x -t x2 Hexadecimal 2 byte units

tr: Translate, squeeze, and/or delete characters from standard input,


writing to standard output.

sed: is a non-interactive editor that can be used to modify text. When performing a search
and replace operation, the sed command will only replace the firstoccurrence of the pattern by
default.The sed command is also able to either insert text before a pattern or after a pattern.
also The command sed Place the pattern to search for between two slashes followed by a d

wc: this command can be used to analyze a text file. By default wc counts the number of
lines, words and byte.

103.3 Perform basic file management

YA ME LO SE DE MEMORIA

104.7 Find system files and place files in the correct location

locate: Command used to search for files by name. locate reads one or more databases
prepared by the updatedb utility and writes file names matching at least one of the PATTERNs
to standard output.

Updatedb: creates or updates a database to be used by the locate utility.

find: Command used to search for files in a directory hierarchy. find searches the directory
tree rooted at each given file name by evaluating the given the expression from left to right.

Which: Command that returns the pathnames of the files, or links, which would be executed
in the current environment. It does this by searching the PATH for executables matching the
names of the arguments.

Type: Command that indicates how a name would be interpreted if used as a command.
When using the type utility, the path to the command will be displayed.
Whereis: Command that is used to locate source/binary and manuals sections for specified
rules. This will locate binary, source, and manual pages for a command.

The Filesystem Hierarchy Standard (FHS) is an agreement to standardize the names and
locations of directories and their content for use within most Linux filesystems. the image and
table highlight some of the most important ones to know.

Directory Purpose

/ The root of the primary filesystem hierarchy.


/bin Contains essential user binary executables.
/boot Contains the kernel and bootloader files.
/dev Populated with files representing attached devices.
/etc Configuration files specific to the host.
/home Common location for user home directories.
/lib Essential libraries to support /bin and /sbin executables.
/mnt Mount point for temporarily mounting a filesystem.
/opt Optional third party add-on software.
/root Home directory for the root user.
/sbin Contains system or administrative binary executables.
/srv May contain data provided by services of the system.
/tmp Location for creating temporary files.
/usr The root of the secondary filesystem hierarchy.
/usr/bin Contains the majority of the user commands.
/usr/include Header files for compiling C-based software.
/usr/lib Shared libraries to support /usr/binand /usr/sbin.
/usr/local The root of the third filesystem hierarchy for local software.
/usr/sbin Non-vital system or administrative executables.
/usr/share Location for architecturally-independent data files.
/usr/share/dict Word lists.
/usr/share/doc Documentation for software packages.
/usr/share/info Information pages for software packages.
/usr/share/locale Locale information.
/usr/share/man Location for man pages.
/usr/share/nls Native language support files.
For security reasons, the FHS divides files based on 2 criteria:
Shareable / Unshareable

•Shareable files can be stored on one host and used on others. For
instance, /var/www is often used as the root directory of a web server, which shares
files with other hosts. Another example is the user home directories.

•Unsharable files should not be shared between hosts. These include process states in
the /var/run directory and the /boot directory.

Variable / Static

•Static files generally do not change, including library files and documentation pages.
An example is the info pages located at /usr/share/info.

•Variable files normally change during the execution of programs.


The /var/rundirectory contains files that are both variable and unshareable.

The table below summarizes the main distinctions between file types:
Shareable Unshareable

/usr /etc
Static
/opt /boot

/var/mail /var/run
Variable
/var/spool/news /var/lock

103.7 Search text files using regular expressions

Basic Regex
Meaning
Character(s)

. Any one single character


[] Any one specified character
[^ ] Not the one specified character
* Zero or more of the previous character
If first character in the pattern, then pattern must be at beginning of the line to match,
^
otherwise just a literal ^
Basic Regex
Meaning
Character(s)

If last character in the pattern, then pattern must be at the end of the line to match,
$
otherwise just a literal $

Extended Regex Character(s) Meaning

+ One or more of the previous pattern


? The preceding pattern is optional
{ } Specify minimum, maximum or exact matches of the previous pattern
| Alternation - a logical "or"
( ) Used to create groups

Vi editor

advantages to the vieditor:

•The vi editor is available on every Linux distribution in the world. This is not true of
any other editor.

•The vi editor can be executed both in a CLI and a GUI. While graphical editors,
like gedit from the Gnome desktop environment or kedit from K desktop environment,
are easier to use, they require a GUI, which servers won't always have running.

There are three modes used in vi: command mode, input mode, and ex mode

Command mode: is used to type commands, such as those used to move around a
document, manipulate text, and access the other two modes.

Command Mode Movement

The following table summarizes the motion keys available:


h Left one character

j Down one line

k Up one line

l Right one character

w One word forward

b One word back

^ Beginning of line
$ End of the line

[count] option

5h : move the cursor five characters to the left


3w: move the cursor three words to the right.

Command Mode Actions

Standard Vi Meaning

cut d delete
copy y yank
paste p put
the following general format for action commands is acceptable:

action [count] motion

Delete : Delete removes the indicated text from the page and saves it into the buffer,
dd : Delete current line
d3w : Delete the next three words

Yank : places content into the buffer without deleting it. Literally copy a text.
yy: Yank current line (copy the current line)

3yy: copy the next three lines

Put: places the text saved in the buffer either before or after the cursor position.
p: Put (paste) after cursor.

The vi program uses search. Search is more powerful than find because it supports both
literal text patterns and regular expressions.

/text Searches for text from the current cursor position forward.
?text Searches for text from the current cursor position backward.

Ex Mode
In order to get to the ex mode, type a : character in command mode.

:wq Writes the current file and quits.


:q! Quits the file without applying any changes. The ! forces the command to do its work.
Only add the ! if you really know what you are doing.
:w filename Writes the current file with a new filename.

vim command explanation


Esc Switches from input mode to command mode. Use this before typing any command.
i, a Switches from command mode to input mode at (i) or after (a) the
current cursor position.
o Opens a new line below t e current cursor position and goes to input mode.
:wq Writes the current file and quits.
:q! Quits the file without applying any changes. The ! forces the command to do its work.
Only add the ! if you really know what you are doing.
:w filename Writes the current file with a new filename.
dd Deletes the current line.
yy Copies the current line.
p Pastes the current selection.
v Enters visual mode, which allows you to select a block of text using the arrow keys. Use d to
cut, or y to copy the selection.
u Undoes the last command. Repeat as often as necessary.
Ctrl+r Redoes the last undo.
gg Goes to the first line in the document.
G Goes to the last line in the document.
/text Searches for text from the current cursor position forward.
?text Searches for text from the current cursor position backward.
^ Goes to the first position in the current line.
$ Goes to the last position in the current line.
!ls Adds the output of ls (or any other command) in the current file.
:%s/old/new/g Replaces all occurrences of old with new .

Redirects
> : will create a new file, or overwrite the contents of an existing file with the same name.
>> : appended to the end of a file. If the specified file doesn’t exist, it’s created.
2> : Creates a new file containing standard error. If the specified file exists, it’s overwritten.
2>> : Appends standard error to the existing file. If the specified file doesn’t exist, it’s created.
&> : Creates a new file containing both standard output and standard error. If the specified file exists,
it’s overwritten.
&>> : appends standard output and standard error to same file. If the specified file doesn’t exist, it’s
created.
< : Sends the contents of the specified file to be used as standard input.

tee: splits the output of a command into two streams: one directed to the terminal, and the
other into a file.

103.5 Create, monitor and kill processes

Daemons: are processes that provide services. They normally are started when
a computer is booted and often (but certainly not in all cases) they are running
with root privileges.

Das könnte Ihnen auch gefallen