Mac Terminal Commands and Tricks List

0
Mac-Terminal-Commands

Many MacOS users may find the Terminal and its commands daunting at first glance. Using the Mac Terminal might appear as if you’re attempting to compromise a system, although that’s certainly not the intention. Familiarizing yourself with Mac Terminal commands, however, can empower you to excel in the MacOS environment.
macos-terminal

What is a Terminal on Mac?

ad

As a Terminal emulator, it grants you text-based access into the operating system, offering a swifter means of navigating the operating system if you are well-versed in MacBook terminal commands. Once you grasp its fundamental functions, you can tackle more complex tasks, and using the Terminal can become an enjoyable experience.

Although mastering the Terminal won’t instantly transform you into an IT specialist, it does mark the initial stride toward that objective if that’s your ambition.

How to open the Terminal on Mac?

The Terminal application can be found in the Utilities folder within the Applications directory. You have multiple options for accessing it:

ad

  1. Through Finder, follow this path to locate the Terminal application: Applications > Utilities > Terminal
  2. Alternatively, you can use Spotlight Search. Simply press the Command and Space keys simultaneously to open the Spotlight Search bar. Then, type “Terminal” and click on the first suggestion.

Using either of these two methods will open the Mac command prompt, which appears as a black box, as shown below.

mac terminal-window

The appearance of your Terminal may vary depending on your settings. To make it more tailored to your preferences, it’s advisable to adjust the appearance, and you can achieve this by changing the Terminal theme.

Here’s how you can do it:

  1. While in Terminal, go to the top menu bar and select “Preferences.”
  2. open-mac terminal-preferencesIn the preferences menu, go to the “Profiles” tab.
  3. Here, you can customize the Terminal’s appearance, including options such as background color, text color, and font.

mac terminal-appearance

Terminal tricks you should know

Navigating the Terminal may not be as intuitive as navigating a graphical user interface (GUI) application. Therefore, it’s essential to familiarize yourself with some key aspects when learning to use the Terminal.

  1. Navigation: You can use either the arrow keys or the mouse to move within the Terminal. If you need to edit a previously entered command, navigate left or right using the arrow keys to the desired edit point. Alternatively, you can use the mouse by holding the ALT key and clicking at the desired position within the line.
  2. Command History: To reuse previously entered commands, employ the up and down arrow keys. These keys allow you to cycle through your command history, eliminating the need to retype the same command repeatedly.
  3. Exact Typing: useful terminal commands for mac must be entered precisely as shown, with each character, space, and symbol being critical. Omitting or altering any character can yield unexpected results.
  4. Execution: After entering a command, press the return/enter key to execute it. If you wish to halt a running command, simultaneously press CTRL + C. This will interrupt the command’s execution.
  5. File Location: Keep in mind that Terminal commands operate within the current file location. To change the file location, you’ll need to use a Terminal command, which will be introduced below.

| Read More:-  Windows Terminal Download for PC

Mac Terminal Commands List

A multitude of commands is available for your use. Let’s concentrate on a selection of the most frequently used MacOS Terminal commands that are essential for mastering the Terminal.

Shortcuts commands

COMMANDACTION
TabAuto-complete file and folder names
Ctrl + AGo to the beginning of the line you’re currently typing on
Ctrl + EGo to the end of the line you’re currently typing on
Ctrl + UClear the line before the cursor
Ctrl + KClear the line after the cursor
Ctrl + WDelete the word before the cursor
Ctrl + TSwap the last two characters before the cursor
Esc + TSwap the last two words before the cursor
Ctrl + LClear the screen
Ctrl + CKill whatever you’re running
Ctrl + DExit the current shell
Option + →Move cursor one word forward
Option + ←Move cursor one word backward
Ctrl + FMove cursor one character forward
Ctrl + BMove cursor one character backward
Ctrl + YPaste whatever was cut by the last command
Ctrl + ZPuts whatever you’re running into a suspended background process
Ctrl + _Undo the last command
Option + Shift + Cmd + CCopy plain text
Shift + Cmd + VPaste the selection
exitEnd a shell session

Basics commands

COMMANDACTION
/ (Forward Slash)Top-level directory
.Current directory
.. (Double Period)Parent directory
~ (Tilde)Home directory
sudo [command]Run command with the security privileges of the superuser
nano [file]Opens the Terminal editor
open [file]Opens a file
[command] -hGet help about a command
man [command]Show the help manual of the command

Change Directory commands

COMMANDACTION
cdHome directory
cd [folder]Change directory, e.g., cd Documents
cd ~Home directory
cd /Root of the drive
cd –Previous directory or folder you last browsed
pwdShow your working directory
cd ..Move up to the parent directory
cd ../..Move up two levels

List Directory commands

COMMANDACTION
lsDisplay the name of files and subdirectories in the directory
ls -CForce multi-column output of the listing
ls -aList all entries including those with .(period) and ..(double period)
ls -1Output the list of files in one entry per line format
ls -FDisplay a / (slash) immediately after each path that is a directory, * (asterisk) after executable programs or scripts, and @ after a symbolic link
ls -SSort files or entries by size
ls -lList in a long format. Includes file mode, owner and group name, date and time file was modified, pathname, and more
ls -l /List of the file system from root with symbolic links
ls -ltList the files sorted by time modified (most recent first)
ls -lhLong listing with human-readable file sizes in KB, MB, or GB
ls -loList the file names with size, owner, and flags
ls -laList detailed directory contents, including hidden files

File Size and Disk Space

COMMANDACTION
duList usage for each subdirectory and its contents
du -sh [folder]Human-readable output of all files in a directory
du -sDisplay an entry for each specified file
du -sk* | sort -nrList files and folders, totaling the size including the subfolders. Replace sk* with sm* to list directories in MB
df -hCalculate your system’s free disk space
df -HCalculate free disk space in powers of 1,000 (as opposed to 1,024)

File and Directory Management

COMMANDACTION
mkdir <dir>Create a new folder named <dir>
mkdir -p <dir>/<dir>Create nested folders
mkdir <dir1> <dir2> <dir3>Create several folders at once
mkdir “<dir>”Create a folder with a space in the filename
rmdir <dir>Delete a folder (only works on empty folders)
rm -R <dir>Delete a folder and its contents
touch <file>Create a new file without any extension
cp <file> <dir>Copy a file to the folder
cp <file> <newfile>Copy a file to the current folder
cp <file>~/<dir>/<newfile>Copy a file to the folder and rename the copied file
cp -R <dir> <“new dir”>Copy a folder to a new folder with spaces in the filename
cp -i <file><dir>Prompts you before copying a file with a warning overwrite message
cp <file1> <file2> <file3>/Users/<dir>Copy multiple files to a folder
ditto -V [folder path][new folder]Copy the contents of a folder to a new folder. In here “-V” print a line of status for every file copied
rm <file>Delete a file (This deletes the file permanently; use with caution.)
rm -i <file>Delete a file only when you give confirmation
rm -f <file>Force removal without confirmation
rm <file1> <file2> <file3>Delete multiple files without any confirmation
mv <file> <newfilename>Move/rename
mv <file> <dir>Move a file to the folder, possibly by overwriting an existing file
mv -i <file> <dir>Optional -i flag to warn you before overwriting the file
mv *.png ~/<dir>Move all PNG files from the current folder to a different folder

Permission Management

Changing permissions for files and folders is a crucial step in safeguarding resources from unauthorized access. Permissions can be granted for reading, writing, and executing by the owner, a specific group, or others.

To check the permissions of a file, you can employ the “ls -l” command, which we discussed in the “List Directory Commands” section. This command displays a list of all files and folders in a long format. In the left-most column, you’ll notice a notation like the following:

  • Example 1: “rw-r–r–“
  • Example 2: “drw-r-xr-x”

These notations consist of nine slots organized into three groups, each designated for a different type of user: owner, groups, and others. In the first example, “rw-r–r–,” the owner has read (r) and write (w) permissions but lacks execute (x) permission. Both groups and others have only read (r) permission.

In the second case, “drw-r-xr-x,” in addition to the standard permission sets, there is a “d” character indicating that this is a directory.

Before delving into specific commands for changing permissions, it’s essential to become acquainted with octal permission notation.

PermissionOctal ValuePermission Notation
No permission0
Execute1–x
Write2-w-
Write and Execute3-wx
Read4r–
Read and Execute5r-x
Read and Write6rw-
Read, Write and Execute7rwx

The Terminal application allows you to set octal notation permissions for files and directories. Let’s look at what commands you can use.

COMMANDACTION
ls -ldDisplay the default permission for a home directory
ls -ld/<dir>Display the read, write, and access permission of a particular folder
chmod 755 <file>Change the permission of a file to 755
chmod -R 600 <dir>Change the permission of a folder (and its contents) to 600
chown <user>:<group> <file>Change the ownership of a file to user and group. Add -R to include folder contents

Process Management

COMMANDACTION
ps -axOutput currently running processes. Here, ‘a’ shows processes from all users and ‘x’ shows processes that are not connected with the Terminal
ps -auxShows all the processes with %cpu, %mem, page in, PID, and command
topDisplay live information about currently running processes
top -ocpu -s 5Display processes sorted by CPU usage, updating every 5 seconds
top -o rsizeSort top by memory usage
kill PIDQuit process with ID <PID>. You’ll see PID as a column in the Activity Monitor
ps -ax | grep <appname>Find a process by name or PID

Network Management

COMMANDACTION
ping <host>Ping host and display status
whois <domain>Output whois info for a domain
curl -O <url/to/file>Download file via HTTP, HTTPS, or FTP
ssh <username>@<host>Establish SSH connection to <host> with user <username>
scp <file><user>@<host>:/remote/pathCopy <file> to a remote <host>
arp -aView a list of all devices on your local network. It will show you the IP and MAC address of all the devices
ifconfig en0View your device IP and MAC address
traceroute [hostname]Identify the path and the hops traversed by the packets from your device to the destination address

Homebrew commands

COMMANDACTION
brew doctorCheck brew for potential problems
brew helpList of useful homebrew formula and cask commands
brew install <formula>|<cask>Install a formula or cask
brew uninstall <formula>|<cask>Uninstall a formula or cask
brew list –formulaList only installed formulas
brew list –caskList only installed casks
brew deps <formula>|<cask>List all the dependencies of a formula or cask
brew search text|/regex/Search formula or cask through regex
brew upgrade <formula>|<cask>Upgrade the formula or cask
brew outdated <formula>|<cask>Search for outdated formula or cask
brew outdated –formulaSearch for outdated formula
brew outdated –caskSearch for outdated cask
brew pin [installed_formula]Pin a formula from getting upgraded
brew unpin [installed_formula]Unpin to upgrade a package
brew cleanupRemove stale lock files and outdated packages for all formula and casks

Environment Variable or Path

COMMANDACTION
printenvDisplay a list of currently set environment variables. Also tells you which shell you’re using
$echoTells the terminal to print something and show it to you
echo $PATHCheck the value of the PATH variable which stores a list of directories with executable files
echo $PATH > path.txtExport the path directory to a text file
export PATH=$PATH:absolute/path to/program/Execute a program via terminal only in your current session. If you use a program regularly, add the path to shell configuration file.

Search commands

COMMANDACTION
find <dir> -name <“file”>Find all files named <file> inside <dir>. Use wildcards (*) to search for parts of filenames
grep “<text>” <file>Output all occurrences of <text> inside <file> (add -i for case insensitivity)
grep -rl “<text>” <dir>Search for all files containing <text> inside <dir>

Output commands

COMMANDACTION
cat <file>Output the content of <file>
less <file>Output the contents of <file> using the less command that supports pagination and more
head <file>Output the first 10 lines of <file>
<cmd> >> <file>Appends the output of <cmd> to <file>
<cmd> > <file>Direct the output of <cmd> into <file>
<cmd1> | <cmd2>Direct the output of <cmd1> to <cmd2>

Determining File Type

Sometimes the extension will not help you in determining the type of file. In that case the following command can help you pinpoint the exact type.

COMMANDACTION
file <file>Determines file type

FAQ’s

What is the purpose of the Terminal on Mac?

The Terminal on Mac serves as a text-based interface to interact with the operating system. It allows for efficient navigation and control using Mac Terminal commands, making it a powerful tool for tasks beyond what a graphical user interface (GUI) can accomplish.

How can I change permissions for files and directories using the Terminal?

You can change permissions using the chmod command followed by the desired permission settings. For example, chmod 755 filename grants read, write, and execute permissions to the owner and read and execute permissions to the group and others. You can also use chown to change the ownership of files and directories.

How can I determine the file type in the Terminal?

To determine the file type in the Terminal, use the file command followed by the filename. This command will provide information about the type of file, helping you identify it even if the file extension is not informative.

Is using the Terminal on Mac difficult for beginners?

For beginners, the Terminal on Mac may initially seem daunting. However, with practice and an understanding of basic commands, it becomes accessible and can greatly enhance your Mac experience.

What are some important tips for navigating the Terminal efficiently?

Navigating the Terminal effectively requires some key skills:

  • Use arrow keys or the mouse to move within the Terminal.
  • Navigate command history with the up and down arrow keys to avoid retyping commands.
  • Enter commands accurately, as even small deviations can lead to unexpected results.
  • Execute a command by pressing Enter, and halt a running command with Ctrl + C.

Conclusion

the macOS Terminal and its commands, though initially intimidating, offer powerful tools to enhance your Mac experience. Learning these commands, whether you’re a novice or aiming for advanced skills, allows for efficient navigation and a wide range of tasks. Embrace the Terminal to unlock your Mac’s full potential and excel in the macOS environment.

ad