I remember the first time I opened up the terminal in macOS. I felt like Indiana Jones slowly stepping forward into the holy grail temple hoping I was righteous enough to pass without getting sliced by the traps set somewhere in my computer’s operating system to prevent naive venturers such as myself. Alas, with the right set of tools not only is it possible to dodge these traps, but you will likely grow to enjoy using terminal for certain tasks more than using the main graphic OS. The main obstacle to overcome is fear. When I first started, I had this massive fear of typing the wrong thing and crashing my whole computer and losing all my data.
First, before you do anything in terminal: Back everything up! In fact this is key engineering lesson #1: Always have backups. Much like climbers rope up in case they fall attempting a move, having backups a) lets you relinquish the anxiety of crashing things and b) gives you room to start experimenting. And engineering is all about experimenting. I won’t cover this too much other than to mention that pretty much anything in your computer can be backed up in the cloud in Dropbox, Box, iCloud, Github, Google Drive, etc. Do this first and then let’s get started.
One thing to note first is the OS you know and love was designed to be forgiving. Terminal is not. The computer only understands EXACTLY what you tell it. Imagine trying to talk to someone in Beijing and if you’re off by the slightest from a perfect accent they don’t understand a word you said. If you meant to type “cp file_abc.txt /usr/local/me” instead of “cp file_ABC.txt /usr/local/me” and guess what… the computer will try to copy the file named file_ABC.txt instead of file_abc.txt. It will see that file_ABC.txt doesn’t exist and throw the error telling you it doesn’t exist. Precision is the name of the game here and many a times you just find yourself trying different permutations until you get it right.
The good news is macOS’s terminal and many flavors of Linux come with autocomplete in bash by pressing tab. This will become your best friend. While sometimes autocomplete fails when you send text messages to your friends, if you type tab after a few characters and bash doesn’t autocomplete, it means something is wrong. You may be in the wrong folder, or wrong server, or you misspelled the first few letters, etc. If not, it will show you autocomplete options you can choose from without needing to type the whole raw command.
Now each command is actually just a program. For those of you coming from business if I type $open file.xls, the program “open” will run, see the excel file, find the program associated with that file and then open it. In my prior example, cp is the program name for copying a file from one folder to another. This is the terminal equivalent of dragging a file from one folder to the next. To run any program just type its program name followed by what input it takes (if the program takes inputs such as folders in our copy example). Also normally programs have what are called flags that modify the program in some way to achieve a different result. For instance “ls” is the command for listing out all the files in a directory. The flag -a lists all the files including hidden files and -l includes the permissions and other details of the files. -h turns the file sizes into “human readable format”. Thanks computer!
My favorite part of using terminal over anything else is how easy it is to install programs. Operating systems come with programs called package managers that allow you to download and install the program you want to install in one step. On Mac it’s called “homebrew”, in Linux there are a few but the most popular is “apt-get”. In fact even programming languages come with package mangers that can be used to install dependencies (other people’s code you can import and use for yourself). Python’s is called “pip”, Node JS uses “npm”. Unfortunately homebrew doesn’t come pre-installed with MacOS but installing it is easy. Just copy and paste from homebrew’s main site:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
That should install homebrew so now installing programs is as easy as typing $brew install [whatever program you want]. Want to install python? Simple as $brew install python3
Here are a few my most commonly used commands (Dollar sign indicates the start of the terminal line and brackets indicate a placeholder. No need to type either the brackets or the dollar sign):
- Change working directory: $cd → Example $cd home
- List files in the current directory: $ls
- Show current working directory: $pwd
- Copy a file: $cp → Example: cp /Users/Me/file.txt /Users/You/file.txt
- Move a file: $mv → Example: mv Users/Me/file.txt /Users/You/file.txt → I tend to use cp instead of mv as if you make a mistake cp leaves the original file alone.
- Go backward a directory: $cd ..
- Current directory $./ → Example: if we are in /Users/Me/home the same can be denoted as ./home
- Homebrew commands: $brew install [name of program to install]
- See content of a file: $cat → Example: $cat fileabc.txt
- See a live log of a file as it changes: $tail -f → Example: $tail -f logfileabc.log
- Change permissions: $chmod [permissions] → See this chmod cheat sheet
- logging into a remote server: $ssh [server ip address] → Normally a username / password are required
- Checking if a site or ip address works: $ping [ip or site]
- See all running processes and cpu usage: $top
- See processes running: $ps
- Stringing together commands: $| → Example: $ps | grep java
- Search: $grep → Example searching for processes running java $ps| grep java
Makeuseof has this amazing cheat sheet that I refer to all the time. As you start to use terminal more, much like speaking a real language, you’ll need the cheat sheet less and less and sooner than you think you’ll start typing away like you’ve been doing it since 1999.