Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Terminal Awareness with notes

Terminal Awareness with notes

Just a general talk on being aware of how to work in the terminal, some basic scripting knowledge, and some tips, tricks and tools I picked up in about a year and a half of working nearly exclusively in a terminal

Ben Nicholas

March 26, 2014
Tweet

More Decks by Ben Nicholas

Other Decks in Programming

Transcript

  1. Shell Anatomy 101 • time php test.php 2>&1 | grep

    $NEEDLE > results.txt Shell Builtin Executables Arguments Redirection Pipe Environment Variable output builtin: a regular command, except provided by your shell instead of an executable executables: a file in your path marked as executable arguments: you all know what this means Redirection: Swapping around the standard streams
  2. Shell Anatomy 101 • time php test.php 2>&1 | grep

    $NEEDLE > results.txt Shell Builtin Executables Arguments Redirection Pipe Environment Variable output Pipe: Special case of redirection, connects stdout to stdin of the other side Environment variable: a variable that gets the value substituted by the shell
  3. man • read the manual page for a given command

    • $ man grep info also exists, but that means using emacs-like bindings… ugh!
  4. cat • Puts the contents of a file onto stdout

    • $ cat “hello.txt”
 Hello, world
  5. head/tail • Puts the beginning or end of a file

    on stdout • great for dealing with logs, especially with -f • $ tail -f /var/log/system.log
 Hello, world
  6. ps • list information about processes • $ ps axu

    | grep grep
 bnicholas 4521 0.0 0.0 2453264 692 s000 S+ 10:26PM 0:00.00 grep grep
  7. kill/killall • send a signal to processes by pid or

    name • $ kill -SIGUSR1 2048 • $ killall perl
  8. sort • Takes lines from stdin and sorts them on

    stdout • $ cat file.txt | sort • $ cat numbers.txt | sort -n
  9. uniq • Removes duplicate lines from stdin to stdout •

    $ cat file.txt | sort | uniq • $ cat ~/history | sort | uniq -c | sort -n
  10. grep • Search for a regular expression • can be

    used on stdin or files • $ cat /var/log/product.log | grep -i “error”
  11. tr • “trim” text, replacing characters with others • $

    cat sentence.txt | tr ‘ ‘ ‘\n’
 My
 name
 is
 ben.
  12. sed • stream editor: a mini text editor • Similar

    to vim find+replace syntax, but so much more too • $ echo “hello world!” | sed -e “s/ w.*d/bn.u/“
 hello bn.u!
  13. find • A file finder with extra • $ find

    . -name “*.orig” -print - delete • $ find . -type f —mtime -1 -print This will find all the files ending in .orig (merge files from git), print them out, and then delete them exec also allows for running any command selectors let you pick files very concisely
  14. awk • a small programming language for dealing with columns

    • ps aux | grep grep | awk ‘{print $2}’
 6845 So much more complicated and powerful than I have time for I’ve only scratched the surface, but it’s really helpful for certain things this example prints the pid of any grep processes
  15. conditionals • if [ $VAR = “foo” ]
 then
 echo

    “bar”
 else
 echo “baz”
 fi explain test, and how dumb it can be sometimes -eq for integer equality, -gt -lt -ne…
  16. Better conditionals • Use && and || when possible •

    cat a.txt | grep “hello” && echo “world”
  17. Key-based auth • More secure and convenient than passwords •

    ssh-copy-id <username>@<host> • Last time you’ll need to type a password • Also applies to git, scp, sftp, and others
  18. ~/.ssh/config • Set up usernames across computers • Keep connections

    alive longer • Set up tunnels tunnels: local ports actually go through to the remote server good things like accessing servers that are firewall’d out
  19. autocomplete stop typing and hit tab. 95% of the time

    it’s gonna do what you want. trust your shell.
  20. fish If you’re still using bash, stop it, install fish,

    make all of your config 100000x easier autocomplete by default, web config, syntax highlighting and checking, sane scripting, small config files (instead of one monolithic _THING_), man page completion
  21. tmux If you’re working on a remote machine, this will

    save you so many times (worked on a laptop, connection on and off, long running tasks that never died)
  22. vim This is going to start a war, I know,

    but knowing a command line editor helps a lot vim is the one of the most powerful, nano, pico, emacs all work, but why?
  23. dsh Elliot and James swear by it, I don’t know

    if it’s worth the config time, but it depends on how you work