macOS LogomacOS is an operating system that is produced by Apple for its range of desktop and laptop computers. It was first released back in 2001 as a replacement for the Classic Mac OS operating system.

macOS is designed to allow the Apple hardware that it runs on to easily integrate with other Apple devices, such as iPhones, as well as services, such as iCloud, that allows for easy backing up of files.

Included with the operating system are a number of apps, or applications, for everyday tasks, which can be added to from those available in Apple’s own App Store. Many of these apps have similar or the same versions for other Apple devices, such as the iPhone, providing an easier transition between devices.

Shell Commands

The Shell is a command language interpreter that executes commands read from a standard input device, such as a keyboard, or from a file. Each command has a number of options, which tailor the output of the command in question. An option follows the command, and is made up of a '-', followed by one or more letters of the alphabet. These shell commands can be run within an application called 'Terminal'.


A backup to a disk image can be created as follows. The 'if' is used to specify the input file, whilst the 'of' is used for the output file.

dd if=/dev/sda of=/tmp/sda/image.img

This image can be restored by reversing the process.

dd if=/tmp/sda-image.img of=/dev/sda


Displays the name of the machine.


Displays the current user ID, together with the groups the account is in.


Displays the username of the current user.



The 'su' command, on its own, switches the user to the root user for the remainder of the terminal session. The password for the root user will need to be entered.


The command can also be used to switch to a named user. Again, the password for the user will need to be entered.

su adminuser

The 'sudo' command elevates privileges just for the command being run. This will only be successful if the current logged in user is an administrator. This can, for example, be used with the 'shutdown' command.

sudo shutdown -h now

df Displays the disk space available on all filesystems that the user has access to. In the below example, the '-h' ensures that it is in a human readable format, such as KB, MB or GB.
df -h


The 'man' command, short for manual, can be used in conjunction with a specified command, such as 'ls', to get help on this command. To exit the manual pages for a particular command, use the letter 'q'.

man ls


All files have permissions associated with them, for the owner, or creator, of the file, the group that the owner is in, along with everyone else. For each of these, permissions for reading, writing and executing the file in question can be assigned.

-rw-rw----   1 fredbloggs  examplegroup 0 26 Mar 17:43 file1.txt

The first dash signifies that these permissions are for a file. If it were a 'd', this would signify a directory. Following this, the 'rw-' relate to the read, write and execute permissions for the owner of the file, who, in this case, has read and write permissions. The next 'rw-' relate to the group, which again has read and write permissions. The final three dashes relate to everyone else. These permissions can be assigned to the file as follows.

chmod 660 file1.txt

Each digit relates to the decimal equivalent of the binary total for the owner, group and everyone else, with read permissions being equivalent to four, write is two and execute is one. As the owner and group have read and write permissions, it is four for read, plus the two for write.


The 'chown' command can be utilised to change the owner and group of a particular file or directory.

sudo chown fredbloggs:examplegroup file1.txt


In order to change the password of the current user, or a named user, the 'passwd' command needs to be used. Specifying 'passwd' on its own will allow for the changing of the current user password. Using 'passwd' in conjunction with 'sudo' and a username of a specified user, will allow the password to be changed for that user.

sudo passwd root


The 'ps' command can be used to list processes running on the system. Using it on its own will list the processes specific to the current user. Stipulating the 'e' option will include all processes, not just those for the current user.

ps -e

Using the 'kill' command, along with a process ID, will terminate that particular process.

kill 1234

If it is necessary to terminate all processes associated with a particular application, such as Firefox, then the 'killall' command can be used to achieve this. Note that elevated privileges are required to run this. It can also be used to abort a shutdown that has been scheduled for some point in the future.

sudo killall firefox
sudo killall shutdown


Clear the command prompt window of any previously run commands, along with their results.



Change the current directory. A '/' can be used to move to the route of the file system. Specifying a path moves to that location. The '..' moves up one level, whilst '~' returns the user to their home directory.

cd /
cd /bin 
cd ..
cd ~

Examine a file and return the file type.

file file1.txt

As the name suggests, the 'find' command can be used to locate a file or files. The first example below searches for files called 'more' starting at the root. Any errors that are returned are sent to a black hole using '2>/dev/null' so they are not displayed to the screen. The second example searches for all text files using the '*' wildcard, starting at the current directory and including its sub-directories.

find / -name "more" 2>/dev/null
find . -name "*.txt"

The 'grep' command can be used to search for a specified piece of text within a file, for example, the word 'help' in a file called 'file1.txt'.

grep help file1.txt

View the contents of a file. The spacebar can be used to page down in the file, whilst 'b' can be used to go up. The up or down arrow keys can be used to go up or down one line at a time. When done, press 'q' to return to the command prompt.

less file1.txt

Displays a list of files and subdirectories in a directory, either the current working directory, or one that is specified. The 'a' option includes files that begin with a '.', while the 'l' option signifies that full details should be shown such as, file permissions, user and group ownership, file size and a timestamp.

ls /bin
ls -al /bin

The 'more' command is similar to 'less' but with limited functionality. Here, the spacebar can be used to page down through a file, but there is no means to go back up.

more file1.txt

Display the current working directory.



Interface Configuration (ifconfig) can be used to view or change network interfaces and IP configuration. Run on its own, with no additional options, will list the active network interfaces, both wired and wireless, on the system.


Including the 'a' option adds in those interfaces which are currently down. This is often the default view, so may show the same output as running 'ifconfig' on its own.

ifconfig -a

To limit the results to just those interfaces that are down or inactive, the 'd' option can be used.

ifconfig -d

Similarly, to only view the interfaces that are up or active, the 'u' option can be added.

ifconfig -u

In order to view the configuration of a specific interface, its name can be specified as an option.

ifconfig en0

If an interface is currently inactive, 'ifconfig' can be used to make it active. Note that elevated privileges are needed for this.

sudo ifconfig en0 up

Similarly, if an interface is active, 'ifconfig' can be used to make it inactive.

sudo ifconfig en0 down


This can be used to lookup information from DNS servers, such as canonical names and IP addresses.


If you get a non-authoritative answer it means that it is from local cache, rather than the DNS server that would normally provide DNS information for the website in question.

A lookup can also be done in the other direction, by using an IP address.



Used to determine what type of network connections are occurring inbound as well as outbound from the machine in question. It shows details such as the protocol, local address, including the ephemeral port number, foreign address, including ephemeral port number, and the state.


Specifying the 'a' option includes server ports in the output.

netstat -a

The 'r' option shows the routing table, showing how packets are routed in the network.

netstat -r

The 's' option displays network statistics for all protocols, whether they are active or not.

netstat -s

The 'v' option includes more detail by adding a column to display the process ID (PID) associated with each open port.

netstat -v

This is a diagnostic tool that can be used to determines the route, from the current machine, to a destination, by sending Internet Control Message Protocol (ICMP) echo packets to the destination. The resulting information shows the time taken in milliseconds to each router, as well as the IP address. It should be noted that some routers are design not to return back any details, so you may not get all the information you require.


This also works with an IP address.



The 'shutdown' command can be used to shutdown or restart the system, either straightaway, using 'now', or a specified number of minutes in the future. A system shutdown is initiated with the 'h' option, with 'r' being required for a restart.

sudo shutdown -h now
sudo shutdown -h +30
sudo shutdown -r now
sudo shutdown -r +30


The 'ping' command is a utility that tests the reachability of a host over an IP network, such as a Local Area Network (LAN), or the Internet. It uses Internet Control Message Protocol (ICMP) echo request messages to contact the host and corresponding replies are received if the host is reachable. By default, echo requests are sent until Ctrl+C is used to stop the requests. Included with each response is the corresponding IP address, the round-trip time in milliseconds and the time to live (TTL), which signifies how many hops it took to reach the destination.


To specify the number of echo requests, the 'c' option can be used, followed by a number to limit the requests.

ping -c 4

An IP address can also be used with the 'ping' command, instead of a domain name.




The 'cat' command can be used to display the contents of a file or files in a terminal window. Where more than one file name is specified, the file contents is displayed one after the other.

cat file1.txt
cat file1.txt file2.txt

When combined with the 'sort' command, the file contents can be displayed in sort order. This can be further combined with the 'uniq' command to return the file contents in sort order, with any duplicates removed.

cat file1.txt | sort
cat file1.txt | sort | uniq

It can also be used to combine two or more files into a third.

cat file1.txt file2.txt > both.txt

If statistics are required about the contents of a file, this can be achieved with 'cat', combined with the 'wc' command. The number of words, characters, and lines will be returned. Again, this can be further extended to count unique words in a file.

cat unsorted | wc
cat unsorted | sort | uniq | wc -w

Copy one or more files to a different location. Here, a file is copied to the same location, but with a different name.

cp file1.txt file2.txt

In order to force a prompt to appear before a file is overwritten, the 'i' option can be used.

cp -i file1.txt file2.txt

To avoid overwriting an existing file, the 'n' option needs to be utilised.

cp -n file1.txt file2.txt

To preserve file attributes such as modification time, access time, file flags, file mode, the owner, and group, the 'p' option is required.

cp -p file1.txt file2.txt

Feedback can also be provided for each individual file being copied using the 'v' option.

cp -v file1.txt file2.txt

If more than one file of the same type, for example, text files, needs to be copied, the '*' wildcard can be used. In this instance, the files are copied to a folder called 'backup' that resides in the current location.

cp *.txt backup

As well as displaying text in the terminal, the 'echo' command can be used to append a line of text to the end of a file.

echo "This is file 1" >> file1.txt

View the first few lines of a file. By default this is the first 10 lines, however, this can be altered by using the 'n' option.

head file1.txt
head -n 20 file1.txt

The 'mv' command can be used to move one or more files to a different location, or rename a file in the current location.

mv file1.txt file2.txt

To move a file into a different location, the folder, or path needs to be specified, for example, a folder called 'backup' in the current location.

mv file1.txt backup

The '*' wildcard can be used to move multiple files, for example, all text files, to a specified location.

mv *.txt backup

The 'rm' command can be used to delete both files and directories. A single file can be deleted on its own, or multiple files can be deleted with the use of the '*' wildcard.

rm file1.txt
rm *.txt

In order to delete a directory, along with its contents, the 'r' option needs to be used, in conjunction with 'rm'.

rm -r backup

If it is necessary to create a directory from the command line, the 'mkdir' command can be used to achieve this.

mkdir backup

View the last few lines of a file. By default this is the last 10 lines, however, this can be altered by using the 'n' option. Specifying the 'f' option will update the terminal as the file changes.

tail file1.txt
tail -n 20 file1.txt
tail -f file1.txt

This command can be used to create a new, empty file.

touch file2.txt
vi Open a file for editing in the Vi editor. Note that this command also creates the file if it doesn't already exist.

vi file1.txt

Vi Commands

Vi (Visual editor) is a command line text editor that can be found in the UNIX, Linux and macOS operating systems. This is a glossary of some of the commands found in vi.

r Replace one character.
R Replace many characters, until Esc is pressed.
cw change the current word with new text, starting with the character under the cursor, until Esc is pressed.
cNw Change N words beginning with character under the cursor, until Esc is pressed e.g. c5w changes 5 words.
C Change (replace) the characters in the current line, until Esc is pressed.
cc Change (replace) the entire current line, stopping when Esc is pressed.
Ncc or cNc Change (replace) the next N lines, starting with the current line, stopping when is pressed.

yy Copy (yank) the current line into the buffer.
Nyy or yNy Copy (yank) the next N lines, including the current line, into the buffer.
p Put (paste) the line(s) in the buffer into the text after the current line.

x Delete a single character under the cursor.
Nx Delete N characters, starting with the character under the cursor.
dw Delete a single word beginning with character under the cursor.
dNw Delete N words beginning with character under cursor e.g. d5w deletes 5 words.
D Delete the remainder of the line, starting with the current cursor position.
dd Delete the entire current line.
Ndd or dNd Delete N lines, beginning with the current line e.g. 5dd deletes 5 lines.

:.= Returns the line number of the current line at bottom of the screen.
:= Returns the total number of lines at bottom of screen.
^g Provides the current line number, along with the total number of lines, in the file at the bottom of the screen.

:x Quit vi, writing out modified file to file named in original invocation.
:wq Quit vi, writing out modified file to file named in original invocation.
:q Quit (or exit) vi.
:q! Quit vi even though latest changes have not been saved for this vi call.

i Insert before cursor.
I Insert before line.
a Append after cursor.
A Append after line.
o Open a new line after current line.
O Open a new line before current line.

h Move left.
j Move down.
k Move up.
l Move right.
w Move to next word.
W Move to next blank delimited word.
b Move to the beginning of the word.
B Move to the beginning of blank delimited word.
e Move to the end of the word.
E Move to the end of Blank delimited word.
( Move a sentence back.
) Move a sentence forward.
{ Move a paragraph back.
} Move a paragraph forward.
0 Move to the beginning of the line.
$ Move to the end of the line.
1G Move to the first line of the file.
G Move to the last line of the file.
nG Move to nth line of the file.
:n Move to nth line of the file.
fc Move forward to 'c'.
Fc Move back to 'c'.
H Move to top of screen.
M Move to middle of screen.
L Move to bottom of screen.
% Move to associated ( ), { }, [ ].

/string Search forward for occurrence of 'string' in text.
?string Search backward for occurrence of 'string' in text.
n Move to the next occurrence of the search string.
N Move to the next occurrence of the search string in the opposite direction.

Vi can be started from the shell or terminal window.
vi filename Edit the named file starting at line 1.
vi -r filename Recover the named file that was being edited when the system crashed.

Useful Links


Sandboxes and Application Isolation