Introduction to Linux

Matt Thomas - Developer (Full Stack) Senior

@matsinet

https://where.matsinet.codes

Why is Linux important?

  • In 2019, 100% of the world’s supercomputers run on Linux.
  • Out of the top 25 websites in the world, only 2 aren’t using Linux.
  • 96.3% of the world’s top 1 million servers run on Linux.
  • 90% of cloud infrastructure operates on Linux & practically all the best cloud hosts use it.
  • 98.8% of mobile phone market (Android & iOS)
  • 54.1% of professional developers use Linux as a platform in 2019.
  • 83.1% of developers say Linux is the platform they prefer to work on.
  • The US Department of Defense migrated to Linux in 2007 because of its higher level of security.
  • The US Navy’s warships have been using a Red Hat-based Linux software since 2013.

It's only growing with electric (Telsa) & self-driving cars, land & air traffic control systems, automation, space travel (ISS & SpaceX).

https://hostingtribunal.com/blog/linux-statistics/
https://www.makeuseof.com/tag/linux-taking-over-world/
https://www.rankred.com/what-hardware-software-does-spacex-use-to-power-its-rockets/

Distributions

A Linux distribution (often abbreviated as distro) is an operating system made from a software collection, which is based upon the Linux kernel and, often, a package management system.

Per lwn.net... "Almost six hundred Linux distributions exist, with close to five hundred out of those in active development."

The Debian Project is an association of individuals who have made common cause to create a free operating system. This operating system that we have created is called Debian.

Debian systems currently use the Linux kernel or the FreeBSD kernel. Linux is a piece of software started by Linus Torvalds and supported by thousands of programmers worldwide. FreeBSD is an operating system including a kernel and other software.

Base of numerous distributions.

Ubuntu is an open source software platform that runs from the cloud, to the smartphone, to all your things

Target platforms

  • Cloud
  • Server
  • Desktop
  • Core
  • IoT
  • Phone
  • Tablet

Based on Debian

My personal favorite at the moment.

Red Hat Enterprise Linux delivers military-grade security, 99.999% uptime, support for business-critical workloads, and so much more. Ultimately, the platform helps you reallocate resources from maintaining the status quo to tackling new challenges. It's just 1 reason why more than 90% of Fortune Global 500 companies use Red Hat products and solutions.

Variations: Enterprise, CentOS & Fedora Linux

The purpose of Linux Mint is to produce a modern, elegant and comfortable operating system which is both powerful and easy to use.

Based on Ubuntu and thus Debian

Raspbian

Debian variant designed for Raspberry Pi

Small in size (8Gb), runs off Micro SD card.

Android

Android is a mobile operating system based on a modified version of the Linux kernel and other open source software, designed primarily for touchscreen mobile devices such as smartphones and tablets

Mac OS X???

Mac OS X has origins in BSD

Not Linux by definition, but is a Unix variant

By default, the terminal in OS X is Bash or ZSH (z shell) so commands work just like Linux

Mac OS X Ancestry

Graphical Desktop Environments

GNOME 3

KDE (Plasma)

Unity (Ubuntu)

Cinnamon (Linux Mint)

Xfce

The Shell

or as you probably know it better as...

Terminal

Bourne Shell (sh)

This is the oldest shell and as such is not as feature rich as many of the other shells.

Bash Shell (bash)

The Bash shell is a combination of features from the Bourne Shell and the C Shell. It's name comes from the Bourne Again SHell. It has a command-line editor that allows the use of the cursor keys in a more "user friendly" manner than the Korn shell. It also has a useful help facility allowing you to get a list of commands by typing the first few letters followed by the "TAB" key. It is the default shell on most Linux distributions.

Z Shell (zsh)

ZSH, also called the Z shell, is an extended version of the Bourne Shell (sh), with plenty of new features, and support for plugins and themes. Since it’s based on the same shell as Bash, ZSH has many of the same features, and switching over is a breeze.

man

Outputs manual aka "man" page that explains how to use the specified command

man <command>

Uses VI navigation and exit commands (To be explained later)

Tab Completion

bash and z shell both support tab completion at the command line

{partial command, file name, directory name}[tab]

Type a string that makes up the beginning of a command, file name and directory name and press the tab key to complete the exact match.

{partial command, file name, directory name}[tab][tab]

If there is no exact match, no results will show, but doing it twice [tab][tab] to get a list of possible matches.

ls

List file & directories (DOS equivalent is dir)

* is the wildcard

ls -l

List files and directory long format and show permissions

ls -a

List files and directory including hidden files

ls -h

List files and directory using human readable file size

ls -lah

Combining them all together

mkdir

Create a new directory

mkdir {directory name}

Path Aliases

Alias Description Purpose
. period/dot Current directory
.. double period/dot dot Parent directory
~ tilde Home directory (/home/{username})
- dash/minus Previously navigated directory
/ slash/front slash/whack Directory separator, Windows uses \ (back slash)
/ slash/front slash/whack Root directory, when used at the beginning of a path

cd & pwd

Change directory

cd {path to any directory}

cd ..

(double period) Change to the parent directory

cd ~

(tilde) Change to your home directory

cd -

(dash) Change to previous directory

pwd

Output your current location

touch

Create a new file

touch {file name}

cp

Copy

cp {source} {destination}

Copy a file, destination default is the current directory & can be omitted.

cp -R {source} {destination}

Copy recursively including directories

Destination default is the current directory (.) & can be omitted.

rm

Remove

rm -R

Remove recursively

rm -f

Remove forcefully i.e.: don't ask if this is what I really mean!

Linux Shell does NOT have a recycle bin! ... Be warned, there is no turning back.

rm -Rf

Remove recursively and forcefully.... Again be warned!

history

Command History, lists commands executed recently

Use the !## (Bang aka Exclamation Point) to execute a previous command

Up and down arrow keys can be used to navigate previous commands as well

ctrl+r

Search command history, right on the command line

Press ctrl + r and type a search string, press enter key to accept

Example: `npm`

grep

Search a file for a given pattern or regex

grep <patttern/regex> <file(s)>

  • There are various wildcards that can be used including the standard asterick *
  • Regex can be used as well. Sorry that is a whole other talk.
    Additional Information: https://en.wikipedia.org/wiki/Regular_expression

cat

Outputs the contents of a file to the terminal

cat <file(s)>

<command> | <command>..

(pipe, pass-through)

You can pipe as many times as you like as long as the commands output to standard out.

Spaces around the pipe (|) are not required, but I personally feel it is more readable.

less is more

(pagination)

<command> | less

Less uses VI navigation and search functionality, space to advance full pages.

 

<command> | more

More is older and has less (not Less) functionality

More is found in most minimal distros due to its smaller size of 2000 lines vs less's 27000 lines.

chown

Change the ownership of a file or directory

chmod

Change the permissions of a file or directory

Permissions, you ask?

777, 664... It is complicated, read this: Permissions Calculator Info

top

Task Manager

htop

Human readable task manager

shutdown

Shuts down or restartt the computer gracefully after a given time period.

shutdown -h now

Shuts down (halts) the computer now

shutdown -r now

Restarts the computer now

shutdown -r 01:00 "The system will reboot at 1 AM"

Restarts the computer at 1:00 AM sending the message to all logged in users via terminal.

At 12:55 AM all new log in attempts will be blocked until after the reboot.

shutdown -c

Cancels a pending shutdown

.bashrc | .zshrc

.profile | .bash_profile | .zprofile

dot files

Dotfiles are used to customize your system. The “dotfiles” name is derived from the configuration files in Unix-like systems that start with a dot (e.g. .bash_profile and .gitconfig)

Getting Started With Dotfiles

Elevating Privileges/Becoming root

sudo <command>

Some actions require elevated privileges, this requires becoming root temporarily

sudo !!

Run the previous command with elevated rights

sudo su -

Becoming root for the duration of the session
This will give you elevated privileges for the entirety of the session

whoami

Find out what user account you are currently logged in to the shell using

exit

Exit the current user account

Editors

Nano

Vim (VI)

Nano

GNU nano is a text editor for Unix-like computing systems or operating environments using a command line interface. It emulates the Pico text editor, part of the Pine email client, and also provides additional functionality.

There is only 1 mode in Nano, Edit.

Commands are executed using Ctrl (^) + Letter combinations i.e.: ^x to exit.

Vim

Linux: http://www.vim.org/

Mac: http://macvim-dev.github.io/macvim/

Happy Birthday - Nov. 2, 1991

Why is Vim important?

Vim is found on EVERY linux computer and is available via the command line.

VI vs Vim

Vim includes most all basic features from VI. For this Intro they can be interchanged.

Vim also has numerous additional features inlcuding:

  • Vim has been ported to a much wider range of OS's than vi.
  • Vim includes support (syntax highlighting, code folding, etc) for several popular programming languages (C/C++, Python, Perl, shell, etc).
  • Vim integrates with cscope.
  • Vim can be used to edit files using network protocols like SSH and HTTP.
  • Vim includes multilevel undo/redo.
  • Vim allows the screen to be split for editing multiple files.
  • Vim can edit files inside a compressed archive (gzip, zip, tar, etc).
  • Vim includes a built in diff for comparing files (vimdiff).
  • Vim includes support for plugins, and finer control over config and startup files.
  • Vim can be scripted with vimscript, or with an external scripting language (e.g. python, perl, shell).

Open a file with VIM

vim {file name}

This will open the file in command mode. What is command mode?

Modes

Command (esc)

Enter commands such as goto line, search, find and replace

Insert (i,I,a,A,o,O)

Insert new characters, Append new characters, Open a new line and then insert characters

Replace (R)

Enter insert mode while replacing characters instead of "pushing them"

Vim Editor Modes Explained

Basic Editing

Command Description
c Change
y Yank Character (Copy)
yy Yank Line (Copy)
x Delete Charactor (Cut)
d Delete, requires movement, quantity optional
dd Delete Line (Cut)
p Put (Paste)
. Repeat Last Command (Period)
u Undo
Ctrl-R Redo

Basic Command Structure

<quantity>command<movement>

All commands are cASe SeNsiTive

Command Description
2dw Delete the next 2 words
2cw Delete the next 2 words and drop into insert mode

Movement

By default Vim uses only the basic keyboard so moving the cursor is down with...

h j k l

Arrow Keys also work most of the time, which I prefer.

Movement Description
b Beginning of word
w End of word
0 Beginning of line
$ End of line
gg First line of the file
G Last line of the file
Vim Movement Shortcuts Wallpaper
(http://naleid.com/blog/2010/10/04/vim-movement-shortcuts-wallpaper)

Visual Mode

There are 3 types of visual mode, character, line & block

Mode Description
v Characters - Select consecutive characters
V Line - Select consecutive lines
Ctrl-V Block - Select continue block in either horizontal and/or vertical directions

We are just scratching the surface of visual mode, more details here: http://vimdoc.sourceforge.net/htmldoc/visual.html

Search

/{search}

"n" & "N" will take you to next (n) and previous (N) search string.

Search and Replace

aka: substitute

:s/{search}/{replace}/g

Search and replace with the "g" option will not ask for confirmation.

The exclamation point (!)

has special meaning in Vim.

When you want to force an action regardless of the consequences use an exclamation point after the command.

A mentor told me to think of the exclaimation point as

Damn it!

Thanks Gregg

Example: To overwrite a file when it should throw a read-only error
use :w! (write damn it!)

With that being said...

Has anyone ever been trapped in Vim?

The key to your escape is...
:q!

(Quit damb it!)

Be careful as explained in the previous slide the ! has consequences, in this case all modifications to the file will be lost.

Demo

Do you have Git Bash installed?
Open it up and follow along.

Questions?

Further Learning:

Feedback...

  • Matt Thomas
  • Twitter: @matsinet
  • Website: https://where.matsinet.codes

Slides: https://where.matsinet.codes/presentations/intro-to-linux

Print the presentation
Created with Reveal.js