Command Line Cheat-Sheet

command line header image

The Command Line is a text based interface that exists on every operating system in some form, eg. Terminal on the Mac. By typing commands in the Terminal you can access files, install and manipulate programs and communicate with the computers internal workings. Pretty much everything is available via Terminal

Programmers use the Command Line to install packages, eg through npm, to aid coding and developing their projects. It’s also used to upload and download projects to platforms like Github. The Command Line can seem a little daunting, but with a few basic commands, like below, will give you a basic understanding of how the Command Line works.

//SHORTCUTS

Tab //Auto-complete files and folder names

Ctrl + A //Go to the beginning of the line you are currently typing on

Ctrl + E //Go to the end of the line you are currently typing on

Ctrl + U //Clear the line before the cursor

Ctrl + K //Clear the line after the cursor

Ctrl + W //Delete the word before the cursor

Ctrl + T //Swap the last two characters before the cursor

Esc + T //Swap the last two words before the cursor

Ctrl + R //Lets you search through previously used commands

Ctrl + L or Command + K //Clears the Screen

Ctrl + C //Kill whatever you are running

Ctrl + D //Exit the current shell
//CORE COMMANDS

pwd //Print working directory - displays the file path from the root directory to the current directory

cd //Home directory

cd [folder name] //Change to named directory

cd ~ //Home directory, e.g. cd ~/folder/

cd / //Root of drive

ls //Short listing

ls -l //Long listing

ls -a //Listing incl. hidden files

ls -t //Lists all content by time they were last modified

ls -lh //Long listing with Human readable file sizes

ls -R //Entire content of folder recursively

sudo [command] //Run command with the security privileges of the superuser (requires password)

open [file] //Opens a file

open . //Opens the directory

top //Displays active processes. Press q to quit

nano [file] //Opens the Terminal editor

pico [file] //Opens the Terminal editor

q //Exit

clear //Clear screen
//COMMAND HISTORY

history n //Shows the stuff typed - add a number to limit the last n items

ctrl-r //Interactively search through previously typed commands

![value] //Execute the last command typed that starts with value

!! //Execute the last command typed
//FILE MANAGEMENT

touch [file] //Create new file in the current working directory

pwd //Full path to working directory

.. //Parent/enclosing directory

ls .. //Long listing of parent directory

cd ../../ //Move 2 levels up

. //Current folder

cat //Concatenate to screen

rm [file] //Remove a file

rm -i [file] //Remove with confirmation

rm -r [dir] //Remove a directory and contents

rm -f [file] //Force removal without confirmation

rm -i [file] //Will display prompt before

cp [file] [newfile] //Copy file

cp [file] [dir] //Copy file to directory

mv [file] [new filename] //Move/Rename, e.g. mv -v [file][dir]
//DIRECTORY MANAGEMENT

mkdir [dir] //Create new directory

mkdir -p [dir]/[dir] //Create nested directories

rmdir [dir] //Remove directory (only works with empty directories)

rm -R [dir] //Remove directory and contents
//PIPES
//Allows to combine multiple commands that generate output

more //Output content delivered in screensize chunks

> [file] //Push output to file, keep in mind it will get overwritten

>> [file] //Append output to existing file

< //Tell command to read content from a file
//HELP

[command] -h //Offers help

[command] --help //Offers help

[command] help //Offers help

reset //Resets the terminal display

man [command] //Show the help for command

whatis [command] //Gives a one-line description of command

Similar in Cheat-Sheets