*nix Command Line Kung Foo DevChatt, March 2010
Who is this guy? Brian Dailey realm3 web applications Nashville
Why the command line?
It is concise.
It is consistent.
It saves time.
What command line?
Posix compliant shell (Preferably bash or dash.)
Not using *nix? Pity. Mac OS X: You're good! Windows: Use cygwin (preferable)  or a Unix port such as: http://unxutils.sou...
Basics: Keyboard Navigation
Shell Keyboard Navigation Basics ↑↓ -  Scroll through previous commands Ctrl+U - Clear all before cursor Ctrl+K - Clear al...
Goodbye, Mouse! Example: mysql> SELECT user.id, user.username, user.email, user.address, user.city, user.state, user.name,...
Shell Keyboard Navigation Kill and yank = Cut and paste Kill Commands: Ctrl+U - Kill all before cursor Ctrl+K - Kill all a...
Shell Keyboard Navigation Use Tab for auto-completion! $ svn ci -m”Fixed issue.” fil[tab] ↓ $ svn ci -m”Fixed issue.” file...
Shell Keyboard Navigation !! - Run the last command (same as  ↑ +return) !my - Runs last cmd starting with ”my”, e.g.,  ” ...
Shell Basics: I/O Redirect Concept can be thought of as ” pipes ” and ” streams ” Negates the need for an application to w...
I/O filters less  – Interactive text pager with search capability. sort  – Sort lines (alphabetically, numerically, etc) g...
I/O Basic example of standard output: # format an XML file and feed it out to formatted.xml $ xmllint feed.xml --format > ...
I/O Examples of pipe chaining... Colorize Subversion diff output: $  svn diff | colordiff | less -R Search files for child...
I/O Example: # copy the latest changeset to a git-less server $ git log -1 --name-status |  # only get files added or modi...
Holy smokes!  How will I remember to type all that?
Let's automate this stuff!
Introducing Alias alias deploy=”git log -1 --name-status |  grep '^[AM]s' |  sed 's/^[AM]s*//' |  xargs -I {} scp {}  [ema...
Alias Things to know: With alias, all arguments are appended to the command. Save your aliases to your .bashrc to automati...
Alias # Usage: fun [name] alias fun='echo $1 is having fun!' $ fun Brian is having fun! Brian Alias is only useful as the ...
Shell Functions Look up PHP command arguments: phpargs() { # $1 is the first argument passed # get php page from manual mi...
Further Resources Comprehensive Introduction to the shell http://www.linuxcommand.org/learning_the_shell.php Bash keyboard...
Thanks! Any questions? Brian Dailey realm3 web applications Web:  http://realm3.com/ Twitter: @brian_dailey Email: brian@r...
Upcoming SlideShare
Loading in...5
×

DevChatt 2010 - *nix Cmd Line Kung Foo

1,637

Published on

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,637
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
18
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

DevChatt 2010 - *nix Cmd Line Kung Foo

  1. 1. *nix Command Line Kung Foo DevChatt, March 2010
  2. 2. Who is this guy? Brian Dailey realm3 web applications Nashville
  3. 3. Why the command line?
  4. 4. It is concise.
  5. 5. It is consistent.
  6. 6. It saves time.
  7. 7. What command line?
  8. 8. Posix compliant shell (Preferably bash or dash.)
  9. 9. Not using *nix? Pity. Mac OS X: You're good! Windows: Use cygwin (preferable) or a Unix port such as: http://unxutils.sourceforge.net/ Other: PuTTY + Amazon EC2, Ubuntu Live CD, etc.
  10. 10. Basics: Keyboard Navigation
  11. 11. Shell Keyboard Navigation Basics ↑↓ - Scroll through previous commands Ctrl+U - Clear all before cursor Ctrl+K - Clear all after cursor Ctrl+A - <Home> Ctrl+E - <End> Alt+B - Move cursor back one ”word” (Similar to Ctrl+ ← ) Alt+F - Move cursor forward one ”word” (Ctrl+ -> ) Alt+Bckspc - Delete previous ”word” (Also, Ctrl+W) Alt+. - Recall last command argument Tab - Autocomplete commands, files, folders, and more. Ctrl+L - Clear the screen Ctrl+_ - Undo CTRL+R - Recall previous commands Esc, T - Swap last two words These shortcuts are nearly universal (bash, MySQL) due to use of the GNU Readline library. Exhaustive list is available here: http://onlyubuntu.blogspot.com/2007/03/bash-shell-keyboard-shortcuts-for-linux.html
  12. 12. Goodbye, Mouse! Example: mysql> SELECT user.id, user.username, user.email, user.address, user.city, user.state, user.name, userfile.file_name, userfile.metadata, userfile.other_stuff FOM user JOIN userfiles ON user.id = userfile.user_id AND user.id BETWEEN 1 AND 100 AND user.created BETWEEN '2008-10-10' AND '2009-10-10' AND user.is_active = 1 AND user.name LIKE 'Frodo%' LIMIT 10; [] n00b: Hold backspace key for 30 seconds. Fix, retype. Good: Press Alt+B to jump backwards one word at a time. Holy Hand Grenade: Press Alt+3,0,B to jump backwards 30 words.
  13. 13. Shell Keyboard Navigation Kill and yank = Cut and paste Kill Commands: Ctrl+U - Kill all before cursor Ctrl+K - Kill all after cursor Alt+Bckspc – Kill previous word All kill commands place text into a buffer (aka “kill ring”). Ctrl+Y – Yank Alt+Y - Rotate kill ring, yank new top Technically yanks text from top of “kill ring”. To rotate through the ring you must first yank, then press Alt+M to rotate.
  14. 14. Shell Keyboard Navigation Use Tab for auto-completion! $ svn ci -m”Fixed issue.” fil[tab] ↓ $ svn ci -m”Fixed issue.” filename.php [Tab][Tab] shows all available matches: $ svn ci -m”Fixed issue.” f[tab][tab] filename.php frodo_baggins.php friendly_fellow.php Enable programmable bash completion for further efficiency (compete args). $ git c[tab][tab] checkout cherry cherry-pick ci clean clone co commit config
  15. 15. Shell Keyboard Navigation !! - Run the last command (same as ↑ +return) !my - Runs last cmd starting with ”my”, e.g., ” mysql -uroot -p db” !my:p - Find last cmd starting with ”my” and print it out vi !$ - Replaces !$ with last argument in previous command vi !* - Insert all arguments from previous command ^foo^bar - Run previous command but replace first “foo” with “bar” !!:gs/foo/bar – Run previous command, but replace all instances of “foo” with “bar” cd - - change to previous working directory A very useful (and more comprehensive) list is available here: http://mail.linux.ie/pipermail/ilug/2006-May/087799.html Also recommended: http://www.catonmat.net/blog/the-definitive-guide-to-bash-command-line-history/
  16. 16. Shell Basics: I/O Redirect Concept can be thought of as ” pipes ” and ” streams ” Negates the need for an application to write to a file, only for another application to read it.
  17. 17. I/O filters less – Interactive text pager with search capability. sort – Sort lines (alphabetically, numerically, etc) grep – Regular expression searches uniq – Detect (report or omit) repeated lines sed – Stream EDitor awk – powerful pattern scanning and text processing language xargs – build and execute commands from input cut - divide into columns There are a lot more of these tools!
  18. 18. I/O Basic example of standard output: # format an XML file and feed it out to formatted.xml $ xmllint feed.xml --format > formatted.xml # same as above, but append to existing file. $ xmllint feed.xml --format >> formatted.xml Standard input: $ mysql -uuser -p db1 < schema.sql Or both: $ mysql -uuser -p db1 < schema.sql > log.txt Can also redirect stderr (errors). Use the pipe! $ history | grep svn
  19. 19. I/O Examples of pipe chaining... Colorize Subversion diff output: $ svn diff | colordiff | less -R Search files for child classes, do not include test files, sort them alphabetically, and paginate. $ grep -r -n -H '^class.*extends.*{$' * | grep -v test | sort | more
  20. 20. I/O Example: # copy the latest changeset to a git-less server $ git log -1 --name-status | # only get files added or modified grep '^[AM]s' | # strip out status from stream sed 's/^[AM]s*//' | # scp to server (one at a time), retaining path xargs -I {} scp {} [email_address] :dir/{} (Disclaimer: There are better ways to do deploy changes.)
  21. 21. Holy smokes! How will I remember to type all that?
  22. 22. Let's automate this stuff!
  23. 23. Introducing Alias alias deploy=”git log -1 --name-status | grep '^[AM]s' | sed 's/^[AM]s*//' | xargs -I {} scp {} [email_address] :dir/{}” # Other Examples # List files in friendly format alias l='ls -l' # Colorize svn diff, use less -R to accept colors alias sdv='svn diff | colordiff | less -R' # Do not allow updates or deletes without a primary key alias mysql='mysql --i-am-a-dummy'
  24. 24. Alias Things to know: With alias, all arguments are appended to the command. Save your aliases to your .bashrc to automatically use in new sessions.
  25. 25. Alias # Usage: fun [name] alias fun='echo $1 is having fun!' $ fun Brian is having fun! Brian Alias is only useful as the beginning of a command. Arguments don't work well, can't do loops, etc.
  26. 26. Shell Functions Look up PHP command arguments: phpargs() { # $1 is the first argument passed # get php page from manual mirror curl -s http://us3.php.net/$1 | # pull all text in target div sed -n '/<div class=&quot;methodsynopsis dc-description&quot;>/,/</div>/p' | # strip out HTML tags sed 's/<[^>]*>//g' | # strip out line feed tr -d &quot; &quot; # add ending line echo } Then from the command line: $ phpargs in_array bool in_array ( mixed $needle, array $haystack [, bool $strict ] ) The only way to learn scripts is to write them!
  27. 27. Further Resources Comprehensive Introduction to the shell http://www.linuxcommand.org/learning_the_shell.php Bash keyboard shortcuts http://onlyubuntu.blogspot.com/2007/03/bash-shell-keyboard-shortcuts-for-linux.html Bash arguments http://www.deadman.org/bash.html Bash programmable completion: http://www.debian-administration.org/articles/316 http://freshmeat.net/projects/bashcompletion/ IBM Bash By Tutorial Series http://www.ibm.com/developerworks/library/l-bash.html http://www.ibm.com/developerworks/library/l-bash2.html http://www.ibm.com/developerworks/library/l-bash3.html Cat on Mat: http://catonmat.com/
  28. 28. Thanks! Any questions? Brian Dailey realm3 web applications Web: http://realm3.com/ Twitter: @brian_dailey Email: brian@realm3.com/ Phone: 917-512-3594 slide-bg: http://bit.ly/xc0m1 Kudos to: http://asi9.net/
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×