Linux 1 Samenvatting
Linux 1 Samenvatting
Linux 1 Samenvatting
- Operating System -> Software that runs on a computing device and manages the
hardware and software components that make up the system.
- Shells: -> Users interact with a Linux system through a shell, which accepts
commands to execute.
- 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.
example: sysadmin@localhost:~$
- The prompt shown contains the following information:
Username (sysadmin)
System name (localhost)
Current Directory (~)
- 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
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.
- 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
Module 7
- 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.
- 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
- 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
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
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
- 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
- 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
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.
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
- 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.
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.
-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 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.
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
- 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
- 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
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 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.
- 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