class: center, middle
Introduction to Linux
class: middle
What is Linux
- Free and Open Source computer Operating System
- First released in 1991 by Linus Torvalds
- Linus was 21 years old at the time
- Today, Linux is found everywhere!
class: center
Linux on Devices
TVs | PCs | Tablets | Phones (since 2013)
class: inverse, center, middle
Why use Linux?
.left-column[
Innovative
]
.right-column[
.center[Package Managers (since 1995)]
.center[]
]
.left-column[
Innovative
]
.right-column[
.center[Workspaces (since 1998)]
.center[]
]
.left-column[
Innovative
]
.right-column[
.center[Live CD (free shipping since 2006)]
.center[]
]
.left-column[
Innovative
]
.right-column[
.center[Task Spread (since 2013)]
.center[]
]
.left-column[
Innovative
]
.right-column[
.center[Convergence (since 2015)]
.center[]
]
.left-column[
Innovative
Adaptable
]
.right-column[
.center[Firefox OS Phone]
- Sold for $35 at launch
- Runs only a web renderer on the Linux kernel
- Feels fast, smooth, fluid
.center[]
]
.left-column[
Innovative
Adaptable
]
.right-column[
.center[Chrome OS]
- First major OS to boot in under 2 seconds!
- Runs only a web renderer on the Linux kernel
.center[]
]
.left-column[
Innovative
Adaptable
Productive
]
.right-column[
.center[XMonad]
.center[]
]
.left-column[
Innovative
Adaptable
Productive
]
.right-column[
.center[Containers]
.center[]
]
.left-column[
Innovative
Adaptable
Productive
]
.right-column[
.center[Terminal]
.center[]
]
class: inverse, center, middle
Architecture
class: center
Linux Architecture
.center[]
class: center
Window Managers
.center[]
class: center
File System Hierarchy
.center[]
class: inverse, center, middle
Basic Navigation
The Shell
The shell prompt will appear when the shell is ready to accept input
1 | [me@linuxbox /working/directory]$ |
- username@hostname
- followed by the current working directory
- followed by a
$
sign
Only the $
sign is required for a shell.
Some shells ignore the username, hostname, and working directory.
Essential commands
pwd
prints the current working directory1
2$ pwd
/home/siddhumkdir
makes a directory1
$ mkdir testdir
cd
changes the current working directory1
$ cd testdir
ls
lists the contents of a directory1
2$ ls /home/siddhu
testdir
Essential commands
man
gives offline documentation for commands1
$ man ls
cp
copies a file1
$ cp source destination
rm
removes a file1
$ rm file
mv
moves a file1
$ mv source destination
Essential commands
less
views a file, one screenful at a time1
$ less file
cat
concatenates files1
$ cat file1 file2 file3
nano
is used to edit a file1
$ nano file
Essential commands
tree
displays the directory tree after the current directory1
2
3
4
5
6
7
8
9
10
11$ tree
.
├── dir1
│ ├── file2
│ └── subdir
│ └── anotherfile
├── dir2
├── dir3
│ └── subdir
│ └── subsubdir
└── file
Aliases
~
is an alias for the current users’ home directory#
instead of$
as a prompt indicates superuser privileges~username
refers to the home directory of username.
refers to the current directory..
refers to the parent directory
Path names may be absolute or relative.
cd /home/siddhu/mydir
uses an absolute path namecd mydir
uses a relative path namecd ../mydir
also uses a relative path name
When you use a relative path name without .
or ..
, the shell replaces it
with an absolute path name:
cd mydir
-> cd $PWD/mydir
$PWD
is a special variable which holds the current working directory
Cursor Movement
Basic movement:
- Up arrow goes to the previous command
- Down arrow goes to the next command
- Left/Right are used to jump characters backwards/forwards
- Ctrl+Left / Ctrl+Right are used to jump words backwards/forwards
Advanced movement (Emacs bindings):
Ctrl-p
goes to the previous commandCtrl-n
goes to the next commandCtrl-r
is used to search for a previous commandCtrl-s
is used to search for a newer commandCtrl-a
moves the cursor to the beginning of the lineCtrl-e
moves the cursor to the end of the lineCtrl-w
deletes a word backwards, whileAlt-w
deletes a line backwardsCtrl-d
deletes a character forward, whileAlt-d
deletes a word forwardCtrl-f
jumps a character forward, whileAlt-f
jumps a word forwardCtrl-b
jumps a character backward, whileAlt-b
jumps a word backward
Tab Completion
Typing out long path names can become tedious. Tab completion makes it a lot easier.
1 | $ cd co<Tab>/l<Tab>/s<Tab>/com<Tab> |
If multiple completions exist, tab completion won’t succeed. Press tab twice in such scenarios’ to view all possible completions.
Pro tip: Use ZSH instead of Bash for insane tab completions
1 | $ cd c/l/s/c<Tab> |
class: inverse, center, middle
UNIX
Unix Philosophy
- Aims for minimalist, modular software development
- Write programs that do one thing and do it well
- Write programs to work together
- Write programs to handle text streams
1 | $ cat localfile.txt | wc -l |
1 | $ curl http://www.tldp.org/LDP/sag/sag.txt | wc -l |
Unix Philosophy
- We use pipes (the
|
character) to chain the output of one program as the input of another
.center[]
Unix Philosophy
Everything is a file.
- A text file is a file
- A binary file is a file
- A directory is a file
- The keyboard is a file
- The monitor is a file
- All I/O devices are files
Files don’t need extensions. File types are determined using MIME types. MIME types are embedded in the file header (inode to be precise).
Some common extensions and their corresponding MIME types are listed below:
1 | .txt – text/plain |
Unix Philosophy
Understanding the Unix Philosophy is key to effectively using the terminal. Consider the two equivalent ways of viewing a file with less.
1 | $ less file |
To understand the what’s going on, consider the following:
- I/O is a file
- stdin, stdout, and stderr are files
- stdout of one program can be piped to stdin of another program using
|
Understanding Filesystems
Have you noticed that directories take up 4096 bytes of memory?
Files on a Linux system have an inode table. The inode table consists of:
- file size
- ids
- file mode
- timestamps
- symbolic link count
- pointers to the disk
A directory is just a special file containing the inode numbers of the files
inside it as an array. The size of this array is usually less 4096 bytes.
Filesystems in Linux are usually block aligned. 4096 bytes is a common block
size, and since the directory size is less than 4096 bytes, it’s aligned at
the 4096 byte boundary, making it’s size 4096 bytes.
Understanding Filesystems
Multiple pointers to the inode number may exist. These pointers are
called hard links. All hard links are equivalent.
The alternative, is to have symbolic links, which point to the original
file path, and not the inode number itself.
.center[]
class: inverse, center, middle
Unix Patterns
Wildcards
Wildcards are patterns for searching files
*
represents zero or more characters?
represents a single character[]
represents a range of characters
1 | $ ls |
Under the hood, the shell auto completes wildcards before executing the command.
Permissions
Linux has 3 permissions:
- r - read (view the file)
- w - write (edit the file)
- x - execute (run the file)
The permissions are granted for 3 groups:
- u - owner - person who owns the file (usually the person who created it)
- g - group - accessible to everyone in the group
- o - others - accessible to everyone
Permissions
Use ls -l
to view the permissions on a file:
1 | $ touch file && mkdir dir |
The format is:
1 | d r w x r w x r w x |
- A dash (
-
) indicates the absense of that field rwx
indicates read/write/execute privilege on the file
Permissions
Use chmod
to alter the permissions of a file:
1 | $ touch file |
To set rwx permissions of all groups simultaneously, use binary representation:
1 | $ chmod 754 file # 7 5 4 = 111 101 100 = rwx r-x r-- |
Piping
|
feeds the output of one command, as the input of the next>
writes the stdout of one command to a file>>
appends the stdout of one command to a file2>
writes the stderr of one command to a file
Processes
Threads:
- The number of threads is limited by the hardware
- The kernel can use special threads called kernel threads
- Kernel threads run on top of hardware threads
- POSIX allows multiple p-threads to run on a single thread
- pthreads run on top of kernel threads, they add an address space
- Userspace threads are called green threads
- Compilers may allow multiple green threads to run on a single p-thread
Processes:
- A process is a program which may use one or more threads
- Processes are expensive, and should not be spawned unnecessarily
- Processes are isolated by process isolation
- Processes communicate using inter-process communication
Processes
Threads may run in userspace, or kernel space
.center[]
Processes
The OS manages theads, offloading to different hardware using different techniques.
You can help the OS by specifying your execution model as a programmer.
.center[]
Processes
You can view running process using top
1 | $ top |
If you want to output process info to stdout, use ps [aux]
.
It gives a lot of output, so we use grep to filter the results.
Processes
To kill an existing process, use kill [signal] <PID>
If kill doesn’t destroy the process by itself, you can pass the
signal -9
which destroys the process at the kernel level.
1 | $ ps aux | grep 'firefox' |
Processes
A process can run as a foreground or background job.
To list the current background jobs, use the jobs
command. Pressing Ctrl-Z
while running a program sends it to the background. To bring a job back to the
foreground, use fg <job-number>
1 | $ sleep 15 & |
class: inverse, center, middle
Filters
cat
cat is used for concatenating files, but abused for printing files to stdout.
1 | $ cat sample |
head
head prints out the first n lines of it’s input. If no value for n is given, it
defaults to printing 10 lines.
head -n file
1 | $ head sample |
tail
tail prints out the last n lines of it’s input. If no value for n is given, it
defaults to printing 10 lines.
tail -n file
1 | $ tail sample |
sort
sort is a utility to sort the input alphabetically (though, other sorting
mechanisms are available)
sort -options file
1 | $ sort sample |
nl
nl numbers the lines in the input
nl -options file
1 | $ nl -w 1 -s '. ' sample |
wc
wc counts the number of words (or lines/characters) in the input
wc -options file
1 | $ wc sample # returns lines, words, characters, filename |
cut
cut is a neat utility for grabbing cloumns from a file
cut -options path
1 | $ cut -f 1,3 -d ' ' sample |
Common options:
-f
for fields-d
for delimiters
sed
sed is a stream editor, which should be used to search and replace text
sed <expression> file
1 | $ sed 's/oranges/bananas/g' sample |
awk
awk is a programming language for text manipulation
1 | $ awk '{print $3}' sample |
Common options:
$n
grabs the n-th column-F
sets the field separator
uniq
uniq removes duplicates from data
uniq [options] file
1 | $ cat file |
tac
tac
is just cat in reverse. It prints the last line first, and first line last.
tac file
1 | $ tac sample |
class: inverse, center, middle
Scripting
Scripting
A script is a document containing actions the shell can perform.
Scripts are typically written using sh
, but recently python
has been replacing shell scripts.
1 |
|
1 | #!/bin/python |
Set execute privileges before running:
1 | $ chmod +x hello_world |
class: inverse, center, middle
Development Environments
Isolated Development Environments
Inspired by functional programming, isolated development environments
prevent mutating the global system by creating isolated containers/shells.
Why?
- Packages are installed in multiple locations (
/bin
,/sbin
,/usr/bin
, etc.) - Upgrading a package is dangerous
- Upgrades are not atomic
- Hard to install multiple versions of packages
- No rollbacks
- Difficult to understand dependencies
- Incomplete dependencies
- Root privileges required
Docker
Docker is a platform for building applications.
1 | FROM ubuntu |
Docker
You can build projects in an isolated container:
1 | $ docker build -t siddhu/servo . |
Good for software development since you know exactly what your dependencies are.
It doesn’t affect the global system at all.
It’s especially useful for developing Linux applications on Mac OS X, or Windows.
Nix
Nix is a purely functional package manager. Nix allows creating isolated
development environments almost instantaneously.
1 | $ nix-shell -p cowsay |
Nix shell packages are temporary, and can be deleted using:
1 | $ nix-collect-garbage -d |
This is super useful when you only temporarily want a package.
Nix
You can also write shell.nix
files for your applications:
1 | with import <nixpkgs> {}; { |
Simply cd into the directory containing shell.nix
and type nix-shell
to
enter the development environment.
Unlike Docker, Nix does not provide an isolated container. It only installs
dependencies without polluting the global system.
Nix
Installing multiple versions of packages with Nix is easy:
1 | $ nix-shell -p haskell.packages.ghc784.ghc |
class: inverse, center, middle
Finding
Regular Expressions
Regular expressions (regex) allow us to specify patterns
.
(dot) - a single character.?
- the preceding character matches 0 or 1 times only.*
- the preceding character matches 0 or more times.+
- the preceding character matches 1 or more times.{n}
- the preceding character matches exactly n times.{n,m}
- the preceding character matches at least n times and not more than m times.[agd]
- the character is one of those included within the square brackets.[^agd]
- the character is not one of those included within the square brackets.[c-f]
- the dash within the square brackets operates as a range. In this case it means either the letters c, d, e or f.()
- allows us to group several characters to behave as one.|
(pipe symbol) - the logical OR operation.^
- matches the beginning of the line.$
- matches the end of the line.
Regular Expressions
eGrep finds matches based on regex’s and prints them
1 | $ egrep 'mellons' sample # find mellons |
class: inverse, center, middle
Makefiles
Makefiles
- Make your life easier
- Automate tasks
- Use their own specification language
- Flexible rules
- Imperative for distribution
Makefiles
Makefile:
1 | CC = gcc |
gcc -o outfile infile
⇒ make infile
Makefiles
Common targets
all: |
This target is mandatory. It runs by default if nothing is supplied
after $ make and usually compiles the main program.
|
clean: |
This target generally cleans all generated files so that the working directory may return to its original state, barring modified files. |
install: |
This target moves compiled files to their respective directories within the filesystem tree. It triggers compilation if necessary. |
Kernel Drivers
- Built-in vs. modular
- File system drivers?
- Webcam drivers?
- Not executable
- May use kernel data structures
- Parameters
- Licensing
Kernel Drivers
Commands to interact with modules
modprobe |
Loads a kernel module. |
rmmod |
Removes a module from the kernel. |
lsmod |
Displays a listing of kernel modules. |
modinfo |
Displays information about a kernel module. |
1 | modinfo iwlwifi | grep -E "filename|author|description|license" |
class: inverse, center, middle
Version Control
Git
A version control system maintains a database containing different
versions of the file. Useful for seeing what changed, and rolling
back to older revisions.
.right-column[]
class: inverse, center, middle
Advanced Navigation
Being more productive
.center[]
Special Variables
$1, $2, $3, ...
are the positional parameters."$@"
is an array-like construct of all positional parameters, {$1, $2, $3 …}."$*"
is the IFS expansion of all positional parameters, $1 $2 $3 ….$#
is the number of positional parameters.$-
current options set for the shell.$$
pid of the current shell (not subshell).$_
most recent parameter (or the abs path of the command to start the current shell immediately after startup).$IFS
is the (input) field separator.$?
is the most recent foreground pipeline exit status.$!
is the PID of the most recent background command.$0
is the name of the shell or shell script.
History Completion
!!
executes the previous command
1 | $ echo "Hello, World" |
History Completion
!-n
executes the n-th previous command
1 | $ echo "World 1" |
History Completion
!programName
executes the previous command that starts with programName
1 | $ ps | grep zsh |
History Completion
!?text
executes the previous command that contains the string text
1 | $ ls /etc/cron.daily/passwd |
History Completion
^str1^str2^
replaces the string str1 in the previous command with str2
1 | $ ls -l /etc/cron.daily/passwd |
History Completion
:^
gets the first argument of a command:$
gets the last argument of a command:n
gets the n-th argument of a command:*
gets all arguments of a command:x-y
gets a range of arguments
1 | $ mkdir ~/backup |
History Completion
!%
gets the last searched command
1 | $ /usr/local/apache2/bin/apachectl restart |
History Completion
:p
completes the command without executing it
1 | $ tar cvf old.tar a b c d |
Removing Path Names
:h
removes trailing path names:t
removes leading path names:r
removes the file extension
1 | $ ls -l code/test/llvm/Main.hs |
class: inverse, center, middle