Linux as told by Me

Created by Gavin Mogan / @halkeye

Assumptions

  • Not sure what default Telus shell is.
    • Most info is pretty universal, but I’m assuming Bash
  • Not sure what default Input is
    • Most things shouldn’t care.
    • I’m assuming emacs
    • set -o emacs
  • $ means command to run

Conventions

  • Generally, Linux doesn’t care what order CLI parameters are in. GNU Standard. Unix does
  • Double Dash for long arguments (find being a notable exception)
  • Double Dash by itself also commonly means ends of arguments

Getting Help

  • Most commands have a man page
  • $ man find
  • Shell functions only have help
  • $ help for
  • Sometimes --help will return things
  • $ ls --help

Keyboard Shortcuts

  • Ctrl+r - Reverse search through your bash history (My fav)
  • Up/Down - Prev/next in history
  • Ctrl w - Delete previous word
  • Ctrl a/e - Move to start/end of line

Where am I?

$ pwd
/home/gavinm
$ echo $PWD
/home/gavinm

How to Change Directory

  • Linux uses forward slash - / as folder separator
    $ cd /apps/habitat_media
  • Spaces and other characters should be escaped or quoted
    $ cd /apps/habitat\ media
    $ cd "/apps/habitat media"

Get back there

$ pwd
/home/gavinm
$ cd /home/gavinm/Develop
$ pwd
/home/gavinm/Develop

$ cd -
$ pwd
/home/gavinm

Special Directories

$ cd ~
/home/gavinm
$ cd ~eti
/home/eti
$ cd
/home/gavinm

What is in this directory?

Simple Directory Listing

$ ls

Long Listing

$ ls -l

Descending(r) by Time(t)

$ ls -ltr

Hidden files

$ ls -a

What's happened recently?

Head will give you the first n lines

$ head -n 2 /var/log/nginx/presentations.gavinmogan.com.access.log
presentations.gavinmogan.com:80 108.172.217.87 - - [24/Jun/2015:07:02:03 +0000] "GET /stats/ HTTP/1.1" 200 5566 "http://odin.kodekoan.com:4080/halkeye/gavinmogan.com/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36" "108.172.217.87"
presentations.gavinmogan.com:80 108.172.217.87 - - [24/Jun/2015:07:02:03 +0000] "GET /stats/css/reveal.css HTTP/1.1" 200 48591 "http://presentations.gavinmogan.com/stats/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36" "108.172.217.87"

Tail will give you the last n lines

$ tail -n 2 /var/log/nginx/presentations.gavinmogan.com.access.log
presentations.gavinmogan.com:80 162.158.64.218 - - [27/Jun/2015:03:35:46 +0000] "HEAD / HTTP/1.1" 403 166 "-" "Mozilla/5.0 (compatible; CloudFlare-AlwaysOnline/1.0; +http://www.cloudflare.com/always-online)" "162.158.64.218"
presentations.gavinmogan.com:80 162.158.64.218 - - [27/Jun/2015:03:35:46 +0000] "GET / HTTP/1.1" 403 345 "-" "Mozilla/5.0 (compatible; CloudFlare-AlwaysOnline/1.0; +http://www.cloudflare.com/always-online) AppleWebKit/534.34" "162.158.64.218"

Common Options

  • -f will keep tailing
  • -F will restart tailing if file is truncated
  • -n #num# will only print out #num# number of lines

Create Edit Update Destroy

  • cat <file> - outputs contents
  • tac <file> - outputs contents in reverse directory
  • less <file> - outputs content (controlled)
  • rm <file> - Remove a file
  • rmdir <dir> - Remove an empty directory
  • mkdir <dir> - Create a directory (mkdir -p as bonus)
  • mkdir -p <dir/subdir/subdir2> - Create all the directories required to make the full path (and doesn't error if already exists)
  • touch <file> - update timestamp/create empty file

Whats going on?

  • ps xf -A (My Favourite)
  • ps aux (Very portable)
    • More w's with ps = more wide
    • ps auxwww
  • pstree
  • top

Redirection

Output to file

$ echo "hi" > file.txt

Errors to file

$ curl http://fake.server 2> errors.txt

Input from file

$ mysql < import.sql 

Wildcards

$ tail -F ~/Develop/*/logs/development.log

Will look find all directories that have a logs directory underneath it.

$ tail -F ~/Develop/*/logs/*.log

Will find all log files under all directories that have a log directory (one level deep)

More Complex Wildcards

$ ls /apps/local/{nginx,ruby}

It is the same as:

$ ls /apps/local/nginx /apps/local/ruby
$ mkdir [a-e]

Makes a,b,c,d,e directories

Serious wildcards

find is super powerful, Check man pages

Find all directories

$ find -type d

Find all files

$ find -type f

Find all files ending in log

$ find -name '*.log'
$ find -name '*.log' -exec ls {} \;
$ find -name '*.log' -exec ls {} +

Power of pipes

With a few combos you can do anything

Some Good Pipes

Filter out text

grep

Pull out parts of a line

ack / cut

Reorder items

uniq / Sort

Loops

for

Grep and Awk Can Do anything

grep /favicon.ico access.log
long output
grep /favicon.ico access.log | awk '{print $17}'
AppleWebKit/537.36
grep /favicon.ico access.log | awk -F'"' '{print $6}'
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36

Add sort and uniq pipes

Bang Bang

$ yum install tig
You need to be root to perform this command.
$ sudo !!
$ sudo yum install tig

What are others doing?

Literally who is logged in to this box right now.

Lists user accounts and when they log in.

$ who

Lists who is online and what they are doing.

$ w

Testing HTTP

Curl and wget work kinda the same, but under different designs.

Wget is great at downloading files.

$ wget http://i.imgur.com/Ia48QDR.jpg

Curl is better at retrieving content.

$ curl http://jenkins/job/test-workflow/api/json?pretty=true

For Loops

for i in $(seq 1 10); do echo $i; done
for i in gavin likes pie; do mkdir $i; done
for i in *; do mv $i $i.bak; done

Search and Replace

Sed, perl, python, etc

I prefer perl pie

$ echo "Gavin likes Pie" > file.txt
$ perl -pi -e 's/Gavin/Phil/' file.txt
$ cat file.txt

Debugging

Run a script in debug mode

$ bash -x script.sh

Enable debugging right now

$ set -x

SSH

What are ssh keys?

Why would you want them?

How do you use them?

man ssh_config

THE END