Command Line Cheat Sheet

January 30, 2016

Recently, I was trying to auto-launch multiple terminals with one command and got different terminals into different directories. A typical output of the solution on Mac is like:

mac-desktop Mac Desktop

In the process of searching for solution, I found the following command line cheat sheets useful for both Linux and Mac user. Sharing them in my post is to keep a record for myself, the credits go to the original authors, DavidChild and Onno Schwanen.


Bash Shortcuts

Mac                     Linux                   Description
Ctrl + C Ctrl + C Kill current command
Ctrl + Z Ctrl + Z Puts whatever you are running into a suspended background process. fg restores it.
Ctrl + A Ctrl + A Go to the beginning of the line you are currently typing on
Ctrl + E Ctrl + E Go to the end of the line you are currently typing on
Ctrl + U Ctrl + U Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + K Ctrl + K Clear the line after the cursor
Ctrl + L Ctrl + L Clears the Screen
Command + K   Reset the Terminal display
Ctrl + H Ctrl + H Same as backspace
Ctrl + R Ctrl + R Lets you search through previously used commands
Ctrl + D Ctrl + D Exit the current shell
Ctrl + W Ctrl + W Delete the word before the cursor
Ctrl + T Ctrl + T Swap the last two characters before the cursor
Ctrl + F Ctrl + F Move cursor one character forward
Ctrl + B Ctrl + B Move cursor one character backward
Esc + F Esc + F Move cursor one word forward
Esc + B Esc + B Move cursor one word backward
Esc + T Esc + T Swap the last two words before the cursor
Tab Tab Auto-complete files and folder names

Core Commands

Mac                     Linux                   Description
cd cd Home directory
cd [folder] cd [folder] Change directory
cd ~ cd ~ Home directory, e.g. ‘cd ~/folder/’
cd / cd / Root of drive
ls ls Short listing
ls -l ls -l Long listing
ls -a ls -a Listing all files
ls -lh ls -lh Long listing with Human readable file sizes
ls -R ls -R Entire content of folder recursively
sudo [cmd] sudo [cmd] Run command with the security privileges of the superuser (Super User DO)
open [file] open [file] Opens a file ( as if you double clicked it )
top top Displays active processes. Press q to quit
nano [file] nano [file] Opens the Terminal its editor
vi [file] vi [file] Opens the editor VIM
exit exit Exit
clear clear Clear screen
reset reset Reset the Terminal display

Command History

Mac                     Linux                   Description
history n history n Shows the stuff typed - add a number to limit the last n items
![value] ![value] Execute the last command typed that starts with ‘value’
!! !! Execute the last command typed

File Management

Mac                     Linux                   Description
touch [file] touch [file] Create new file
cat cat Concatenate to screen
cat [file1] [file2] cat [file1] [file2] Concatenate files and output
less [file] less [file] View and paginate file
file [file] file [file] Get type of file
rm [file] rm [file] Remove a file
rm -i [file] rm -i [file] Remove with confirmation
rm -r [dir] rm -r [dir] Remove a directory and contents
rm -f [file] rm -f [file] Force removal without confirmation
cp [file1] [file2] cp [file1] [file2] Copy file1 to file2
cp [file] [dir] cp [file] [dir] Copy file to directory
mv [file1] [file2] mv [file1] [file2] Move/Rename file1 to file2
head [file] head [file] Show first 10 lines of file
tail [file] tail [file] Show last 10 lines of file
tail -F [file] tail -F [file] Show last lines of file as it changes

Directory Management

Mac                     Linux                   Description
pwd pwd Full path to working directory
.. .. Parent/enclosing directory
. . Current folder
mkdir [dir] mkdir [dir] Create new directory
mkdir -p [dir]/[dir] mkdir -p [dir]/[dir] Create nested directories
rmdir [dir] rmdir [dir] Remove directory ( only operates on empty directories )
rm -R [dir] rm -R [dir] Remove directory and contents

Pipes - Allows to combine multiple commands that generate output

Mac                     Linux                   Description
more more Output content delivered in screensize chunks
> [file] > [file] Push output to file, keep in mind it will get overwritten
» [file] » [file] Append output to existing file
< < Tell command to read content from a file

Help

Mac                     Linux                   Description
[cmd] -h [cmd] -h Offers help
[cmd] –help [cmd] –help Offers help
[cmd] help [cmd] help Offers help
man [cmd] man [cmd] Show the help for ‘command’
whatis [cmd] whatis [cmd] Gives a one-line description of ‘command’

 

Yingzhou Li

Discussion

I'm a graduate student at Stanford University. Follow me on Github and Bitbucket. Hopefully, you will find my code useful. You are also welcome to either email me or post comments below to discuss on these posts.