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

Handy Shell Commands

Handy Shell Commands

A few handy shell commands, from basic to less basic.

Mike Klemarewski

August 06, 2015
Tweet

More Decks by Mike Klemarewski

Other Decks in Programming

Transcript

  1. Navigation Go to home directory cd Go to previous directory

    cd - Alias commands in your ~/.bashrc alias ..="cd .."
  2. List directory contents ls options -l : display long output

    format -h : prefix bytes as KB MB etc. -A : display all directories and files accept for . and .. -G : enable colored output alias ll="ls -lhAG"
  3. Commands for Amnesia Find out where you are pwd Find

    out what user you are logged in as whoami
  4. Last command Get the last command !! Redo the last

    command with a typo gir init ^gir^git
  5. Look farther back in time Find out what you have

    done history Reverse search for a command control + r
  6. grep Searches input files for lines that match a given

    pattern options -i : case insensitive -r : recursive search -l : only display the filename -n : display line number Highlight search: export GREP_OPTIONS='--color=auto' GREP_COLOR='0;35'
  7. wc Display the number of lines, words or bytes in

    the file or standard input -l : display line count -w : display word count
  8. find Recursively descends each path evaluating a given expression. #

    Find all markdown files in the current directory find . -depth 1 -name "*.md"
  9. ps Lists running processes options -a : display info about

    other users processes as well -x : display processes running without a terminal window -u : display the user that the process was started by
  10. # Start a bunch of servers for i in `seq

    3333 3343`; do serve -p $i > /dev/null & done # list all the processes with "serve" ps aux | grep serve # in your ~/.bashrc alias pg="ps aux | grep"
  11. kill Send a signal to the process Signals: 9 KILL

    15 TERM kill PROCESS_ID pkill node /Users/username/local/bin/serve
  12. xargs Takes strings and passes them to a utility as

    arguments options -n : limit number of arguments taken from standard input for each invocation -p : prompt the user for every invocation find . -name "*.md" | xargs -p -n1 rm
  13. Other aliases To expand an alias: hit esc then control

    + e # List all the processes listening on ports alias ports="sudo lsof -i -P | grep -i 'listen'" # Open the last executed command in 'explainshell.com' alias explain='history | tail -2 | head -1 | sed -e '"'"'s/^\ *[[:digit:]]*\ *//'"'"' | php -R "echo urlencode(\$argn);" | sed -e '"'"'s/^\(.*\)/\"http:\/\/www.explainshell.com\/explain?cmd=\1\"/g'"'"' | xargs -I{} open -a "/Applications/Google Chrome.app/" {}'