Command Guide for Mac & Linux Terminal
Author: Diego Cuevas
Originally published: 11 March 2022
Updated: 25 March 2022
The Terminal! That mythical app you may come across while exploring your Mac’s or Linux PC’s apps. For most people it’s an utility program which they’ll probably never use, but for a programmer it’s where they’ll live the rest of their lives. Here’s a comprehensive list of useful commands and shortcuts to use while in the Terminal:
Shortcuts:
- Tab -> Auto-complete file and folder names
- Ctrl + A -> Go to the beginning of the line
- Ctrl + E -> Go to the end of the line
- Ctrl + L -> Clear screen
- Ctrl + C -> Kill/stop ongoing processes
Basic:
sudo
-> Run with administrator/super-user security privilegesman [cmd]
-> Display manual page of specified commandpwd
-> Display current pathdate
-> Display current datecal
-> Display calendar of current month
Directory management:
cd [dir]
-> Enter directory, use apostrophes for names with spacescd ..
-> Move up to the parent directorycd ../..
-> Move up two directoriesls
-> Display list of subdirectories and files:-a
-> Also display hidden items,-l
-> Long listing,-h
-> Display file sizes,-t
-> List by time modified (most recent first),-s
-> Sort by size.mkdir [dir]
-> Create directoryrmdir [dir]
-> Delete directory, it only works on empty directoriesrm -r [dir]
-> Delete a directory and its contentsmv [dir] [otherDir]
-> Move directory to a different directory or subdirectorymv [dir] [newDirName]
-> Rename directory
File management:
./[exec]
-> Run executable fileopen [file]
-> Open file:-a
-> Specify app (Mac)cp [file] [dir]
-> Copy file to directoryrm [file]
-> Delete a file:-i
-> Prompt confirmation,-f
-> Force removalmv [file] [newfile]
-> Rename a filemv [file] [dir]
-> Move file to a different directory or subdirectorycat > [file]
-> Create text file and insert text, specify file extensioncat [file]
-> Display text filehead [file]
-> Display the first 10 lines of a text file:-n
-> Show first “n” lines of a text filetail [file]
-> Display the last 10 lines of a text file:-n
-> Show last “n” lines of a text filewc [file]
-> Word counter, displays number of lines, words and characters in that order:-l
-> Display number of lines only,-w
-> Display number of words only,-c
-> Display number of characters only.
Permissions:
chmod
-> Change the permissions of a file, directory, link or executable. It uses codes to activate different permission settings: r = 4, w = 2 and x = 1, the sum will generate the permission codes for each of the three groups of permission settings: users, groups and others
Disk management:
diskutil
-> Disk Utility (Mac)cd /volumes/[drive]
-> Access Flash Drive, SD Card or Hard Drive (Mac)
Package management:
port
-> MacPorts package manager:install [package]
-> Install package,uninstall [package]
-> Uninstall package (Mac)*brew
-> Homebrew package manager:install [package]
-> Install package,uninstall [package]
-> Uninstall package (Mac)*apt
-> Apt package manager:install [package]
-> Install package,remove [package]
-> Uninstall package (Linux: Ubuntu, Mint, Kali)apk
-> Apk package manager:add [package]
-> Install package,del [package]
-> Uninstall package (Linux: Alpine)
Extra:
shutdown
-> Shut down the computer,-h [hh:mm]
-> Shut down the computer at a specific timesay [word(s)]
-> Text-to-speech utility (Mac)espeak [word(s)]
-> Text-to-speech utility (Linux)
Git:
git init
-> Initialize repository.git add .
-> Adds files that aren’t already being tracked to the repositorygit status
-> Check the status of the repositorygit commit -m "[message]"
-> Make a commit and add a messagegit log
-> Show the log of commitsgit checkout .
-> Revert back to the last commitgit merge [branch]
-> Merge current branch with other [branch]
*Usually not pre-installed in the system