Love Your Command Line

1,278 views

Published on

Brief hands on tutorial on using the command line. Presentation from BlogHer Geek Lab and She's Geeky. Beginning commands

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

  • Be the first to like this

No Downloads
Views
Total views
1,278
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
19
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Love Your Command Line

  1. 1. Stupid Unix Tricks or Love Your Command Line! Liz Henry BlogHer 2009
  2. 2. The command line is like your trusty robot friend always waiting for you to tell it what to do! How do I get to the command line? MacOS or Linux: Terminal Windows: HyperTerminal or PuTTY, CygWin You can get these slides from my blog or we'll download them at end of session, so don't worry about taking notes!
  3. 3. Navigation: Where are you? pwd Print(current) working directory ls “list” files in the directory you're in. Type this a lot! cd change directory (blank will take you to your home dir) mkdir dev Make a directory called “dev” in your current working directory cd dev change into that directory! whee! ls What's in there? Probably nothing since you just made it. pwd where are you again? up arrow key Push it a few times. It shows the commands you last typed.
  4. 4. Navigation: Where are you? echo $SHELL What command line shell am I in? tcsh, csh, bash? whoami So existential! What's my username? hostname What's the name of the computer or server I'm on? ping google.com Are you on the net? (Control-C to stop this) ifconfig Really, how about that network!? Cryptic... traceroute google.com Where am I really? In relation to somewhere else? easy challenge: Poke around and see what other environment variables you can echo! Hint, type “printenv” harder challenge: change your prompt to be in color and tell you what directory you're in (look up later and try it)
  5. 5. Look around some more! man ps alias woman=”man” alias wom=”man” wom ls Q to quit reading man pages alias ls = “ls -lah” ps ps -x Whoa! ps -x | more Pipes the output into a paging program Q to quit ps -x | grep Term Pipe into “grep” to search for specific process top Info about top processes (Q to quit)
  6. 6. Get remote files First use curl or wget to download two sample text files. curl -O http://bookmaniac.org/stuff/slides/example.tar.zip or wget http://bookmaniac.org/stuff/slides/example.tar.zip ls There's the file, tarred and zipped up tar -xvzf example.tar.zip Uncompress the files! ls Reassure yourself... cat bloglist.txt Take a look at the file contents cat stats.txt ls -lahFSr Fancy! sorts by file size. Challenge for later: Download Wordpress and unzip it from the command line Download some WP themes and unzip them! Challenge: Download and install wget if you don't have it. Then, try downloading an entire web site with its files and images 2 levels deep: wget --wait=9 --recursive --level=2 http://example.org/
  7. 7. Manipulate text files sort bloglist.txt This sorts on the first field, alphabetically sort stats.txt sort stats.txt > tmp.txt Sorts into a new file called tmp.txt sort stats.txt | uniq > tmp.txt Sorts, then pipes output to uniq, then writes to a file sort -t',' -k 2,2 stats.txt Sorts with comma as field separator, on field 2 sort -t',' -k 2,2 -nrg stats.txt -nrg means general numerical sort, reverse order There are many more tricks and oneliners, try out all the options for sort, and search for “sort examples unix” or “sort examples command line”
  8. 8. Manipulate text files Manipulate text files – Awk is weird but fun awk '{print $1}' bloglist.txt Prints first field, field separator is a blank space awk -F"," '{print $1}' stats.txt Print first field with a comma as field separator awk -F"," '{print $2 $1}' stats.txt Print both fields, the other way round awk -F"," '{print $2 "," $1}' stats.txt Do that again but with a comma separator awk -F"," '{print $2 "," $1}' stats.txt | sort -nr Pipe it through sort, numerical, reversed! awk -F"," '{print $2 "," $1}' stats.txt | sort -nr | mail -s 'Stats from me' lizhenry@gmail.com Email me the results! (“Stats from me” is the subject line)
  9. 9. Manipulate text files Manipulate text files – grep and regular expressions Even grep baby talk is incredibly useful! Regular expressions are fantastic. * is a wildcard. It means “match anything” grep Sarah bloglist.txt Shows any lines where “Sarah” appears in that file grep Sarah *.txt Shows any lines containing “Sarah” in any file in this directory grep "S" *.txt Lines containing a capital S grep -r sidebar *.php Look in this directory, and all the ones below it, for any php files that contain the pattern “sidebar” grep -v widget *.css Look in all the css files in this directory for the word “widget” grep -r sidebar *.php | grep -v test As above, but then send the output into another grep filter that will return every line that doesn't match the pattern “test”
  10. 10. Manipulate text files – comm, sort, and join comm bloglist.txt stats.txt Compare 2 files. Output is in 3 columns Col 1: lines unique to file 1 Col 2: lines uniq to file 2 Col 3: lines that appear in both files head -1 stats.txt >> bloglist.txt Stick the 1 st line from stats at the end of bloglist. comm bloglist.txt stats.txt Now there is a line in the 3 rd column! comm -12 bloglist.txt stats.txt Suppress column 1 and column 2. sort -t"," -k2,2 bloglist.txt > blogs-sorted.txt Sort on url sort stats.txt > sorted-stats.txt Sort this file on url too
  11. 11. Manipulate text files – Apache logs if on blogherista server acct, cd cd examples ls more access.log A sample bit of an Apache log file. awk '{print $1}' access.log | sort | uniq -c | sort -nr pull out the IP numbers of people accessing the site sort them and unique them, counting (-c) the times they appear and sort again numerically, reversed, to see who visits your site most often Try this with the other fields too. What files are accessed most often? What's the most commonly used browser? nslookup 195.240.190.227 Spy on whoever it is a little. whois 195.240.190.227 Some ISP in the Netherlands is downloading my music. Challenge: Go through every example on the fabulous “Awk one-liners” page! http://www.catonmat.net/blog/wp-content/uploads/2008/09/awk1line.txt
  12. 12. Other Useful Stuff to Try scp filename.txt [email_address] :path/to/files/ Copy some files from where you are to a remote account. tar -cvzf target-filename.tar.gz file1.txt file2.txt *.html *.jpg Compress and zip some files. ftp -i [email_address] Handy. -i means you don't have to type y y y at every file it's uploading. make a bunch of aliases in your .bash_profile or other shell profile. Understand the drwxrwxrwx stuff from the ls -a command. And learn to use chmod. Try a lot of grep examples. Do the Bash for Beginners tutorial. Have fun!

×