Terminal/Command Line for Beginners
This tutorial is written for the complete beginner and will show you how to access the command line and the most common commands you will use as a software developer.
The command line can be a scary place when you first encounter it. Completely unknown to most computer users, the command line is a critical part of any software developer’s toolset. But if you are serious about learning how to program, you will need to learn how to use the command line. The sooner the better.
So what is it, exactly? Simply stated the command line is a text-only interface that harkens back to the computer world’s roots before the mouse and graphical interfaces made computers widely accessible. It can feel a bit like stepping into the Matrix for the first time.
The good news however is that you can become proficient in the command line relatively quickly. While there are many, many commands available, in everyday most software developers use the same few over and over again.
How to Open Terminal/Command Line
To access the command line on a Mac computer, we need to use an application called Terminal. To find it, open your Applications folder, then the Utilities folder, then doubleclick on the Terminal application. You will see a white rectangular box open up.
Let’s try out our first command. Type
ls into Terminal and hit Enter.
ls stands for “list files” and will list all the files in your current directory.
pwd to find our where you are within your computer. This command means “print working directory” and will tell you the exact working directory you are currently in. Currently we are in what is known as the “home” directory.
To change directories use the
cd command. For example, to change into the Desktop directory type
cd Desktop. Now type
pwd to confirm you are within the Desktop directory and
ls to view the files and folders on your Desktop.
Now what if you want to go back into the home directory? Use the command
cd .. to navigate back “up.” If you type
pwd and then
ls you can confirm we have returned.
So far we have learned about
cd. With these three commands we can navigate our entire computer from the command line and view available files and folders.
What else might we want to do on our computer? How about create new folders or files? You probably know how to do this with your mouse and keyboard but with the command line we can accomplish the same task much faster.
Let’s assume we want to create a new folder called “code” on our Desktop and create a file within it called test.txt. Within Terminal we first need to navigate to the Desktop. If you are already in your home directory, you can type
cd Desktop and then
pwd to confirm you are in the right spot. To make a new directory (or folder) we type the command and then the name of the new directory. So in our case, type
mkdir code to create a new directory called “code”. If you type
ls you can see the new directory is visible and we can change into by typing
cd code. (Note, you can also see this new folder visible on your desktop itself). Now to create a new empty file called text.txt type the following:
touch text.txt. Again we can type
ls to see the new file was created.
Congratulations! You have learned how to use the command line. So far we have only introduced five commands:
touch. In everyday these five commands plus
cp (copy a file),
mv (move a file), and
rm (remove a file or directory) will make up the majority of your command line usage.
If you would like to learn more, I recommend completing The Command Line Crash Course which takes a much more comprehensive approach. But for everyday use as a budding software developer, knowing how to open Terminal and using these eight commands will get you 80% of the way towards feeling comfortable on the command line.