Lecture Linux
Lecture Linux
Jens Saak
Computational Methods in Systems and Control Theory (CSC) Max Planck Institute for
Dynamics of Complex Technical Systems
Linus ([email protected])
PS. Yes - it’s free of any minix code, and it has a multi-threaded fs. It is NOT
protable (uses 386 task switching etc), and it probably never will support anything
other than AT-harddisks, as that’s all I have :-(. ”
After this the (r)evolution has been fast as the following time-line shows.
Unix/Linux history (following Wikipedia)
A short History of an Accidental Revolution
Unix/Linux history (following Wikipedia) http://en.wikipedia.org/wiki/History_of_Linux
1983 Richard Stallman creates the GNU project with the goal of
creating a free operating system.
1989 Richard Stallman writes the first version of the GNU
General Public License.
1991 The Linux kernel is publicly announced by the 21 year old
Finnish student Linus Benedict Torvalds.
1992 The Linux kernel is relicensed under the GNU GPL. The
first so called “Linux distributions” are created.
1993 Over 100 developers work on the Linux kernel. With their
assistance the kernel is adapted to the GNU environment,
which creates a large spectrum of application types for
Linux. The oldest currently existing Linux distribution,
Slackware, is released for the first time. Later in the same
year, the Debian project is established. Today it is the
largest community distribution.
A short History of an Accidental Revolution
Unix/Linux history (following Wikipedia) http://en.wikipedia.org/wiki/History_of_Linux
2> same as above for the error output only, can be used to redirect
the standard error messages to standard output so it is recognized
by the > and | as well via 2>&1
1> same as above for the standard output without the errors
>> as > but appends the output instead of overwriting the file
$ used in command substitution and for referring to shell and envi-
ronment variables
& a single & after a command name sends the execution to the
background. Double && stand for the logic AND.
bash and its Basic Helpers
Special Characters
Environment Variables
▶ runtime settings are stored in so called environment variables
▶ referencing them is done using $
▶ some are defined by the system standards
▶ users can invent their own variables for storing information, e.g. in
script files
▶ names usually in all capital letters
bash and its Basic Helpers
Influencing the Working Environment
Environment Variables
▶ runtime settings are stored in so called environment variables
▶ referencing them is done using $
▶ some are defined by the system standards
▶ users can invent their own variables for storing information, e.g. in
script files
▶ names usually in all capital letters
Example
▶ $USER name of the user running the shell
▶ $HOME file system location of the users home directory
▶ $PATH search path for executable binaries
▶ $EDITOR default text editor, $PRINTER standard printer,
$HOSTNAME computers name, . . .
Basic Commands for Managing Files
bash and its Basic Helpers
Basic Commands for Managing Files
pwd short for print working directory, and printing the name of
the directory you are currently working in is exactly what
it does.
cd change directory, switches the current working directory to
the directory given as the argument. If no argument is
given cd takes you home, i.e., switches to your users
home directory.
mkdir creates a new directory in the current working directory
rmdir removes the directories specified as arguments if they are
empty.
touch creates an empty file, or, if it already exists, sets the
access date of the file to the current time and date
rm removes files. It can also be used to remove directories
with the -r (recursive) option. This is especially useful
when rmdir does not work since the directory is not
empty. The -f (force) option can be used to remove even
protected files.
bash and its Basic Helpers
Basic Commands for Managing Files
The pipe operator | in the Linux shell can be employed to directly use
the output of one program as the input for another one. A statement of
the form
program1 | program2
can be used if program1 writes its output to the standard output and
program2 reads its input from the standard input.
Manipulation of Simple Commands
The Pipe Operator
The pipe operator | in the Linux shell can be employed to directly use
the output of one program as the input for another one. A statement of
the form
program1 | program2
can be used if program1 writes its output to the standard output and
program2 reads its input from the standard input.
Problem
Often programs expect a number of input arguments to be supplied and
inputs are not read from user interactions via the standard input device.
Example
Remove all .pdf files from the working directory and its subdirectories.
▶ find produces the list of .pdf files.
▶ rm expects a list of files as arguments, not from user input.
Manipulation of Simple Commands
The Pipe Operatorand the xargs utility
Example
Remove all .pdf files from the working directory and its subdirectories.
▶ find produces the list of .pdfs.
▶ rm expects a list of files as arguments not from user input.
Solution
xargs can be used to split a list from standard input into several
arguments for another program.
find . -name '*.pdf' | xargs -n 1 -P 4 -d '\n' rm
Output Redirection
program1 > output.txt
Output Redirection
program1 > output.txt
Output Redirection
program1 > output.txt
can be used to simply append the new data. If output.txt does not
exist it is created.
Manipulation of Simple Commands
Redirection Operators: <
Input Redirection
To stream the input from a file input.txt instead of the standard
input we write
program1 < input.txt
or
program1 <input.txt >>output.txt
Manipulation of Simple Commands
Redirection Operators: Splitting Messages and Errors
There are two special variants of the output operator that allow to
separate between standard outputs and error messages.
program 1>output 2>errors
A common application:
find / -name search_expression 2>/dev/null
Example
Both
echo Yeah, today is `date`, the term is almost over!
echo Yeah, today is $(date), the term is almost over!
result in
Yeah, today is Thu Oct 11 14:33:35 CEST 2016, the term \
→ is almost over!
Script File Basics
Script files
Script File Basics
Script Files
If the shell is told to execute a text file it simply interprets the content as
shell command unless the file starts with the expression \#! followed by
the full path to an executable.
writes
Hello World!
Regular Expression
▶ strings that can be used to establish complex search and replace
operations on other strings.
▶ consist of a combination of special and basic characters
▶ used to match (specify and recognize) substrings in a string.
Examples
grep Time logfile
If you are not sure whether Time was written with capital T you can use
grep -i Time logfile
Recursive Operation
In the case you do not remember which file in your large software project
contains the definition of a certain function you can have grep search a
complete directory recursively
grep -r function-name *
Output Negation
You can also negate the output of grep by the switch -v to suppress
printing of all lines that match the pattern.
sed
Simple Automatic File Manipulation
sed
sed the Stream Editor is a basic text editor that in contrast to the usual
text editors (like vi, emacs, nano, . . . ) is not interactive but uses
certain command strings to manipulate the text file streamed into it
automatically without user interaction.
Example Scenario
sed is especially useful when, e.g., a variable or function (or any other
identifier) in a large software project is supposed to be renamed. Consider
the name of variable called complicatedname is to be replaced by
simplename for better readability of the code in a large C project.
Simple Automatic File Manipulation
sed
To complete the picture we can use find to search for all .c and .h
files and execute the above line for every single one of them.
find . -name '*.[ch]' -exec sed -i 's/complicatedname/\
→ simplename/g' {};
Simple Automatic File Manipulation
sed
To complete the picture we can use find to search for all .c and .h
files and execute the above line for every single one of them.
find . -name '*.[ch]' -exec sed -i 's/complicatedname/\
→ simplename/g' {};
Simple Automatic File Manipulation
sed
Inplace Backups
sed -i.orig 's/foo/bar/4' filename.txt
we can use
sed 10q file
as well.
Simple Automatic File Manipulation
sed
can be written as
sed -n '/foo/p' file
1 https://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html
Simple Automatic File Manipulation
awk
awk programs
▶ Basic format of a rule:
Condition { Action }
Expressions
Expression can be one of
▶ column specifier
▶ numeric value
▶ a string enclosed by double quotes
Operators
Operators are the usual comparison or assignment operators
▶ ==, !=,
▶ <, >,
▶ +=, -=,
▶ ...
Simple Automatic File Manipulation
awk
All rows where the third column is larger than one are returned by
cat file | awk '$3>1.0 { print $0; }'
Simple Automatic File Manipulation
awk
changes to
cat file | awk 'BEGIN{FS="|";} $3>1.0 { print $0;}'
Working on remote machines
Secure Shells and file Transfers
Working on remote machines
Secure Shells and file Transfers
Classically rsh, rlogin were used. They are deprecated. Do not use!
Secure Shell ssh
▶ remote shell with modern security and encryption features