Skip to content

Command line Cheat Sheet

Lance Pollard edited this page Sep 26, 2012 · 7 revisions

This is a generic/intro guide to the bash shell.

The reason for this is to provider relatively new coders with a simple reference to the command-line.

General Notes/Tips

  • Press tab after typing a few letters for tab-completion.
  • If you're on a Mac, iTerm is a better terminal to use.
  • The \ backslash is used to write a command across multiple lines (for readability).

Open New Terminal Tab from Command-line on Mac

osascript -e 'tell application "Terminal" to activate' -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down'

File System Commands

Print Current Directory

pwd

Print directory structure

find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'

But better, install this: http://mama.indstate.edu/users/ice/tree/

Changing Directories

cd ./packages
cd packages

List Files in Directory

ls

Moving Files

mv packages lib

Copying Files and Directories

cp ./lib/tower.js ./tmp/tower.js

Create a File

touch ./tmp/tower.js

Create a directory

mkdir ./tmp
# make nested directories
mkdir ./tmp/The\ Beatles\White\ Album\ Disc\ 1

Removing Files and Directories

rm -rfv ./tmp

Symlinking Files

Make a file "executable"

chmod 755 ./bin/tower

Append content to a file

Replace content in a file

Searching files

Inspecting a File's Metadata

# on Mac
mdls ./bin/tower

Mac Spotlight actually uses that information when you search.

Unzipping a File

Zipping a File

Open a File

open ./bin/tower

Open current directory

open .

If you put this command in your ~./bash_profile it will open that file when you start your computer.

HTTP Commands

Downloading Files

# `L`: follow redirect if server is throwing some intermediate redirect.
# `O`: save as local file(s) rather than sending to STDOUT.
curl -L https://github.com/downloads/viatropos/tower/tower.png -O

Uploading Files

curl -X POST \
  -F attachment[files][]="[@some/image.png;type=image/png" \
  http://localhost:3000/attachments.json

GET JSON

curl -H "Accept: application/json" -H "Content-type: application/json" \
  http://localhost:3000/posts.json

POST JSON

curl -H "Accept: application/json" -H "Content-type: application/json" \
  -X POST \
  -d '{"post":{"title":"A Post!"}}' \
  http://localhost:3000/posts.json

PUT JSON

curl -H "Accept: application/json" -H "Content-type: application/json" \
  -X PUT \
  -d '{"post":{"title":"New Title"}}' \
  http://localhost:3000/posts/301860cb52ad676200000003.json

DELETE JSON

curl -H "Accept: application/json" -H "Content-type: application/json" \
  -X DELETE \
  http://localhost:3000/posts/301860cb52ad676200000003.json

Downloading a Website

wget  -r -e robots=off --wait 0.25 --random-wait -N --user-agent "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_7) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5" "http://example.com/"

Downloading Large (or Lots) of Files

You can use rsync, but I would use rsync through wget:

wget

Simulating Web Traffic (Load Testing)

Apache bench.

ab -n 1000 -c 5 http://localhost:3000/posts.json

Login to Remote Command-line

ssh username@hostname

System Commands

Show Operating System Version

uname -a

Show Your IP Address

ifconfig

See your most recent commands

history

...and make aliases for them:

history | awk '{print $2}' | awk 'BEGIN {FS="|"} {print $1}' | sort | uniq -c | sort -r

Print IP Address

Show executable path

which tower

List Processes

ps -ax

Kill a Process

Logging

tail -n 500
top

Limits

ulimit -n 10240

Take a snapshot

# Mac:
screencapture -iW ~/Desktop/screen.jpg  # currently focused window
screencapture -S ~/Desktop/screen.jpg   # entire desktop

Copy to clipboard

# copies the directory listing
ls -l | pbcopy

Paste from clipboard

# get pasteboard lines containing foo and save them in a_file
pbpaste | grep foo > a_file

Shell Variables

Environment Variables

Custom Variables

Pipes

Filters

Keyboard Events

File System Events

Configuring the Shell

~/.bash_profile

~/.bashrc

~/.profile

Command Summary

  • sed
  • awk
  • grep
  • find
  • which
  • chmod
  • curl
  • wget
  • touch
  • cp
  • cd
  • pwd
  • ls
  • kill
  • ps
  • wc
  • cat
  • echo
  • ping
  • sudo
  • history
  • alias
  • sort
  • uniq
  • open

Random Useful Mac Commands

# eject disk
drutil tray open
# open movie with vlc
open -a vlc movie.avi

Useful Command-line Tools

  • pdftotext
  • brew
  • git
  • ImageMagick

Resources

Clone this wiki locally