December 29, 2020

Mac Terminal command-line interface (CLI) cheatsheet

Last updated on March 26th, 2021

summary


This is the syntax I use throughout this cheatsheet:

syntax description
$ a shell prompt
text in monospaced font a command or other terminal text
[text in brackets] an optional parameter
underlined text a parameter you must replace with a value
[…] a parameter that might be repeated
/ the top level directory a.k.a. folder
. the current directory
.. the parent directory
~ the home directory

editing shortcuts description
Ctrl ⌃ + A then Ctrl ⌃ + K
or Ctrl ⌃ + E then Ctrl ⌃ + U
delete the entire line of text
Ctrl ⌃ + U delete the line of text before the cursor
Ctrl ⌃ + K delete the line of text after the cursor
Ctrl ⌃ + H
or delete
delete the character before the cursor
Ctrl ⌃ + W delete the word before the cursor
Ctrl ⌃ + A move cursor to beginning of the line
Ctrl ⌃ + E move cursor to end of the line
Ctrl ⌃ + T swap the last two characters before the cursor
Esc + T swap the last two words before the cursor

navigation commands description
$ pwd display path to current directory
$ cd [~] go to home directory
$ cd directory-relative-path go to directory
$ cd / go to root of the drive
$ cd - go back to previous directory
$ cd .. go to parent/enclosing directory
$ cd ../.. go to parent/enclosing directory of parent/enclosing directory
$ q exit
$ clear
or Ctrl ⌃ + L
or Command ⌘ + K
clear screen
Tab auto-complete file and directory names

history commands description
$ history n display n previously typed commands
arrow up display previously typed commands one by one
Ctrl ⌃ + R then search-query then Ctrl ⌃ + R to display anterior match search history of commands previously used in Terminal

file and directory management commands description
$ ls display list of items in current directory
$ ls -a display list of items and hidden items of current directory
$ ls -lh display list of items with details, with size in human readable format
$ ls -lSh display list of items with details, sorted by size in descending order, in human readable format
$ touch file-name.extension-name create a new file
$ cp source-path destination-path copy a file
$ mv current-file-name new-file-name rename a file
$ mv source-path destination-path move a file
$ mv keyword* destination-path move all files starting with a specific keyword
$ mv *keyword* destination-path move all files including a specific keyword
$ rm file remove file
$ rm -i file remove file with prompt asking for confirmation yes/no
$ rm -r directory remove a directory and contents
$ mkdir name-of-new-directory create a directory
$ cat file display the content of a file in the Terminal
drag the file or folder to the Terminal window display the path of a file or folder
$ find directory -name file find a file in a directory
$ open file open a file with the default application
$ open file -a application open a file with a specific application
$ open directory -a application open all files in a directory with a specific application

applications management commands description
$ open -a application-name open an application from any directory (-a)
$ open -a tor browser” open an application (with a space in its name) from any directory
$ open https://yctct.com open a url in your default browser
$ nano open terminal text editor

system commands description
$ df -h display disk usage information of a file system in human readable format
$ du -sh display size information of a file or a directory in human readable format
$ du -sh * display size information of items in a directory in human readable format
$ date display current date and time
$ TZ=time-zone date display current date and time of a particular time zone (you can find the list of time zones in the zoneinfo file located at /usr/share/zoneinfo on a Mac).
$ cal display current month calendar
$ cal year display 12-month calendar of year
$ caffeinate -d prevent display from sleeping
$ caffeinate keep Mac awake as long as the Terminal is running. To stop process: Ctrl ⌃ + C
$ caffeinate -t n in second keep you Mac awake for a set number of second
$ diskutil unmount drive unmount drive (e.g. USB stick, hard drive), so that you can physically remove it safely (i.e. “eject safely”)
$ sudo shutdown -r now restart a Mac
$ sudo shutdown -h now shutdown a Mac

CLI-based application commands description
$ curl wttr.in display weather of current location (based on IP address)
$ curl wttr.in/city display weather in any city (blog post coming soon, subscribe to RSS feed)
$ wikit search query display summary of a Wikipedia page

command-line interface (cli)

No affiliates, No (Google) Analytics. © 2016-2023 yctct.com. Content is licensed under CC BY-NC-SA 4.0 .