Mac Terminal Commands | Mac Terminal Commands Cheat Sheet

This is a command line tutorial primarily conducted in the macOS High Sierra command line. The command line can be a scary place when you first encounter it but fear not it’s really not that difficult to use.

Use shortcuts to save time when using Terminal.[Read: Mac Terminal Shortcut Keys]

Q: How to open the command line?

Step 1: Click on Finder
Step 2: Click on Applications present on Left sidebar
Step 3: Search for Utility Folder and double click on Utility Folder
Step 4: Click on Terminal.

OR

Press Command + Space to open spotlight search, and type terminal and hit return.

Anatomy of the Console

First, let’s clarify a few terms:
Console: This is the system as a whole. This is both the command line as well as the output from previous commands.
Command Line: This is the actual line in a console where you type your command.
Prompt: This is the beginning of the command line. It usually provides some contextual information about who you are, where you are and other useful info. It typically ends in a $. After the prompt is where you will be typing commands.
Terminal: This is the actual interface to the console. The program we use to interact with the console is actually a “terminal emulator”, providing us the experience of typing into an old-school terminal from the convenience of our modern graphical operating system.


Command Syntax

Nearly all commands follow a common pattern with 3 main parts. 
  1. Program(utility)
  2. Options(flags), and 
  3. Arguments
Let’s see an example

$ ls -l ~/Desktop


Let's break this command down into parts:
  • ls is a utility. Utilities are also sometimes known as commands all on their own, because they indicate the general idea of what you want. Most of the time, you can simply run a utility all by itself, without any flags or arguments. Most commands only have one utility.
  • -l is a flag that alters how the utility operates. Flags are like options or preferences: the utility will usually work perfectly well with the defaults, but sometimes, you want to modify how it works slightly. Flags always start with either one or two dashes (-), and they usually come between the utility and the arguments.
  • ~/Desktop is an argument to the utility. Arguments are used when the utility needs to know exactly what you want for a certain action, and there is no clear default setting. You can think of it more like a conversation than an argument: The utility says "I don't know how I should do this!", and you use an argument to say, "Here, this is how you should do it." Arguments usually come at the end of the command, after the utility and the flags (if any flags are used). The number of arguments used generally depends on the utility: some don't need any arguments, some require exactly one argument, some require lots of arguments, and some are flexible in the number of arguments they can take.

CORE COMMANDS

cdHome directory
cd [folder]Change directory
cd ~Home directory, e.g. ‘cd ~/folder/’
cd /Root of drive
lsShort listing
ls -lLong listing
ls -aListing incl. hidden files
ls -lhLong listing with Human readable file sizes
ls -REntire content of folder recursively
sudo [command]Run command with the security privileges of the superuser (Super User DO)
open [file]Opens a file
open .Opens the directory
topDisplays active processes. Press q to quit
nano [file]Opens the Terminal it’s editor
pico [file]Opens the Terminal it’s editor
qExit
clearClear screen

COMMAND HISTORY

history nShows the stuff typed – add a number to limit the last n items
ctrl-rInteractively 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
pwdFull path to working directory
..Parent/enclosing directory, e.g.
ls -l ..Long listing of parent directory
cd ../../Move 2 levels up
.Current folder
catConcatenate to screen
rm [file]Remove a file, e.g. rm [file] [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 to 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 operates on empty directories )
rm -R [dir]Remove directory and contents

PIPES – Allows to combine multiple commands that generate output

moreOutput 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 fi

HELP


[command] -hOffers help
[command] —helpOffers help
[command] helpOffers help
resetResets the terminal display
man [command]Show the help for ‘command’
whatis [command]Gives a one-line description of ‘command’

Comments