01 Intro to Linux and bash (1)
01 Intro to Linux and bash (1)
[1
2
]
WHAT EXACTLY DOES A SYSADMIN
DO?
System administration in a larger company, these may all
be separate positions within a computer support or
Information Services department.
IT Support
Database administrator (DBA)
Network administrator
Security administrator
Web administrator
Computer operator
SRE Site Reliability Engineer
[1
3
]
SYSADMIN’S DUTIES
Analyzing system logs and identifying potential issues with computer systems.
Applying operating system updates, patches, and configuration changes.
Installing and configuring new hardware and software.
Adding, removing, or updating user account information, resetting passwords,
etc.
Answering technical queries and assisting users.
Responsibility for security.
Responsibility for documenting the configuration of the system.
Troubleshooting any reported problems.
System performance tuning.
Ensuring that the network infrastructure is up and running.
Configuring, adding, and deleting file systems.
Ensuring parity between dev, test and production environments.
Training users
Plan and manage the machine room environment
[1
4
]
[2
5
]
[3] 6
[3] 7
IMPORTANT NOTES ON LINUX
Linux is a kernel, not OS.
Linux is not a UNIX clone, it was written from scratch.
A Linux distribution is the Linux kernel and a collection of
software that together creates an OS.
Why Linux is so popular?
Open Source
Lightweight
Secure
Multiuser – Multitask
Simplified update for all installed software
Multiple distributions ( RedHat, Debian, etc)
[4
8
]
LINUX HISTORY
1991: The Linux kernel is publicly announced on 25 August
by the 21-year-old Finnish student Linus Benedict Torvalds.
[5 9
]
LINUX HISTORY
1992: The Linux kernel is relicensed under the GNU GPL
which means Linux became Open Source. The first Linux
distributions are created.
1993: Over 100 developers work on the Linux kernel. The
oldest currently existing Linux distribution, Slackware, is
released for the first time. Later in the same year, the
Debian project is established.
1994: The XFree86 project contributes a graphical user
interface (GUI). Commercial Linux distribution makers Red
Hat and SUSE publish version 1.0 of their Linux distributions.
1995: Linux is ported to the DEC Alpha and to the Sun
SPARC.
1996: Version 2.0 of the Linux kernel is released. The kernel
can now serve several processors at the same time using
symmetric multiprocessing.
[5,6 10
]
LINUX HISTORY
1998: Many major companies such as IBM, Compaq and
Oracle announce their support for Linux. In addition, a
group of programmers begins developing the graphical user
interface KDE.
1999: A group of developers begin work on the graphical
environment GNOME, destined to become a free
replacement for KDE.
GNOME has been crucial to the spread of desktop Linux. It has
given us a number of programs that we use today on desktop
Linux.
2000: Dell announces that it is now the No. 2 provider of
Linux-based systems worldwide and the first major
manufacturer to offer Linux across its full product line.
[5,6 11
]
LINUX HISTORY
2001: Version 2.4 of the Linux kernel is released.
2002: Red Hat Enterprise Linux which is the first
commercial Linux for Business IT was released. RHEL is one
of the few Linux distributions that changed Linux forever.
2003: Version 2.6 of the Linux kernel is released.
2004: Ubuntu 4.10 released.
2005: 2005: Linus Torvalds created Git which is software for
tracking changes in any set of files, usually used for
coordinating work among programmers collaboratively
developing source code during software development. Its
goals include speed, data integrity, and support for
distributed, non-linear workflows (thousands of parallel
branches running on different systems).
[5,6 12
]
LINUX HISTORY
2006: Oracle releases its own distribution of Red Hat
Enterprise Linux.
2007: Dell starts distributing laptops with Ubuntu pre-
installed on them.
2008: Android version 1.0 which is a mobile operating
system based on Linux Kernel released. Linux took the first
step in the world of mobile OS.
2009: Google started Chrome OS project
2011: Version 3.0 of the Linux kernel is released.
2012: The aggregate Linux server market revenue exceeds
that of the rest of the Unix market.
[5,6 13
]
LINUX HISTORY
2013: Google's Linux-based Android claims 75% of the
smartphone market share, in terms of the number of
phones shipped.
2014: Ubuntu claims 22,000,000 users.
2015: Version 4.0 of the Linux kernel is released.
2017: All of Top500 list of fastest supercomputers run Linux.
2019: Version 5.0 of the Linux kernel is released.
[5,6 14
]
THE LINUX DISTRIBUTION
ZOO …
15
THE MAIN LINUX
DISTRIBUTIONS
Debia RedHa
SuSE
Gento
Small Secure
n t o
OpenSuS Damn
Ubuntu RHEL SELinux
E Small
Enterpris
Kali CentOS Puppy
e
Mint Fedora
Yellow
Dog
16
THE FILE SYSTEM & NAMING
CONVENTIONS
Hierarchical (of course):
/one/two/three/file.ext
Home directories:
/home/<userid>
System directories:
/usr/local/share/emacs
scope category application
17
SCOPE & CATEGORIES
/ (root) /usr /usr/games /usr/kerberos /usr/local /usr/X11
Program Hardwar
Docs Config Develop Web Display Runtime
s e
includ
bin doc etc cgi-bin fonts dev var
e
public_
lib man rc.d misc log
html
proc
spool
18
OS DIRECTORIES
/boot
ls
List down the files and sub-directories within your current
directory
One of the most frequently used commands in Linux
ls [flags] [directory]
ls –l
Displays the mode, number of links, owner, group, size (in
bytes), and time of last modification for each file.
https://www.ibm.com/docs/en/power6?topic=commands-ls-command
20
FILE TYPES
First character of ls -l
- regular file
b block device
c character device
d directory
l symbolic link
p named pipe
21
PERMISSIONS ATTRIBUTES
Owner / Group / All
r read
w write
x execute
Attribute manipulation
chmod
chown
chgrp
22
PERMISSIONS ATTRIBUTES
chmod u+x file (assign execute permission to user in file)
To use chmod to set permissions, we need to tell it:
Who: Who we are setting permissions for.
u: User, meaning the owner of the file.
g: Group, meaning members of the group the file belongs to.
o: Others, meaning people not governed by the u and g
permissions.
a: All
What: What change are we making? Are we adding or
removing the permission?
–: Minus sign. Removes the permission.
+: Plus sign. Grants the permission.
=: Equals sign. Set a permission and remove others.
Which: Which of the permissions are we setting?
r: The read permission.
w: The write permission.
x: The execute permission.
23
PERMISSIONS ATTRIBUTES
Chown : change owner of the file or directory
chown owner_name file_name
e.g. chown root newFile
Chgrp: change the group ownership of a file or directory
24
DESKTOP ENVIRONMENT
File location
26
BASIC THINGS TO KNOW ABOUT THE
SHELL
File comparison
27
28 SHELL-BASED TEXT
PROCESSING
TASK #1: FIND YOUR TEXT
EDITOR …
The classical
vi/vim
The quick & easy
pico/nano
The lifestyle
emacs
Graphical
gedit, geany
29
FLASH RECALL: I/O STREAMS,
REDIRECTION, PIPES
err.log
pipe
P0 P1
Stream pipelining
30
STREAM REDIRECTION
(BASH)
> Redirect stdout &> Redirect stdout
to a file; and stderr a file;
create/overwrite file. create/ overwrite file.
>> Redirect stdout to a
< Read stdin from a
file; Append to/create
file.
file.
2> Redirect stderr to a << Accept text on
file; create/overwrite following lines as
file. stdin.
2>> Redirect stderr to <> Use file both for
a file; Append to/create stdin and stdout.
file.
31
PIPING BETWEEN
PROCESSES
ls –l wc -l
List files w/ attributes Count number of text lines
> ls –l | wc -l
Count number of files (+ 1)
ls -l | wc -l
32
UNIX-STYLE COMMAND
COMPOSITION
UNIX-STYLE COMMAND
COMPOSITION
find –name '*.pdf'
34
UNIX-STYLE COMMAND
COMPOSITION
find –name '*.pdf' > pdf.lst
Find all PDF files in current subtree. Place result in pdf.lst
35
COMMAND COMPOSITION
XARGS
• Xargs will take a stream of arguments and pass them as a
list.
• Some commands such as grep and awk, can take in
arguments from stdin
• However, others cannot and that is why we need this
36
COMMAND COMPOSITION
TEE
find –name '*.pdf' | xargs ls –lh | tee pdf.lst
Save the results to pdf.lst and also send them stdout.
(I.e., save the intermediate results, continue streaming.)
37
COMMAND COMPOSITION
GREP
find –name '*.pdf' | xargs ls –lh | grep "K "
Find all lines in the listing containing string K<space>.
(I.e., select files with size measures in Kbytes.)
38
COMMAND COMPOSITION
GREP
find –name '*.pdf' | xargs ls –lh | grep " [0-9][0-9][0-9]K "
39
USING GREP
grep [options] regexp [files]
-c Count matching lines.
-f Specify a pattern input file.
-i Ignore case.
-r Search recursively (when used with files).
- Range expression.
a[1-3]z <-> a1z, a2z, a3z
. Any single character.
a.z <-> a1z, a2z, aaz, aCz, …
^$ Start/end of line.
^abc$ line consisting of "abc" only
42
REGULAR EXPRESSIONS (2)
?*+ Repetition operators: 0-1, 0+, 1+ occurrences
a.*z anything starting with "a" and ending on "z"
(regardless of length)
() Grouping.
Used to disambiguate interpretation.
\ Escaping.
google\.com matches "google.com".
43
COMMAND COMPOSITION
(4) AWK
find –name '*.pdf' | xargs ls –lh | grep " [0-9][0-9][0-9]K "
| awk '{print $5}'
Print only the size column (column #5).
44
COMMAND COMPOSITION
(4) CUT
find –name '*.pdf' | xargs ls –lh| grep " [0-9][0-9][0-9]K "
| cut –c 21-25
Print only characters 21-25 from every line. Same output.
45
COMMAND COMPOSITION
(5) SED
find –name '*.pdf' | xargs ls –lh | grep " [0-9][0-9][0-9]K "
| awk '{print $5}'| sed 's/K//'
Remove trailing "K".
46
COMMAND COMPOSITION (6) SUM,
SORT, UNIQ
find –name '*.pdf' | xargs ls –lh | grep " [0-9][0-9][0-9]K "
| awk '{print $5}'| sed 's/K//'| sum
Add up all the size numbers.
find . –name *.pdf | xargs ls –lh| grep " [0-9][0-9][0-9]K "
| awk '{print $5}'| sed 's/K//' |sort|uniq|wc -l
How many different file size are there b/w 100K & 999K.
49
CREATING SCRIPTS
50
CREATING SCRIPTS
Printf:
printf “ABC”
printf “Print First: %s\nPrint Second: %s\n" "30" “40“
OUTPUT:
Print First: 30
Print Second: 40
%s – Replace the argument
51
TOPICS & SCRIPTS (1)
52
TEST CONSTRUCTS
Remember:
By Unix convention, exit status of 0
means success.
[[ … ]] (( … )) let … produce exit status
If the expression expands to a non-zero value, return is 0.
53
TESTS, IF-THEN CONSTRUCT
if [ condition-true ]
then
command 1
command 2
...
else # Or else ...
# Adds default code block
# executing if original condition tests false.
command 3
command 4
...
fi
54
IF-ELIF-THEN CONSTRUCT
if [ condition-true ]
then
command 1
command 2
...
elif [ condition2 ] # Same as else if
then
command 3
command 4
...
else
default-command # if all conditions fail do this
fi
55
TEST, [ ], AND [[ ]]
02-07-testing.sh
bash$ type test
test is a shell builtin
bash$ type '['
[ is a shell builtin
test: both a built-in and a command
bash$ type '[['
[[ is a shell keyword
bash$ type ']]'
]] is a shell keyword [: both a built-in and a command
58
BINARY COMPARISON
OPERATORS
Integer String
-eq is equal to = is equal to
-ne is not equal to != is not equal to
-lt is less than < is before in dictionary order
-le is less than or equal to > is after in dictionary order
-gt is greater than Unary (string)
-ge is greater than or equal to -z is null (zero length)
< (( is less than )) -n is not null
<= (( is less than or equal Compound logical
to)) [[ && ]] logical AND
> (( is greater than )) [[ || ]] logical OR
>= (( is greater than or equal
to ))
02-10-comparisons.sh 02-11-null-string.sh
59
FOR LOOPS
for arg in [list]
do
command(s)...
done
while loops
02-20-while-loop.sh
02-21-while-c-style.sh
02-22-continue-break.sh
02-23-nested-break.sh
61
TEXT PROCESSING
GREP & SED &
AWK
62
63
HOW SED/AWK WORK
Most useful sed scripts are one liners and do something simple.
65
SED EXAMPLE
66
SED EXAMPLE
Replace all “sed” text with DrNur
Command: sed ‘s/sed/DrNur/g’ sedTest
/g is for global.
/1 Replace first match, /2 Replace second match
67
SED EXAMPLE
Parenthesize first character of each word
Command: echo “Hello Class How Are
You" | sed 's/\(\b[A-Z]\)/\(\1\)/g'
68
SED ONE LINERS BY
EXAMPLE …
external:
http://sed.sourceforge.net/sed1line.txt
https://catonmat.net/sed-one-liners-explained-part-one
https://linuxhint.com/50_sed_command_examples/
69
AWK BY EXAMPLE
An awk program is a sequence of pattern-action statements:
70
[9]
AWK BY EXAMPLE
Command: awk {print} stringSample
Print file
71
AWK BY EXAMPLE
72
AWK BY EXAMPLE
awk 'BEGIN { for(i=1;i<10;i++) print "square of", i, “=", i*i;}'
73
AWK (QUESTION FROM
STACKOVERFLOW
[10] 74
[10] 75
REFERENCES
[1] https://en.wikipedia.org/wiki/System_administrator
[2] https://www.wired.com/2016/08/linux-took-web-now-taking-world/
[3] https://webtribunal.net/blog/linux-statistics/
[4] https://www.geeksforgeeks.org/linux-from-the-beginning-history-and-evolution/
[5] https://itsfoss.com/25-years-linux/
[6] https://en.wikipedia.org/wiki/History_of_Linux
[7] https://hangar118.sdf.org/p/bash-scripting-guide-home/
[8] https://www.gnu.org/software/gawk/manual/gawk.pdf
[9] https://www.geeksforgeeks.org/awk-command-unixlinux-examples/
[10] https://stackoverflow.com/questions/25544974/awk-after-grep-print-value-
when-grep-returns-nothing
76