Linux 1 Samenvatting

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 14

- Linux means the kernel of the system, which is the central controller of

everything that happens on the computer.


- Linux is a combination of software called GNU/Linux, which defines the operating
system.

- Operating System -> Software that runs on a computing device and manages the
hardware and software components that make up the system.

- Users today have a choice between three major operating systems:


Microsoft Windows
Apple macOS
Linux

- Linux software generally falls into one of three categories:


Server Applications: The purpose of this software is to serve information to
other computers, called clients.
Desktop Applications: Web browsers, text editors, music players, or other
applications with which users interact directly.
Tools: A loose category of software that exists to make it easier to manage
computer systems.

- Shells: -> Users interact with a Linux system through a shell, which accepts
commands to execute.

- Cloud computing allows computing resources to be moved to remote locations where


content can be accessed, manipulated and shared around the globe.

- Cloud adoption is the migration of an organization's IT applications and


processes to cloud services.

- A cloud can be described as computing resources from one or many off-site data
centers which can be accessed over the internet.

- The data and resources that organizations store in the cloud can include data,
servers, storage, application hosting, analytics and a myriad of other services.

- A cloud deployment model provides a basis for how cloud infrastructure is built,
managed, and accessed.
There are four primary cloud deployment models:
Public Cloud: A cloud infrastructure deployed by a provider to offer cloud
services to the general public and organizations over the Internet.
Private Cloud: A cloud infrastructure that is set up for the sole use of a
particular organization.
Community Cloud: A cloud infrastructure that is set up for the sole use by a
group of organizations with common goals or requirements.
Hybrid Cloud: A hybrid cloud is composed of two or more individual clouds,
each of which can be private, community, or public clouds.

- Open Source Philosophy -> Philosophy that users have the right to obtain the
software source code and modify it for their own use.

- Licenses without copyleft are called permissive.

Module 5 Command Line Skills

example: sysadmin@localhost:~$
- The prompt shown contains the following information:
Username (sysadmin)
System name (localhost)
Current Directory (~)

-The typical format for a command is as follows: command [options] [arguments]

- Options are used to modify the core behavior of a command.


- Arguments are used to provide additional information (such as a filename or a
username).

ls: lists content of directory.


ls -l: results in a long listing. providing additional info.
ls -a: lists all files also the hidden ones.
history: entire history list of terminal.(you see all the commands you typed)
HISTSIZE -> staat voor historysize.
PATH -> The PATH variable lists all the places that the system can look for
programs to execute.
env -> outputs a list of the environment variables. example: env | grep variable1
export -> used to turn a local variable into an environment variable. example:
export variable1
unset -> removing exported variables
cd -> change directory
which -> searches for the location of a command by searching the PATH variable.
man -> manual command
whereis -> search for the location of a command or the man pages for a command.
example: whereis ls
locate -> to find any file or directory
locate -c -> lists how many files match
locate -b -> only includes listings that have the search term in the basename of
the filename.
info -> also provides documentation on operating system commands and features.
--help -> provides basic information
pwd -> can be used to determine where the user is currently located within the
filesystem.
ls -lh -> presents the file size in a more human readable size
ls -d -> refers to the current directory.
ls -ld -> to use the -d command in a more meaningful way requires the addition of
the -l.
ls -r -> perform a recursive listing.
ls -S of ls -lS -> sort files by size.
ls -t -> sorts files based on the time they were modified.
--full-time -> display the complete timestamp (including hours, minutes, seconds).
example: ls -t --full-time /etc/ssh
cp -> used to copy files. example: cp [source] [destination] example: cp /etc/hosts
~
cp -i -> With the -i (interactive) option, the cp will prompt before overwriting a
file (y (yes) or n (no)):
mv -> to move a file
mv -i -> Interactive: Ask if a file is to be overwritten.
mv -n -> No Clobber: Do not overwrite a destination files' contents.
mv -v -> Verbose: Show the resulting move
touch -> create an empty file
mkdir -> create an directory
gzip -> used to compress files. example: sysadmin@localhost:~/Documents$ gzip
longfile.txt
gzip -> shows info about the compressed file.
gunzip -> restoring compressed files. example: sysadmin@localhost:~/Documents$
gunzip longfile.txt.gz
gzip -d -> restoring compressed files.
tar -cf ARCHIVE -> Create an archive. Use archive file. The argument ARCHIVE will
be the name of the resulting archive file.
tar cfz -> Tarballs can be compressed for easier transport, either by using gzip on
the archive or by having tar do it with the -z option example:
sysadmin@localhost:~/Documents$ tar -czf alpha_files.tar.gz alpha*
tar tjf -> Given a tar archive, compressed or not, you can see what’s in it by
using the -t option. The next example uses three options:
sysadmin@localhost:~/Documents$ tar -tjf folders.tbz meer uitleg: The example lists
the contents of the folders.tbz archive:
tar -x -> extract the archive. example: sysadmin@localhost:~/Documents$ tar -xjf
folders.tbz
zip -> add files to archive and compress it
unzip -> decompress archives
cat -> used to display files or used to redirect file content to other file.
head -> used to display the first lines of a file
tail -> used to display only the last few lines of a file.
more or less -> to view larger files.
sort -> can be used to rearrange the lines of files or input in either dictionary
or numeric order.
wc -> command provides the number of lines, words and bytes (1 byte = 1 character
in a text file) for a file, and a total line count if more than one file is
specified.

Options can be used in conjunction with other options:


Options are often single letters; however, sometimes they are words or phrases as
well.
Typically, older commands use single letters while newer commands use complete
words for options.
Single-letter options are preceded by a single dash - character, like the
-h option.
Full-word options are preceded by two dash -- characters like the full-word form of
the -h option, the --human-readable option

- To execute the most recent command type !! and hit Enter.


- To execute the most recent iteration of a specific command, type !command and hit
Enter.

- A variable is a feature that allows the user or the shell to store data.
Variables are given names and stored temporarily in memory.
There are two types of variables used in the Bash shell, local and environment.
Local or shell, variables exist only in the current shell. When the user closes a
terminal window or shell, all of the variables are lost.

To set the value of a variable, use the following assignment expression ->
variable=value -> variable1='Something'
To display the value of the variable, use a dollar sign $ character followed by the
variable name as an argument to the echo command:
echo $variable1 display= Something

Environment variables, also called global variables, are available system-wide.


Examples include the PATH, HOME, and HISTSIZE variables.

One of the most important Bash shell variables to understand is the PATH variable.


The PATH variable lists all the places that the system can look for programs to
execute.
- The type command can be used to determine information about command type.
There are several different sources of commands within the shell of your CLI:
Internal commands
External commands
Aliases
Functions

Internal Commands -> Also called built-in commands, these commands are built into
the shell itself.
A good example is the cd  (change directory) command as it is part of
the Bash shell.

External Commands -> External commands are stored in files that are searched by the
shell.

Aliases -> An alias can be used to map longer commands to shorter key sequences.
For example, the command ls -l is commonly aliased to l or ll.

Functions can also be built using existing commands to:


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.

- double quotes -> Double quotes stop the shell from interpreting some


metacharacters, including glob characters like (*, ?).
Double quotes still allow for command substitution, variable substitution, and
permit some other shell metacharacters (i.e., the PATH variable)

- backslash character -> A technique to essentially single quote a single character


is to use the backslash character \.
example: echo The service costs \$1 and the path is $PATH.

backquotes -> Backquotes, or backticks, are used to specify a command within a


command, a process called command substitution.
example: sysadmin@localhost:~$ echo Today is `date`
display: Today is Mon Nov 4 03:40:04 UTC 2030

- Control Statements:
Control statements include:
Semicolon (;)
Double ampersand (&&)
Double pipe (||)

- The semicolon can be used to run multiple commands, one after the other: example:
cal 1 2015; cal 2 2015; cal 3 2015
- The double ampersand && acts as a logical "and" if the first command is
successful, then the second command (to the right of the &&) will also run:
example:sysadmin@localhost:~$ ls /etc/xml && echo success
- The double pipe || is a logical "or". It works similarly to &&; depending on the
result of the first command, the second command will either run or be skipped:
example: sysadmin@localhost:~$ ls /etc/junk || echo failed

Module 6

- To view a manual page for a command use the 'man' command. example: man ls
- By default there are nine default sections of man pages:
General Commands
System Calls
Library Calls
Special Files
File Formats and Conventions
Games
Miscellaneous
System Administration Commands
Kernel Routines

- Finding Commands and Documentation


To search for the location of a command or the man pages for a command, use
the 'whereis' command.
Man pages are excellent sources of information, but they do tend to have a few
disadvantages:
Each man page is a separate document, not related to any other man page.
Man pages may can be difficult to read.
The info command also provides documentation on operating system commands and
features.
Info documentation as a logical organizational structure, making reading
documentation easier.
Information is broken down into categories that work much like a table of contents
in a book.
Consider man pages to be more of a reference resource and info documents to be more
of a learning guide.

- Many commands will provide basic information, very similar to the SYNOPSIS found


in man pages, by simply using the --help option to the command.
example: ps --help

Module 7

In Linux, everything is stored in files.


- Files are used to store data such as text, graphics, and programs.
- Directories are a type of file used to store other files.
- Directories are used to provide a hierarchical organization structure.

- To view the contents of the root directory, use the 'ls' command with the '/'
character as the argument:

- The 'pwd' command prints the working directory, which is the current location of
the user within the filesystem.

- A path is a list of directories separated by the '/' character


There are two types of paths: absolute and relative.

- Absolute paths allow the user to specify the exact location of a directory.
Absolute paths always starts at the root directory, and therefore it always begins
with the / character.
The path /home/sysadmin is an absolute path; it tells the system to:
Begin at the root / directory > move into the home directory > then into the
sysadmin directory

- A relative path gives directions to a file relative to the current location in


the filesystem.
The user must currently be in a directory that contains objects in the path
Relative paths start with the name of a directory.
Listing Files in a Directory

- The ls (list) command is one of the most powerful tools for navigating the
filesystem.
- the ls command omits hidden files by default.
- to display all files, including hidden files, use the '-a' option to the 'ls'
command.
- The following describes each of the fields of data in the output of the ls -l 
command:
example: -rw-r--r-- 1 root root 17869 Mar 14 17:48 alternatives.log

drwxr-x--- 2 root adm 4096 Mar 14 17:48 apache2

The first character of each line indicates the type of file. The file types are:
- the next nine characters demonstrate the permissions of the file.
kijk voor meer informatie naar hoofdstuk 7.
Symbol File Type Description
d directory A file used to store other files.
- regular file Includes readable files, images files, binary
files, and compressed files.
l symbolic link Points to another file.
s socket Allows for communication between processes.
p pipe Allows for communication between processes.
b block file Used to communicate with hardware.
c character file Used to communicate with hardware.

- To present the file size in a more human readable size, like megabytes or
gigabytes, add the -h option (with the -l option) to the ls command:
example: ls -lh /usr/bin/omshell display: -rwxr-xr-c 1 root root 1.5M Oct 9
2012 /usr/bin/omshell

Recursive Listing

- Recursive listing is when you want to display all of the files in a directory as
well as all of the files in all subdirectories under a directory.
To perform a recursive listing, use the -R option to the ls command:

Sort a listing
- By default, the -ls command sorts files alphabetically by file name. Sometimes it
may be useful to sort files using different criteria.
To sort files by size, we can use the -S option (capital letter s). example: ls -lS
/etc/ssh

Module 8 Managing Files and Directories


Globbing

- Glob characters are often referred to as wild cards.


Globs are powerful because they allow you to specify patterns that match filenames
in a directory:
Instead of manipulating a single file at a time, you can easily execute commands
that will affect many files.

- The asterisk character is used to represent zero or more of any character in a


filename.
example: echo /etc/t display: /etc/terminfo /etc/timezone

The question mark character matches exactly one character, no more and no less
Suppose you want to display all of the files in the /etc directory that begin with
the letter t and have exactly 7 characters after the t character:
example: :~$ echo /etc/t??????? display: /etc/terminfo
/etc/timezone

- The asterisk and question mark could also be used together to look for files with
three-letter extensions by running the echo /etc/*.??? command:
display: /etc/blkid.tab /etc/issue.net

- Brackets are used to match a single character by representing a range of


characters that are possible match characters.
For example, echo /etc/[gu]* will print any file that begins with either
a g or u character and contains zero or more additional characters:
display: /etc/gai.conf /etc/groff /etc/group /etc/group- /etc/gshadow /etc/gshadow-
/etc/ucf.conf /etc/udev /etc/ufw /etc/update-motd.d /etc/updatedb.conf

Brackets can also be used to a represent a range of characters by using the -


character (i.e., any letter between and including a and d):
example: sysadmin@localhost:~$ echo /etc/[a-d]*

- The exclamation point is used in conjunction with the square brackets to negate a
range.
For example, the command echo /etc/[!a-t]* will display any file that does
not begin with an a thru t:
display: /etc/ucf.conf /etc/udev /etc/ufw /etc/update-motd.d /etc/updatedb.conf
/etc/vim #/etc/wgetrc /etc/xml

Copying files and directories

- The cp command is used to copy files. It requires a source and a destination


The source is the file to be copied. The destination is where the copy is to be
located.
- The -v option will cause the cp command to produce output if successful.
The -v option stands for verbose.
An example of the -v option used with the cp command:
display: `/etc/hosts' -> `/home/sysadmin/hosts'

- When the destination is a directory, the resulting new file will have the same
name as the original file.
- If you want the new file to have a different name, you must provide the new name
as part of the destination.
- Using the 'cp' command to copy directories will result in an error message
- However, the -r (recursive) option to the cp command will have it copy both files
and directories.
- Be careful with this option. The entire directory structure will be copied. This
could result in copying a lot of files and directories!

Moving files
- To move a file, use the mv command.
The syntax for the mv command is much like the cp command: mv [source]
[destination]
When a file is moved, the file is removed from the original location and placed in
a new location.
Note: If you don't have the right permissions, you will receive a "Permission
denied" error message.

- The mv command is not just used to move a file, but also to rename a file.
- The name of the file will change only if a destination file name is also
specified.
If a destination directory is not specified, the file will be renamed using the
destination file name and remain in the source directory.
- For example, the following commands will rename the newexample.txt file
to myexample.txt:
exampe: sysadmin@localhost:~/Videos$ mv newexample.txt myexample.txt

- Like the cp command, the mv command provides the following options:


-i -> Interactive: Ask if a file is to be overwritten.
-n -> No Clobber: Do not overwrite a destination files' contents.
-v -> Verbose: Show the resulting move
Important: There is no -r option as the mv command will by default move
directories.

Creating files
- To create an empty file, use the touch command as demonstrated below
sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates
Videos
sysadmin@localhost:~$ touch sample
sysadmin@localhost:~$ ls -l sample
-rw-rw-r-- 1 sysadmin sysadmin 0 Nov 9 16:48 sample
sysadmin@localhost:~$ ls
Desktop Downloads Pictures Templates sample
Documents Music Public Videos

Creating Directory
- To create a directory, use the mkdir command:
sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates
sample.txt
sysadmin@localhost:~$ mkdir test
sysadmin@localhost:~$ ls
Desktop Downloads Pictures Templates test
Documents Music Public sample.txt

Deleting Files
To delete a file, use the rm command:
sysadmin@localhost:~$ ls
Desktop Downloads Pictures Templates sample
Documents Music Public Videos
sysadmin@localhost:~$ rm sample
sysadmin@localhost:~$ ls


Desktop Documents Downloads Music Pictures Public Templates
Videos

Deleting Directories
- The rm command can be used to delete directories. However, the default usage (no
options) of the rm command will fail to delete a directory:
To delete a directory, use the -r (recursive) option to the rm command:
example: sysadmin@localhost:~$ rm -r Videos

Module 9

- File archiving is used when one or more files need to be transmitted or stored as
efficiently as possible.
There are two fundamental aspects which this chapter explores:
Archiving: Combines multiple files into one, which eliminates the overhead in
individual files and makes it easier to transmit.
Compression: Makes the files smaller by removing redundant information.

-Compression reduces the amount of data needed to store or transmit a file while
storing it in such a way that the file can be restored.
The compression algorithm is a procedure the computer uses to encode the original
file, and as a result, make it smaller.

When talking about compression, there are two types:


- Lossless: No information is removed from the file.
- Lossy: Information might be removed from the file.

Linux provides several tools to compress files, the most common is gzip. Here we
show a file before and after compression:
sysadmin@localhost:~/Documents$ ls -l longfile*
-rw-r--r-- 1 sysadmin sysadmin 66540 Dec 20 2017 longfile.txt
sysadmin@localhost:~/Documents$ gzip longfile.txt
sysadmin@localhost:~/Documents$ ls -l longfile*
-rw-r--r-- 1 sysadmin sysadmin 341 Dec 20 2017 longfile.txt.gz

The original size of the file called longfile.txt is 66540 bytes.


The file is compressed by invoking the gzip command with the name of the file as
the argument.
After that command completes, the original file is gone, and a compressed version
with a file extension of .gz is left in its place.
The file size is now 341 bytes.

Compressed files can be restored to their original form (decompression) using


either the gunzip command or the gzip –d command.
After gunzip does its work, the longfile.txt file is restored to its original size
and file name.

- Archiving is when you compress many files or directories into one file.
The traditional UNIX utility to archive files is called tar, which is a short form
of TApe aRchive.
Tar has three modes that are helpful to become familiar with:
Create: Make a new archive out of a series of files.
Extract: Pull one or more files out of an archive.
List: Show the contents of the archive without extracting.

Creating an archive with the tar command requires two named options:
-c -> Create an archive
-f ARCHIVE -> Use archive file. The argument ARCHIVE will be the name of the
resulting archive file.

The following example shows a tar file, also called a tarball, being created from
multiple files:
sysadmin@localhost:~/Documents$ tar -cf alpha_files.tar alpha*
sysadmin@localhost:~/Documents$ ls -l alpha_files.tar
-rw-rw-r-- 1 sysadmin sysadmin 10240 Oct 31 17:07 alpha_files.tar

Given a tar archive, compressed or not, you can see what’s in it by using the -
t option. The next example uses three options:
-t -> List the files in the arhive
-j -> Decompress with the bzip2 command.
-f ARCHIVE -> Operate on the given archive.

The following example lists the contents of the folders.tbz archive:


sysadmin@localhost:~/Documents$ tar -tjf folders.tbz

ZIP Files
- The ZIP file is the default archiving utility in Microsoft.
ZIP is not as prevalent in Linux but is well supported by the zip and
unzip commands.

Module 10 Working with Text


- The 'cat' command is a useful command that can be used to create and display text
files, as well as combining copies of text files.
To display a file using the cat command type the command followed by the filename:
sysadmin@localhost:~/Documents$ cat food.txt display: Food is good.

-The cat command can also be used to redirect file content to other files or input
to another command by using redirection characters.
The cat command is good for viewing small files but is not ideal for large files.

- To view larger files, use a pager command


- There are two commonly used pager commands:
The less command provides advanced paging capability but is not included with all
Linux distributions.
The more command has fewer features than less, but is available in Linux
distributions.
The more and less commands allow users to move around a document using keystroke
commands.

To view a file with the less command, pass the filename as an argument
example: sysadmin@localhost:~/Documents$ less words

- The head and tail commands are used to display only the first or last few lines
of a file.

Command Line Pipes


- The pipe '|' character can be used to send the output of one command to another.
The pipe character allows you to utilize the head and tail commands not only on
files, but on the output of other commands.
For example, some commands have large amount of output.
To more easily view the beginning of the output, pipe it to the head command. The
following example displays only the first ten lines:
sysadmin@localhost:~$ ls /etc | head

Input/Output Redirection
- Input/Output (I/O) redirection allows for command line information to be passed
to different streams.
The streams are:
STDIN: Standard input, or STDIN, is information entered normally by the user via
the keyboard.
STDOUT: Standard output, or STDOUT, is the normal output of commands.
STDERR: Standard error, or STDERR, are error messages generated by commands.
I/O redirection allows the user to redirect STDIN so that data comes from a file
and STDOUT/STDERR so that output goes to a file.
Redirection is achieved by using the arrow < > characters.
- The standard output of a command will display to the screen:
sysadmin@localhost:~$ echo "Line 1" display: Line 1

- Using the > character, the standard output of a command can be redirected to a


file instead:
sysadmin@localhost:~$ echo "Line 1" > example.txt

- The example.txt file contains the output of the echo command, which can be viewed
with the cat command:
sysadmin@localhost:~$ cat example.txt display: Line 1

STDOUT(StandardOutput)
- The single arrow overwrites any contents of an existing file.
- It is possible to preserve the contents of an existing file using
the >> characters, which append to a file instead of overwriting it.
sysadmin@localhost:~$ echo "New line 1" > example.txt
sysadmin@localhost:~$ cat example.txt
Line 1
sysadmin@localhost:~$ echo "Another line" >> example.txt
sysadmin@localhost:~$ cat example.txt
New line 1
Another line

meer informatie in module 10.

Sorting Files For Input


- The sort command can be used to rearrange the lines of files or input in either
dictionary or numeric order.
- The sort command can rearrange the output based on the contents of one or more
fields.
Fields are determined by a field delimiter contained on each line.
bin:x:2:2:bin:/bbin:x:2:2:bin:/bin:/bin/sh

- For example, the following command can be used to sort the third field of the
mypasswd file numerically.
The -t option will allow for another field separator to be specified
To specify which field to sort by, use the -k option with an argument to indicate
the field number
The -n option is used to perform a numeric sort.
sysadmin@localhost:~$ sort -t: -n -k3 mypasswd

Viewing File statistics


- The wc command provides the number of lines, words and bytes (1 byte = 1
character in a text file) for a file,
and a total line count if more than one file is specified.
sysadmin@localhost:~$ wc /etc/passwd /etc/passwd-
35 56 1710 /etc/passwd
34 55 1665 /etc/passwd-
69 111 3375 total

It is also possible to view only specific statistics:


Use the  -l option to show just the number of lines
The -w option to show just the number of words
The -c option to show just the number of bytes, or any combination of these options

Filtering Files
- The cut command can extract columns of text from a file or standard input
The cut command is used for working with delimited files—which contain columns
separated by a delimiter.
By default, the cut command expects its input to be separated by the Tab character,
but the -d option can specify alternative delimiters such as the colon or comma.
The -f option can specify which fields to display.
The -c option is used to extract columns of text based upon character position.
In the following example, the first, fifth, sixth and seventh fields
from mypasswd database file are displayed:
sysadmin@localhost:~$ cut -d: -f1,5-7 mypasswd

- The'grep' command can be used to filter lines in a file or the output of another


command that matches a specified pattern:
sysadmin@localhost:~$ grep --color bash /etc/passwd
root:x:0:0:root:/root:/bin/bash
sysadmin:x:1001:1001:System Administrator,,,,:/home/sysadmin:/bin/bash

- The grep command can be used with several options to filter lines:
The -d option can specify alternative delimiters such as the colon or comma
by default, the cut command expects its input to be separated by the Tab character
The -f option can specify which fields to display.
The -c option is used to extract columns of text based upon character position.

Basic Regular Expressions


- Basic Regular Expressions include the following:
. -> Any single character
[] -> A list or range of characters to match one character. If the first character
is the caret ^, it means any character not in the list
* -> The previous character repeated zero or more times
^ -> If the first character in the pattern, the pattern must be at the beginning
of the line to match, otherwise just a literal ^
$ -> If the last character in the pattern, the pattern must be at the end of the
line to match, otherwise just a literal $

-The grep command is just one of the many commands that support regular


expressions. Some other commands include the more and less commands.

- The . character matches any character except for the new line character.
The pattern r..f would find any line that contained the letter r followed by
exactly two characters and then the letter f:
sysadmin@localhost:~/Documents$ grep 'r..f' red.txt
reef
roof

- The square brackets [] match a single character from the list or range of
possible characters contained within the brackets:
sysadmin@localhost:~/Documents$
grep '[0-9]' profile.txt
I am 37 years old.
3121991
I have 2 dogs.
123456789101112

- Each possible character can be listed out [abcd] or provided as a range [a-d], as


long as the range is in the correct order.
- To match a character that is not one of the listed characters, start the [] set
with a ^ symbol.

Extended Regular Expressions


- The use of extended regular expressions often requires a special option be
provided to the command to recognize them.
The -E option to the grep command can understand extended regular expressions.
The following regular expressions are considered extended:

? -> Matches previous character zero or one time, so it is an optional character


+ -> Matches previous character repeated one or more times
| -> Alternation or like a logical or operator

Module 12 Understanding Computer Hardware


-

You might also like