Using the command line



Using the command line

0 0


command-line-presentation

Slides for a command line (bash) basics presentation

On Github actionshrimp / command-line-presentation

Using the command line

Disclaimer

Mostly about bash

Windows cmd.exe does some of this (or powershell)

BUT you can use git bash on windows

Command processor

Text (commands) -> Executing programs [ -> Text output ]

 $ ls
 sushi-cat-1.jpg sushi-cat-2.jpg sushi-cat-3.jpg ...
                    
 $ rm angsty-poetry.txt 
                    
 $ curl http://www.7digital.com
 <html lang="en-GB" ...
                    

Text!

  • Text is awesome
  • You can input it with all ~100 buttons on the keyboard!
  • You can save it for later
  • You can READ it after it has been saved
  • Lots of tools for dealing with it
  • e.g. diffing, your fave text editor (shoutout to vim <3)
  • You can generate it easily with a program
  • Powerful UI for running programs
  • e.g. an option per letter rather than 26 radio buttons

Some Common unixy programs

Little programs each good at one thing (usually)

  • mv
  • rm
  • cp
  • curl
  • find
  • grep
  • head
  • tail
  • ...

OH: "unix: microservices since 1973" #truth

— Pieter Noordhuis (@pnoordhuis) October 22, 2014

Help!

For built-ins:

 $ help
                    

For commands, CONVENTION: --help (or -h)

 $ curl --help
 Usage: curl [options...] <url>
 Options: (H) means HTTP/HTTPS only, (F) means FTP only
      --anyauth       Pick "any" authentication method (H)
  -a, --append        Append to target file when uploading (F/SFTP)
      --basic         Use HTTP Basic Authentication (H)
 ...
                    
                        
 $ curl --help | less
                    

The Manual

More in depth help: man <command>

(May not be installed on git bash)

 $ man curl
 NAME
        curl - transfer a URL
 
 SYNOPSIS
        curl [options] [URL...]
 
 DESCRIPTION
        curl  is a tool to transfer data from or to a server...
        RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP)...
 
                        
  • Paged
  • Search with /, n to go to next match, N to go back (vim shortcuts)
  • Page down (up) or CTRL+D (CTRL+U)
  • Same keys work for less

More conventions

  • Short option / long option
  (from ls's help)

  -a, --all    do not ignore entries starting with .
                   
  $ ls -a
  ...
  $ ls --all
  ...
                   
  • Long options are more readable
  • Short options are dense, but combinable
  $ ls -lahg
                       
  • Similar meanings, e.g. -n for "number of" something
     $ tail -n 5 index.html
     ...
     $ head -n 5 index.html
                                
  • -r / -R for "recursive", -f for force
     $ rm -rf /
     ...
                                
  • -h for "human readable"
     $ ls -l
     total 88K
     drwxr-xr-x  4 dave users 4096 Sep 29 16:16 css
    
     $ ls -lh
     total 88K
     drwxr-xr-x  4 dave users 4.0K Sep 29 16:16 css
    
     $ du -h
     ...
                                
  • Wait, what?
  • Colliding abbreviation (-h)
  • (in these cases --help still works though!)
  • Other oddballs, e.g. find
     $ find . -iname "*.json"
                                
  • Often favours power (and experience learning curve!)
     $ find . -name 'foo.cs' '!' -path '.git'
                                
  • Program predates conventions (or maintainer is anti-social?)

On windows (cmd.exe)

/? instead of -h or --help

Switches are generally in the style /X rather than -x

Powershell

... but it's what you're used to

Bash itself

Good at

Helping you write commands

Executing programs

Home directory - save typing

 $ cd ~
                    

Aliases serve a similar purpose

History

 $ history
                    

Basic wildcards

 $ mv *.jpg pictures/
                    
 $ ls src/**/*.cs
                    

Brace expansion

 $ echo {a,b}{c,d}
 ac ad bc bd
                    
 $ ls
 long-file-name-with-a-msitaek-in-the-middle.txt
 $ mv long-file-name-with-a-{msitaek,mistake}-in-the-middle.txt
 $ ls
 long-file-name-with-a-mistake-in-the-middle.txt
                    
 $ echo {1..10}
 1 2 3 4 5 6 7 8 9 10
                    
 $ cat server-errors-2014-09-2{5..9}.log
 ...
                    

Looping

Syntax is a bit nasty

 $ for i in {1..4}; do touch new-file-$i.txt; done;
                    
 $ touch new-file-1.txt
 $ touch new-file-2.txt
 $ ...
                    

Brace expansion can often achieve similar results depending on the command you're running, e.g.

 $ touch new-file-{1..4}.txt
                    
 $ touch new-file-1.txt new-file-2.txt ...
                    

Using a scripting language can be quicker for more complex stuff

Keyboard shortcuts

  • A few readline shortcuts:
    • Tab: auto-complete filenames/directories
    • Up arrow: Last command
    • CTRL+W: Delete last word
    • ALT+B: Jump cursor back a word
    • CTRL+R: Search through command history (and keep pressing to CTRL+R to cycle through history)

Executing programs

Streams

Simple command

e.g.

 $ ls -lah
                    

Command substitution

 $ whoami
 a-stupidly-long-username-argh-what-a-pain-to-type

 $ sudo chown $(whoami) someone-elses-file.txt
                    

This also works:

 $ sudo chown `whoami` someone-elses-file.txt
                    

Those are backticks, $(..) is prefered.

Piping

 # Search all .cs files (recursively) in src/ for 'ElusiveWidgetFactory'
 $ cat src/**/*.cs | grep ElusiveWidgetFactory

 # Page through 7digital.com HTML
 $ curl www.7digital.com | less

 # Monitor error log file and report lines which match "500"
 $ tail -f /var/log/server/error.log | grep 500
                    

Requires stdin convention to be followed

Rule of Silence - no unnecessary output

Rule of Repair - fail noisily

Redirection

 $ curl "http://api.7digital.com/1.2/release/search?q=..." \
   | xml format > search_results.xml
                    
 echo bin >> .gitignore
                    
 # /dev/null becomes "nul" on windows
 $ curl -vs www.7digital.com >/dev/null
                    
 $ run-the-tests 2>&1 >/dev/null | grep "a-particular-error"
                    
 $ grep "something" < myfile.txt
                    

Final thoughts

We didn't cover "if then else"

Don't try to do too much!

Useful because it's everywhere

Use a real scripting language if it makes sense