Introduction to Linux
Introduction to Linux
to Navigator
It’s an
Operating
System
What is Linux?
? Linux is a Unix clone written from scratch by
Linus Torvalds with assistance from a
loosely-knit team of hackers across the Net.
? Unix is a multitasking, multi-user computer
operating system originally developed in
1969 by a group of AT&T employees at Bell
Labs.
? Linux and Unix strive to be POSIX compliant.
? 64% of the world’s servers run some variant
of Unix or Linux. The Android phone and the
Kindle run Linux.
The Linux Philosophy
The *Nix Philosophy of Doug McIlroy
(i) Make each program do one thing well. To do a new
job, build afresh rather than complicate old programs
by adding new features.
(ii) Expect the output of every program to become
the input to another, as yet unknown, program. Don't
clutter output with extraneous information. Avoid
stringently columnar or binary input formats. Don't
insist on interactive input.
(iii) Use tools in preference to unskilled help to
lighten a programming task, even if you have to
detour to build the tools and expect to throw some of
them out after you've finished using them.
Linux Has Many
Distributions
What is Linux?
Linux + GNU Utilities = Free
Unix
◦ Mac OS X
●“Terminal” is already installed
●Why? Darwin, the system on which Apple's Mac OS
X is built, is a derivative of 4.4BSD-Lite2 and
FreeBSD. In other words, the Mac is a Unix system!
Let the Linux Lab Begin!
? Syntax:
BEGIN { Actions}
{ACTION} # Action for every line in a file
END { Actions }
? Try
◦ls –l /usr
◦ls –l /usr | awk ‘{print $9 “\t” $5}’
◦ls –l /usr > usr.txt
◦awk ‘print $9 “\t” $5}’ usr.txt (gives same results as 2 nd
command line, but awk is acting on a file instead of saved
output)
◦ls –lh /lib | awk ‘{printf “%20s\t%s\n”,$9,$5}’
◦ls –l /lib | awk ‘BEGIN {sum=0} {printf “%20s\t%s\n”,
$9,$5; sum+=$5} END{sum/=1000000; printf “\nTotal:
%d GB\n”,sum}’
Editing Output Lines With awk
Output from awk commands
Editing Output Lines With sed
? sed replaces one substring with another
? sed operates on every line in a file or processes
every line piped into it
? sed matches patterns using regular expressions
(See regular-expressions.pdf cheat sheet)
? Common regular expression metacharacters:
◦ . – any character
◦ ? – quantified zero or one
◦ * - quantifier none or more
◦ + - quantifier one or more
◦ ^ - beginning of line
◦ $ - end of line
◦ [XxYy] – character class matching upper or lower case “X”
or “”Y”
Editing Output Lines With sed –
continued
? Try
◦ echo “The rain in Spain stays mainly in the
plain.” > easy_sed.txt; cat easy_sed.txt
◦ sed –i.bak
‘s/rain/snow/;s/Spain/Sweden/;s/plain/mountains
/’ easy_sed.txt; cat easy_sed.txt
◦ ls -l /lib | awk 'BEGIN {sum=0} {printf "%s\t%s\
n",$9,$5; sum+=$5} END{printf "\nTotal: %d\
n",sum}' | sed -e 's/\.so\(\.[0-9]*\)*//' | less
(challenge: get rid of soname extension)
◦ ls -l /lib | awk 'BEGIN {sum=0} {printf "%s\t%s\
n",$9,$5; sum+=$5} END{printf "\nTotal: %d
GB\n",sum}' | sed -e 's/\.so\(\.[0-9]*\)*//' | awk
'{printf "%20s\t%s\n",$1,$2}‘ | less (pretty print)
Editing Output Lines With sed
Output from sed commands
Editing Files with Emacs and Vim
You don’t have to take sides and there is always “nedit”
Editing Files with Emacs and Vim
? Movement: <C-b>,<C-n>, ? Movement: <h>,<j>,
<C-p>,<C-f>,<M-b>,<M- <k>,<l>,<b>,<e>,<0>,<
e>,<C-a>,<C-e>,<M-’<‘
$>,<gg>,<G>
>,<M-’>’ >,
? Change/Delete/Replace: <C-
? Change/Delete/Replace:
d>,<M-d-esc>,<M-d>,<C- <x>,<cw>,<dw>,<dd>,<
kk>,<C-d’char’>,<Insert> r>,<R>
? Copy/Paste: <C-space>,<C- ? Copy/Paste:
y>,<C-_>,<M-w>,<C-aky> <v>,<P>,<u>,<y>,<yy>
? Search/Replace: <C-s ? Search/Replace:
enter>,<C-s>,<C-r>,<M-x, </>,<n>,<N>,<:
‘replace-
%s/’regex’/’replacement’/
string’<CR>’srchstr’<CR>’re
placement’<CR> g>
? Save/Quit: <C-xs>,<C- ? Save/Quit:
xw>,<C-xc,’n’,’yes’<CR>> <:q>,<:w>,<:q!>
Emacs – Control Keys Vim – Modal
C=Ctrl and M=Meta (Alt) Cmd, Insert, and Visual
Mission Possible: Editing Files with Emacs and Vim
• Someone has corrupted Edgar Allen Poe’s poem, “The Raven.” Your
mission, should you decide to accept it, is to repair the damage
with emacs or vim and then confirm with the “diff” command.
Hint: Also use diff to find corruption.
• After editing and saving your file, confirm you work with:
• diff bad-the-raven.txt good-the-raven.txt
Finis