Like many software engineers, I spend a decent amount of time on the command line. It’s an invaluable tool that I use daily and one that I simply could not be without. Whilst graphical user interfaces (GUIs) do make life easier, sometimes you need to drop to the command line to get the job done.
Whilst this post is aimed at commands and tools for macOS, many of them will work on Linux as well. I frequently swap between Mac and Linux distros such as Ubuntu and CentOS and rarely have an issue with a command or tool not being available. If it’s not, there’s usually an alternative that works just as well.
In addition to the list of commands, I've compiled some examples for them. This should give you a headstart on getting up to speed with them.
1. ls (the list command)
The ls command is likely one of the very first commands that most people learn when beginning to use the terminal. I mean, you need to know what files you're dealing with in the current directory, right?
# Show me what is in the current directory ls # Show me the contents of the directory but with more information ls -l # Show me hidden files and directories too ls -la # Show me things with more readable file-size information ls -lh
2. cd (changing directories)
This is likely one of the other commands people first learn about. Once you've learned about the directories at your disposal via the ls command, you're likely going to want to jump into a few. This is where the cd command comes in. It’s simple in nature and allows us to move around in directories.
# Go to a directory that is available from the current location cd ./a_directory # Go to a very specific directory cd ~/home/user/project/my_cool_project # Go home cd ~/ # Go home (but shorter) cd # Go to the previous directory that you were in cd - # Go up one directory cd ..
3. open (open files and directories)
The open command does what it says on the tin. It opens files and directories. However, it's fairly configurable and you can do some interesting things with it.
# Open the Spotify app (assuming it's installed) open -a Spotify # Open a link using your default browser open "https://www.chriswales.uk" # Open a markdown file using the specified application (overriding the default) open -a Safari document.md
Another surprisingly simple command is the clear command. It 'clears' the screen. This is pretty much essential if you want any readability on your terminal — especially if you're running a lot of commands and things are becoming a tad difficult to read.
There's also a neat variation of this in the form of a keyboard shortcut using CMD + K. This doesn't clear the screen in the same way, but actually appears to delete the previous terminal output. Depending on your preferences, you may prefer one or the other.
# Clear the screen clear
5. man (read the manual)
The man page command is a very useful resource that is handy to get acquainted with. It is a digital manual for commands that you want to learn about. For instance, if you want to learn more about the ls command, e.g. how to change how it is formatted, you could look up this information on the man page for the command. If you’re not into using the terminal for reading man pages, there are also various websites you can visit to read man pages such as ss64.org.
# Find out about that `ls` command man ls # Read the man page about the `man` command! man man
Sadly, the cat command has nothing to do with cats. Instead, it echoes the contents of files. If we take a look at the man[^1] page, we can see that the name of the cat command is derived from the word concatenate. Simply, this command is used to print or "concatenate" file content. It's super useful if you want to quickly see the contents of a file or copy the contents of a file and pipe it into another file.
# Echo the contents of one file cat file.txt # Echo multiple files cat file1.txt file2.txt # Echo the contents of file(s) into another file cat file1.txt file2.txt file3.txt > combined_file.txt
I probably use this every day. The ssh command — meaning secure shell — is a utility for connecting to and executing commands on remote machines in a secure manner. This particular command is essential if you need to work with multiple machines — especially if they're in a different physical location.
# Connect to the remote machine ssh username@hostname_or_ip # Connect using a specified port ssh username@hostname_or_ip -p 20123 # Connect using a specific key ssh -i /path/to/key username@hostname_or_ip # Run a command instead of opening a tty ssh username@hostname_or_ip -p "cat some_file.txt"
8. git (track changes to your work)
Git, a tool that describes itself on its' man page as a "stupid content tracker", is one of the best (and most frustrating) tools that I use daily. Git is a revision control system that can track changes made to files. I use it for pretty much most text documents I write and all of my personal and professional code repositories. Git is so renowned for being simultaneously brilliant and infuriating, that there have been many a comic made about it such as the one in Figure 1. Despite its faults, If I can store it in Git, I probably will.
# Create a brand new repository. An exciting moment in any project git init # Add all the files in the current directory git add . # Commit files with the default text editor git commit # Commit files but specify commit message inline git commit -m "my commit message" git remote add origin ssh://your_repo_address git push origin master
9. pbcopy (copying stuff)
The pbcopy command is short for pasteboard copy (e.g. your clipboard). This is a macOS-specific command, but there are others as well such as xclip or xsel on their respective Linux distros. It's useful for copying things in the terminal without using your mouse to highlight them. It has a sister command called pbpaste, but I've never found much use for it as of yet.
pbcopy < ~/.ssh/a_key.pub
10. time (timing your commands)
The command is used to measure how long a program or command takes to run. If you're developing some program and want to see how long it takes to run, the time command is a good, but slightly crude, way in which you could evaluate it. I have frequently used it in the past for longer running commands to estimate how long it takes — purely so I know how long I should expect to wait in the future when rerunning the command.
A heads up, you may need to use an explicit path to use the time command with flags. This is likely because time is a reserved word. To find the location of the command, you can use the snippet below which will probably print something like /usr/bin/time.
# Time the sleep command, and sleep for 2 seconds # Outputs: sleep 2 0.00s user 0.00s system 0% cpu 2.010 total time sleep 2 # Find out where time is # Outputs: # time is a reserved word # time is /usr/bin/time type -a time # Run the time command with the -p and -h flags which makes it # more readable and outputs each stat to its own line # Outputs: # real 5.01 # user 0.00 # sys 0.00 /usr/bin/time -ph sleep 5
11. networksetup (manage your network configuration)
I discovered this one fairly recently. It allows you to configure your network from your terminal without needing to open up system preferences manually. I use it to quickly connect to a VPN occasionally and it's just much nicer than having to click through the various levels of menus and buttons needed to get to the settings on macOS.
# Connect to your VPN networksetup -connectpppoeservice "myVPN" # Disconnect from the VPN networksetup -disconnectpppoeservice "myVPN"
Admittedly, I don't use this one a whole lot, but I always end up googling it or whatnot at some point after I've forgotten about it. This command is used to clear the DNS cache on your Mac. Our machines cache things at so many different levels, so if you're having issues with a domain or server not being available, or pointing to the wrong place, this command is a good one to try out in your efforts to resolve the issue.
# Flush DNS cache sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
13. du (how big is this thing?)
This is a useful command if you want to see disk usage of a file or directory. You can achieve this with ls (but it defaults to bytes and carries with it a lot of other information that can be distracting). I use the du command frequently when connecting to remote servers. Paired with the -h (human-readable) and -s (summary) flags, it results in a readable summary of what you need to get information about.
du -sh ./a_directory_or_file
14. caffeinate (stay awake)
Caffeinate is useful if you need to leave your computer for a while if it's doing some long-running task. The Caffeinate command prevents your Mac from sleeping whilst idle. In my job, I occasionally need to leave my computer for a few hours at a time if I'm running a one-off command e.g. a database migration that takes a while. After all, I don't want to sit and prod my computer all evening. I can run this command and then just leave my computer until it's done.
# Stay awake for an hour caffeinate -u -t 3600 # Don't let the display turn off caffeinate -d # Stay awake until the command has finished caffeinate -i super_long_command
The alias command is great for those who want to tailor their shell experience to themselves a bit. It allows you to create custom shortcuts or abbreviations for commands or programs. It's excellent if you want to cut down on the verbose commands which use a lot of options and whatnot. The only caveat is that you need to add these aliases to your shells configuration file (e.g. .bashrc, .zshrc) if you want them to persist between sessions! After you have created your alias, you can use it like any other command.
# Show me all the current aliases alias # Create an alias to open a connection to a remote computer alias remote_station= 'open vnc://192.X.X.X' # Shortcut git to g alias g=git
16. Python's HTTP Server
This is one of my favourite commands (okay, technically not a command, but whatever). I don't necessarily use it often, but I love it because of how simple and flexible it is. It's super convenient for when you have some HTML files that you want to look at in your browser.
python3 -m http.server 9000 -d folder_name
17. Oh My Zsh
Oh My Zsh is a framework managing and extending Zsh (the default macOS shell nowadays). Oh My Zsh has many great features such as themes, plugins, tab completion, and more. Even better, it's open-source! One of my favourite features is the git integration. In Figure 2, you can see my standard shell screen which shows an example of the git integration. It shows what branch I'm on and will indicate if there are changes present (the cross in the picture shows I've made some changes).
There are a variety of text editors available in your terminal. Vim happens to be my favourite. Don't misunderstand me here, I'm no Vim aficionado (I wish I was, but I love my IDEs a bit too much), but I know enough to get around. There are others such as Vi, Nano, GeEdit, Emacs, etc; whatever you choose is up to you. It's my opinion that you should at least know one.
# Open an empty buffer vim # Open a specific file vim ~/test_file.txt
19. code (VS Code)
I use this command almost daily. It's extremely useful if you need to examine or edit a somewhat more complicated, or longer, file. The code command allows you to launch Visual Studio Code (VS Code) from the command line. If you're unfamiliar with VS Code, I'd definitely recommend it. It's a fast and powerful text editor that is very flexible in what it can do. I mainly use it for light text editing where I don't need the power of a fully-fledged IDE. The command to open VS Code is also super quick and the editor launches almost instantly.
# Open VS Code as if you had just pressed the app icon code # Open VS Code in the current directory code . # Open a specific file or directory code /path/to/file.txt
N.B. If you have VS Code installed, but this does not work for you, jump into VS Code and run the following command from the command palette: "Install 'code' command in PATH".
Homebrew is one of the best macOS tools out there. Homebrew is macOS's defacto unofficial package manager. It is an open-source tool that allows you to install software from the command line and is built using Ruby. If I'm setting up a new macOS install, it's one of the very first things I install. With it, you can install most things ranging from CLI tools like the Wget command to fully-fledged applications such as Postman.
# Search what install candidates there are for Python brew search python # Install Python 3.11 brew install [email protected] # Install tmux brew install tmux # Install a fully-fledged application brew install —cask balsamiq-wireframes # List what formulae and casks are installed on our system brew list
Downloading YouTube videos used to be simpler. Luckily, some smart people developed youtube-dl to help people download youtube videos easily. It also works for quite a few other video-sharing sites as well.
# Download a cool YouTube video youtube-dl 'https://www.youtube.com/watch?v=J—-aiyznGQ'
22. pandoc (converting documents)
Pandoc is a cool CLI tool that converts documents from one format to another. I use it sometimes when I want to quickly convert Latex documents into plain text, HTML, or markdown. It's a neat tool and even works with Word documents which is super handy.
# Convert and print directly in the terminal pandoc —to=plain document.tex # Convert a markdown document to HTML pandoc report.md -o report.html # Same as above and serve with our handy Python HTTP server! pandoc report.md -o report.html && python3 -m http.server 9000
As you can see, the possibilities using the command line are endless — and we have barely scratched the surface of what it is capable of. It's an extremely versatile tool that can do seemingly magical things. If you are not already familiar, I encourage you to not be afraid and dive in! At the very least, I hope you have learned about a new command that you can use in your day-to-day hacking.
Christopher Wales is a software engineer based in Edinburgh, Scotland. He enjoys learning about and practicing multiple software development discplines such as application development, web development, DevOps, and Artificial Intelligence. He works as a Software Engineer at Rohallion where he develops custom web solutions and holds a first-class degree with honours in Computer Science from Edinburgh Napier University.