Introduction
Mihai Lefter
• Simple tasks automation:
• Run a series of programs (commands) successively.
• Scripts = programs in their own right:
• Allow the use of real programming functions. • Pass arguments to commands.
• Reusable and scalable.
• Far more useful functions through command substitution. CAN YOU PASS
THE SALT?
I SAID
-I KNOW! -I'M DEVELOP-ING A SYSTEM TO PASS YOU ARBITRARY CONDIMENTS.
IT'S BEEN 20 MINUTES!
Basic Linux Concepts Recap • Terminal, shell, and kernel. • The filesystem:
• Special directories. • Absolute/relative paths.
Applications
Applications
cat cd mines sol >_ terminal User Operating System Hardware Kernel
Shell A program that helps runother programs. Command interpreter.
terminal
>_
User
Kernel
terminal
>_
User
Kernel
Shell IBM 1620 Model 1 console
(Wikipedia)
History:
a piece of equipment through which you could interact with a computer. teletypewriter - "tty".
terminal >_ User Kernel Shell History:
a piece of equipment through which you could interact with a computer. teletypewriter - "tty".
console - furniture point of view. terminal - electronic point of view:
terminal
>_
User
Kernel
Shell
Terminal, GNOME Terminal, XTerm, KDE Konsole, ...
History:
a piece of equipment through which you could interact with a computer. teletypewriter - "tty".
console - furniture point of view. terminal - electronic point of view.
Today:
terminal
>_
User
Kernel
Shell
An ordinary program, you can choose the one that you like. Examples:
sh
bash The Bourne-Again shell: can
be found installed and is the default interactive shell on most Linux systems. The basic shell: the original small program shell with few features, still used on UNIX systems.
csh Its syntax resembles that of the C programming language.
tcsh TENEX C shell: a superset of
the common C shell, enhancing user-friendliness and speed (Turbo C shell).
ksh The Korn shell: a superset of the Bourne shell (a nightmare for beginners).
zsh The Z shell: an extended Bourne shell including some features of ksh, and tcsh.
Everything is a file
Applications Applications
Special directories
bin home usr var
bash znew
bin games src
Documents Videos
2to3 zxpdf espdiff mines sol
/
zorro gulliver
The root directory marks the begining of the hierarchical filesystem. Sometimes referred to as the trunk, it is denoted by /.
The current (working) directory is the directory in which a user is working at a given time. Every user is always working within a directory.
file1 fileZ
Important
Absolute paths
For the root folder - signalizes an absolute path.
bin home var
bash znew
bin src
Documents Videos
2to3 zxpdf espdiff mines sol
/ zorro gulliver file1 fileZ Important start / / stop usr games
The path to mines from root is absolute:
Relative paths
bin var
bash znew
bin src
Videos
2to3 zxpdf espdiff mines sol
/ zorro gulliver file1 fileZ Important home Documents usr games stop start / /
The path to mines from important is relative:
Quiz 1 - Q1
bin home usr var
bash znew
bin games src
Documents Videos
2to3 zxpdf espdiff mines sol
/ zorro gulliver file1 fileZ Important stop start
What's the relative path to
Quiz 1 - Q1
bin home usr var
bash znew
bin games src
Videos
2to3 zxpdf espdiff mines sol
/
zorro gulliver
file1 fileZ
Important The path to ../Documents/Importantimportant from videos is:
Quiz 1 - Q2
bin home usr var
bash znew
bin games src
Documents Videos
2to3 zxpdf espdiff mines sol
/
zorro gulliver
file1 fileZ
Important
Quiz 1 - Q2
bin usr var
bash znew
bin games src
Documents Videos
2to3 zxpdf espdiff mines sol
/
zorro gulliver
file1 fileZ
Important The absolute path of /home/gulliver/Documents/file1file1 is:
Format
command[option]... [argument]...
Most input lines have three basic elements:
• command - executing program (application) name (path). • option(s)- modify what the command may do.
• argument(s)- what the command operates on.
Some notes:
• The commandmay be followed by one or more options.
• Options usually start with one or two dashes.
• Plenty commands have nooptions, noarguments, or neither.
Basic Commands Overview
Navigate through the filesystem
Prints the absolute path of the current/working directory. ls[option]...[directory]...
Lists information about thedirectoryin alphanumeric order.
If nodirectoryis specified theworking directoryis assumed.
cd[directory]
Navigate through the filesystem pwd[option]...
Prints the absolute path of the current/working directory. ls[option]...[directory]...
Lists information about thedirectoryin alphanumeric order.
If nodirectoryis specified theworking directoryis assumed.
cd[directory]
Basic Commands Overview
Manipulate files and directories
Create newdirectories.
cp[option]...[source]... [destination]
Copies thesourceto the destination.
mv[option]...[source]... [destination]
Move or renamesource todestination.
rm[options]...[file/directory]...
Remove specifiedfiles or directories.
Manipulate files and directories mkdir[option]..[directory]...
Create newdirectories.
cp[option]...[source]... [destination]
Copies thesourceto the destination.
mv[option]...[source]... [destination]
Move or renamesource todestination.
rm[options]...[file/directory]...
Remove specifiedfiles or directories.
Basic Commands Overview
Inspect files content
View the entire content of the inputfiles.
more[option]...[file]...
View thefile content one page at a time.
less[option]...[file]...
View thefile content with scrolling.
head[option]...[file]...
Print the first 10 lines of eachfile.
tail[option]...[file]...
Print the last 10 lines of eachfile.
head/tail-number[file]...
Inspect files content cat[option]...[file]...
View the entire content of the inputfiles.
more[option]...[file]...
View thefile content one page at a time.
less[option]...[file]...
View thefile content with scrolling.
head[option]...[file]...
Print the first 10 lines of eachfile.
tail[option]...[file]...
Print the last 10 lines of eachfile.
head/tail-number[file]...
Basic Commands Overview
Search and count
Outputs every path that contains the providedpattern.
find[path]...[option]...[expression]
Search for files and directories based on the givenexpression.
grep[option]...[pattern] [file/directory]...
Search text files for the pattern occurrence and output any line
containing a match. wc[option]...[file]...
Search and count
locate[option]...[pattern]
Outputs every path that contains the providedpattern.
find[path]...[option]...[expression]
Search for files and directories based on the givenexpression.
grep[option]...[pattern] [file/directory]...
Search text files for the pattern occurrence and output any line
containing a match. wc[option]...[file]...
Basic Commands Overview
Other useful commands
Summarize disk usage of the set offiles/directories.
echo[option]...[text]
Printtext to screen.
ssh[option]...[user@]hostname
Connect asuserto the hostname machine.
wget[option]...[url]...
Download file located aturl.
history
See the previous executed commands.
man[command]
Other useful commands
du[option]...[file/directory]...
Summarize disk usage of the set offiles/directories.
echo[option]...[text]
Printtext to screen.
ssh[option]...[user@]hostname
Connect asuserto the hostname machine.
wget[option]...[url]...
Download file located aturl.
history
See the previous executed commands.
man[command]
Quiz 2
Quiz 2
Quiz 2
What is the output of thegrep command from below?
$cat wines_list.txt
Chardonnay Sauvignon Blanc
Pinot Gris & Pinot Blanc Riesling & Gewrztraminer Cabernet sauvignon Zinfandel
Quiz 2
What is the output of thegrep command from below?
$cat wines_list.txt
Chardonnay Sauvignon Blanc
Pinot Gris & Pinot Blanc Riesling & Gewrztraminer Cabernet sauvignon Zinfandel
$grep ’Sauvignon’ wines_list.txt
• CLI:
• Nano: an easy to use text editor for both new and advanced
Linux users.
• Vi/Vim: one of the most popular among System
Administrators and programmers.
• Emacs: a highly extensible and customizable text editor.
• ...
• GUI:
• Gedit: a general purpose text editor installed by default on
Gnome.
• Kate: a feature rich and highly pluggable text editor that comes with KDE.
Filename expansion
$ # List all files $ls
DC_000100.jpgsssDocumentssssssssDownloadssssssssflower.jpg humansssssssssssgenessssssssssssMusicsssssssssssprograms ...
projectsssssssssTemplatessssssssVideosssssssssssZagreb.jpg
$ # List all JPEG files $ls *.jpg
Wildcards (meta-characters)
Characters that the shell will expand to match existing pathnames in the filesystem.
* Match zero or more characters.
? Match exactly one character.
[chars] Match any character listed in the brackets.
[!chars] Match any character that does not appear within the brackets.
[a-z] Match all characters between a and z, in a case-sensitive manner, based on the characters’ value in the ASCII character set.
Examples
$ # List all JPEG files with one character names $ls ?.jpg
$ # List all files starting with ’file’ and ending in a number. $ls file*[0-9]
How to escape wildcards
• Use the non-quoted backslash\.
• Use quotes - more on this in the next lecture.
$ls
flower.jpgsss*.jpgssssssssParis.jpgssssZagreb.jpg
$rm \*.jpg
$ls
Quiz 3
List all files starting with a small letter and ending in a number.
$
List all files that do not start with c, have r or L as second character, and end in a number.
$
List all files that have exactly 3 characters.
$
List all files that contain the * character.
Quiz 3
List all files starting with a small letter and ending in a number.
$ls [a-z]*[0-9]
List all files that do not start with c, have r or L as second character, and end in a number.
$ls [!c][rL]*[0-9]
List all files that have exactly 3 characters.
$ls ???
List all files that contain the * character.
• Makes it possible to control:
• where the output of a command goes to. • where the input of a command comes from.
Pipelining
• Redirect a command output as input to another command.
terminal
>_
command 1
output
Operators command> file
Redirectcommand output to the file.
Thefile is created or overwritten if already existing.
command>> file
Redirect and appendcommand output to the file.
Thefile is created if not existing.
command< file
Redirect input tocommand from fileinstead of stdin.
command 1|command 2
Example I
$ls
DocumentssssDownloads
$ls >list.txt;catlist.txt
Documents Downloads
$ls >>list.txt;catlist.txt
Example II
$ls
abba_poster.jpgsssdancing_queen.FLACssslist.txt ...
sos.mp3ssssssssssswaterloo.mp3 $ls |less
$ls |grep ’mp3’ >my_music_collection.txt
Going deeper - file descriptors command terminal >_ stdin stdout stderr File descriptors (FD) Handles used to access a file or
other input/output resource.
FD 0
FD 1
FD 2
command[file_description_number]redirection_operator file
• File description numbers implicit for stdin and stdout.
$cat file11> file2 # same as below
Standard error output
• Very popular Linux feature.
• Many commands provide great amounts of errors.
• Avoid messages cluttering up the normal program output while executing scripts.
$mv
mv: missing file operand
Try ’mv --help’for more information. $mv 2>error.log
$cat error.log
mv: missing file operand
Redirect both stdout and stderr to same file
$grep something somewhere> file_out 2>&1
In the above:
• stdout is redirected to file_out.
• stderr is redirected to stdout. Alternatives:
$grep something somewhere&> file_out
/dev/null- the black hole
• Everything written to it disappears.
• Attempts to read/output to/from it result in nothing.
$cat file> /dev/null
Contents of thefilewill not be listed to stdout.
$cat /dev/null>file
Deleting file contents, but preserving the file itself with all its
Quiz 4
Write the text “Hello world!” to file “hello.txt”:
$
Append the contents of “file1” and “file2” into “file3”:
$
Print the previous executed “ssh” commands:
Quiz 4
Write the text “Hello world!” to file “hello.txt”:
$echo ’Hello world!’> hello.txt
Append the contents of “file1” and “file2” into “file3”:
$cat file1 file2>> file3
Print the previous executed “ssh” commands:
Jeroen Laros Magnus Palmblad Jonathan Vis Mihai Lefter