############################################################################### # $Id$ # # Linux command line Shortcuts and Tricks # Doc at: http://quicloud.com/blug/shortcuts-and-tricks.txt # # Some useful command-line shortcuts and tricks to help LAMP stack sysadmins # In examples below: # PROMPT> = your linux prompt # [bracketed-text] = some text you'll want to replace when you run # ^ = "CTRL" (^R = CTRL+R) # # Course online at: http://quicloud.com/blug/ # Accepting applications for next course (Feb 27, 2011)! Contact rICh for details # # @author rICh ################################################################################ ################################################################################ # Faster SSH connections # http://www.revsys.com/writings/quicktips/ssh-faster-connections.html # Add to LOCAL (not server) ~/.ssh/config ################################################################################ Host * ControlMaster auto ControlPath /tmp/%r@%h:%p # Add this to ~/.subversion/config, if using SVN #[tunnels] #ssh = ssh -o ControlMaster=no ################################################################################ # shortcuts on command line ################################################################################ ^a = "go to start of line" ^e = "go to end of line" ^k = "cut from this point forward" ESC-d = "cut word after cursor" ^w = "cut word before cursor" ^y = "paste from clipboard" (^a^k #copy current command; #do something; ^y #paste command back) ################################################################################ # navigation (moving around) ################################################################################ cd - "send me back to my last directory" PROMPT> pwd /some/super/long/directory/i/would/hate/having/to/retype/ PROMPT> cd /etc #i go there, do some stuff, then want to go back PROMPT> cd -; pwd; /some/super/long/directory/i/would/hate/having/to/retype/ ################################################################################ # useful wildcards ################################################################################ * = 'zero or more chars' {.php,.inc} = 'any of enumerated values' # examples: PROMPT> ls -lat *.php #list all "*.php" files PROMPT> ls -alt .php* #list all files that end with "php[something-or-nothing]" PROMPT> ls -lat *unstable* #list all files that have 'unstable' anywhere in filename PROMPT> rm -rf *{.bak,.orig,#,~} #remove all cruft in a directory (good to alias!) # cd - "send me back to my last directory" PROMPT> pwd /some/super/long/directory/i/would/hate/having/to/retype/ PROMPT> cd /etc #i go there, do some stuff, then want to go back PROMPT> cd -; pwd; /some/super/long/directory/i/would/hate/having/to/retype/ ################################################################################ # browsing man (searching back & forward) ################################################################################ # I remember "find" command had a "changed xxx minutes ago" switch... what was it?! PROMPT>man find # pulls up manpage /minute # starts scanning forward n # find next N # find prev ################################################################################ # history (do something you did before) ################################################################################ # !! "run last command over again" sudo !! # run last command as sudo (after getting permision denied) # !!:p "print out (but don't run) last command" sudo !!:p # i want to review the command before I let sudo do it's stuff # history | grep [partial-command]; ![history ID] "lets you look up history & then run command over again" PROMPT> awk '{print $1}' /var/log/apache/access.log | sort -u > my-unique-ips # i ran this command 5 days ago ... then i did a bunch of other stuff ... now I want to run it again, but all I can remember about running it is "my-unique-ips" PROMPT> history | grep my-unique-ips 1031 awk '{print $1}' /var/log/apache/access.log | sort -u > my-unique-ips PROMPT> !1031 # runs that command over again #NOTE: ^R also lets you search history in reverse (^R [TYPE TEXT] [^R TO STEP BACK]) -- can never remember that one, myself # history -d [history ID] "lets you delete an item from your history PROMPT> mysql -uroot -pNoOneShouldKnow my-production-database # Aw NUGGETS!!! I shouldn't have done that PROMPT> history ... 1032 mysql -uroot -pNoOneShouldKnow my-production-database PROMPT> history -d 1032 #delete "1032" from history PROMPT> history ... 1032 history -d1032 ... now i can run the command minus "-p" to keep my password from appearing in history # !$ "last argument from last command" PROMPT> ls -lat myfile -rw-rw-r-- 1 rich dev 3380 Jan 12 18:37 myfile PROMPT> cat !$ # pass last argument ("myfile") to "cat" command # !^ "first argument from last command" PROMPT> cp file-im-gonna-edit.php file-im-gonna-edit.php.bak PROMPT> vim !^ # open "file-im-gonna-edit.php" in vim # !* "all arguments from last command" PROMPT> cp file-im-gonna-edit.php file-im-gonna-edit.php.bak PROMPT> emacs !* # open "file-im-gonna-edit.php" AND file-im-gonna-edit.php.bak in emacs # ^[search]^[replace] "run las command, replacing text" PROMPT> find . -type f -name '*.php' -min -60 | xargs grep google-analytics #really long, and i borked it in the middle find: invalid predicate `-min' PROMPT> ^-min^-mmin #fixes bork # !!:gs/[search]/[replace]/ "does same thing as ^^, but replaces multiple instances (^^ only does one) PROMPT> cat ~john/.ssh/ ################################################################################ # copying, renaming, moving. single or groups of files ################################################################################ # rename s/search/replace/ *.[file-extension] "rename batches of files" PROMPT> ls file1.class.php file2.class.php file3.class.php file4.class.php PROMPT> rename s/.class.php/.php/ *.class.php file1.php file2.php file3.php file4.php # cp [filename]{.bak} back up a working file before making a big change to it (rename something *.bak) PROMPT> cp my-file-that-is-totally-working-right-now.php{,.bak} # copies to new file with ".bak" at end # did you know that "diff" works on directories? PROMPT> diff ~tarzan ~jane # shows files which exist in on dir, but not another... list file diffs if in both ################################################################################ # apache ################################################################################ # Rewrite Rule to redirect 'shop.[domain.com]' to https. RewriteCond "%{SERVER_PORT}" "^80$" RewriteRule "^(/shop/.*)" "https://%{HTTP_HOST}$1" [R, L]