We've all seen that little black icon labeled "Terminal" hidden in a utilities folder on our Mac, but what really is it? Well, it is a very important part of how your computer functions, and it's crucial for you to understand if you want to call yourself a professional developer.
In this post, we'll go over what the command line is, as well as some commands to get started using it.
What Is Terminal?
Terminal is an interface for you to execute text-based commands, and it gives you direct access to the underlying operating system, via a shell, on your Mac. A long time ago, before computers had graphical user interfaces (GUIs), the only way to operate a computer was through the command line. Needing to run the computer through the command line helped users better understand what was going on inside their computer.
Ever since GUIs became more commonplace, we have almost forgotten about the command line, and the typical Mac user probably doesn't even know that it exists. This may be okay for the layperson, but as software developers, it's still crucial that we know how to use the terminal.
Why Use the Terminal?
Wasn't the whole point of graphical user interfaces to eliminate the need for a command line? Well, not really. There are still several reasons that you should learn how to use the Terminal if you are a developer, or even a user who cares about performance:
- Certain things simply cannot be done without the command line (especially as a developer). These include installing CocoaPods, using a git repository, and more.
- You can easily make changes to your computer's settings which would be tedious or even impossible to do using the user interface only.
- The terminal is very flexible, and once you learn to use it, it is quite simple and straightforward.
- It is a very direct way of telling your computer what to do, whereas a GUI is akin to controlling the computer from a distance.
Those are just a few of the reasons that the Terminal is still a useful tool today. It's a practical and functional tool to improve and enhance your daily interactions with your computer.
Terminal is located in your Applications folder within a subfolder called Utilities. If you cannot find it, activate Spotlight search by pressing Command-Spacebar and search for Terminal. Once you have it open, you should see something which looks like this:
This is called the "command prompt"—you'll see the current directory, then your username followed by a
To run a command, simply type it in at the command prompt and press Enter. Try it now with the command
ls—this will list all the files in the current directory. Often, online instructions will write commands to be entered starting with a
$. You don't need to type this out. For example:
$ cd ~/Desktop
For the command above, you would just type
cd ~/Desktop, omitting the
Basic Commands Cheat Sheet
Here is a list of some of the basic commands that you'll need to know in order to use the terminal effectively. This is a handy cheat sheet for the most important and most commonly used commands.
help—as the name suggests, you can type this command into the Terminal to get a list of different commands.
man <command name>—similar to the previous command, this command tells you exactly what a command does (and gives you full documentation) without you having to search Google for it.
ls—lists all of the contents in the current directory. This command comes in handy if you don't want to use the Finder to browse files—you can simply list them using this command in your Terminal.
cd <directory name>—this command is used to change directories. If you write
cdalone, you will move out of the current directory. After using
ls(to see the directories), you can write the name of the directory you want to enter.
defaults <setting to change>—this command is used to modify default system settings, some of which cannot be changed without using the terminal.
caffeinate—as the name suggests, this command is used to prevent your Mac from dimming, turning off, or sleeping. To end this, all you need to do is press Control-C.
vim <file name>—this is one of my favorites. You can edit text files using the default TextEdit (or any GUI based editor), but
vimis basically a command-line text editor—that is, it works entirely within the terminal.
ping <URL or IP Address>—this command allows you to check the server response time of a specified URL or IP Address. This may not be useful for every developer, but it is nice to know.
sudo <action to perform>—a way to override your user's privileges and become a superuser of the computer. You will be prompted for an admin password when you use it.
Now that you know a few of the basic commands, let's go a little bit more in depth to take a closer look at five of the things you can do with the Terminal.
1. Change the Default Format for Screenshots
We all know about using Command-Option-4 to take screenshots on our Macs. And although it's great that these screenshots are taken in high-quality PNG files, we don't necessarily want 15 MB to 20 MB of our precious storage taken up for every single screenshot. Luckily, you can change this default with the Terminal.
After your prompt (remember, it's that
$ sign), type in the following command:
$ defaults write com.apple.screencapture type jpg
After you press Enter, you will notice that the file type of your screenshots has changed to what you just set it to. You can use any format such as PDF, JPG, and more, but for this example I have used JPG.
2. Stress Test Your Mac
There is a simple command which many Apple technicians use when your computer is acting up. This command stresses your CPU by simply printing the letter "y" over and over. If you open your Activity Monitor, you will soon see that this process takes up most of your CPU!
In order to do this, after the prompt, all you need to do is type the following command:
This seemingly innocent command will stress your Mac's CPU to the max, and will easily help you check if your computer is crashing under pressure.
3. Stop Your Mac From Sleeping
Isn't it annoying when you are installing or uploading something on your Mac, and your Mac keeps going to sleep? This amusingly named command will make sure that doesn't happen.
All you need to do is type the following command after the prompt:
Press Enter, and your Mac won't sleep, dim, or turn off. Once you're ready to disable this, all you need to do is press Control-C from the terminal.
4. Show Hidden Files
By default, your computer hides certain important files for its user, but as developers, sometimes we need access to these files. This prevents amateur users from messing with the computer's internal settings, but as developers, we understand how to handle these files.
To make all hidden files visible, enter the following command after the prompt:
$ defaults write com.apple.finder AppleShowAllFiles TRUE
And then, type this command to reset Finder after the prompt:
$ killall Finder
If you want to hide them again, enter the following command after the prompt:
$ defaults write com.apple.finder AppleShowAllFiles FALSE
And then reset Finder again:
$ killall Finder
5. Downloading Files
We've all gone through the process of downloading files, unzipping them, and using them. But that can be a hassle, especially when you need to do this multiple times. Using the terminal, you can do this with the URL of your download file.
To do this, use the
curl command as follows:
$ curl http://example.com/path-to-file
This will download the file at the given URL to the current folder.
This is a pretty complicated command, with lots of options, so take a look at the
man page for more details. To open the manual page for curl, run the following command:
$ man curl
Although it may seem daunting at first, the command line is an extremely powerful tool that, in some cases, is easier to use than the GUI. I hope you enjoyed this tutorial and found it helpful.
For more about coding for Mac and iOS, check out some of our other tutorials here on Envato Tuts+.
- Terminal40 Terminal Tips and Tricks You Never Thought You NeededJordan Merrick
- TerminalHow to Tweet From Terminal on a MacHarry Guinness
- XcodeWhat's New With Git Support in Xcode 9Doron Katz
- XcodeHow to Get Started With CocoaPodsVardhan Agrawal
- DevOpsUseful *NIX Shell Commands for Web DevelopersAnton Bagaiev