mastering the unix command line
TRANSCRIPT
Mastering the UNIX Command Line
Howard Mao
March 28, 2013
UNIX
I Created by Ken Thompson and Dennis Ritchie in 1970I Modern descendants include Linux, OSX, and BSD
Looking up Information
I Man Pages: man <command>I Google is your friendI Stackexchange Sites
I stackoverflow.comI serverfault.comI superuser.comI unix.stackexchange.com
Basic Filesystem Navigation
I look around - lsI move about - cd, pushd, popdI move, copy, delete - mv, cp, rmI file info - stat, du, fileI changing permissions - chmod, chown
More About Permissions
I Three types of permissions: read (r), write (w), execute (x)I Three categories: user (u), group (g), other (o)I Also represented by an octal number: read (4), write (2),execute (1)
I Add them together to get number. One octal digit for eachcategory
I Ex. 644 means read + write for user, read only for groupand other, execute for nobody
I Execute permission for directory means you can list files indirectory
Globbing
I Match everything *I Choices {foo,bar,baz}I Characters [abc]I Numbers {0..5}
The Almighty Find
Find allows you to search for filesI By Name: find . -name \*.txtI By Type: find . -type dI By Timestamp: find . -mtime 1I And many many more
Can also run commands on filesfind . -name .txt -exec rm {} \;
I/O Redirection
I I/O Commands: echo, cat, lessI Input from File: command < input.txtI Output to File: command > output.txtI Pipes : command1 | command2
Filters
I Search lines with grepI Find and Replace with sedI Select columns with cut and awk
Manipulating Processes
I Start in background: command &I One after another: command1; command2I If successful, then: command1 && command2I If unsuccessful, then: command1 || command2I Suspend and resume: command <Ctrl>+Z; [bg|fg]I See running procs: ps, ps -au $USER, ps aux
Networking
I Server info - ping, traceroute, host, whoisI Sending raw traffic - nc, telnetI The World Wide Web - wget, curlI What’s my address? - ip addr, ip linkI What ports are open - ss, nmapI Who’s on the port - lsof
Scripting
I Believe it or not, the shell is turing completeI Variables : VAR=abcI If statements : if [ "$VAR" ]; then echo $VAR; fiI For loops : for i in {1..5}; do echo $i; doneI While loops : while true; do echo "forever"; doneI User input : read var; echo $varI Functions : function func () { do something }