Now that we have understand what a terminal does and how to find it, let’s learn some commands. We’ll start with the basics.
Subsection2.3.1What is a Command?
Commands are pieces of text that are entered at the terminal and that can be understood by a computer. In general, commands are made up of three pieces:
Service Name
Many commands come from external sources. To access these commands, we need to specify the source we are pulling the command from. Example service names are python, pretext, brew, and git.
Command Name
Once we have selected the command source, we need the command name itself. This is often a verb such as push, pull, build, or install.
Extra Arguments
Some commands come with extra options or required arguments. For example, building on the previous examples, you may need to supply locations to push to or pull from or what to build or install. A command could have many arguments.
Not all commands have all three of these parts but it’s important to know the basic command structure. For example, some commands (such as git status, which we will explore later) do not require any arguments. Other commands don’t need a service name since they are built into your terminal by default. To start simple, we will explore these latter types of commands first and add in commands from the git service later on in the book.
Subsection2.3.2Navigating Your Computer With the Command Line
When your first open your terminal, you aren’t given very much information. On Mac and Windows, you get your username and maybe your computer name/id but not much else. This isn’t very helpful. How can we do anything with our computer when we don’t even know where we are on the computer?
Using pwd.
Our first command will show us where we are: pwd, which stands for present working directory.
Note2.3.2.1.Folders Are Not Directories.
Until now, I have been using “folder” when I probably should be using “directory”. From Wikipedia 27 we can read that
If one is referring to a container of documents, the term folder is more appropriate. The term directory refers to the way a structured list of document files and folders are stored on the computer.
That’s a little confusing! The difference here is mostly technical. Essentially, a directory is a “file system object” whereas a folder is just a user-friendly way to represent directories. For our purposes (and for most purposes), the two terms can be used interchangeably. I use “directory” when speaking about functions on the command line and “folder” when speaking in informal settings. If you are interested, you can read more about the history of the terms at this Quora question 28 .
Checkpoint2.3.2.2.Trying pwd.
Enter pwd at your terminal. What does it tell you?
Helpful Hint.
Anytime I talk about “entering something at your terminal”, type in the desired command and press Enter.
pwd prints one thing: the file path to the folder that is “active” at your terminal. When you first open your terminal, Mac users might see something like /Users/<your-username> and Windows (Git Bash) users /c/Users/<your-username>. If you ever get lost in your terminal, type in pwd for some reassurance.
Using cd.
Knowing where you are is great, but as of right now, we are stuck in one place. What use is pwd if we can’t move anywhere? By default, your terminal places you in the “highest” level possible for the active user; from there we can only move “in”. Essentially, you are moving from one directory into another. The cd, or change directory command can do this for us. cd requires another piece of information: the directory you wish to navigate to, which must be typed using file-path syntax.
File-path syntax refers to how we describe where a file lies on your computer. The basic structure is as follows:
Try running pwd after cd. Do you notice anything different here?
If everything went correctly, you should now be “in” your Desktop directory (folder, if you insist). You may have even noticed that your terminal changed. On any operating system, you should see that the word “Desktop” appeared before your cursor. This is a nice check to make sure that cd worked and that you are where you want to be.
Using ls.
Ok, we’re making progress! Now that we are in the Desktop, what can we do? Let’s use another terminal command to see what files and directories are currently on your Desktop. ls, or list contents will do the trick. Note that the output will differ for each user as we all have different files in different places.
Checkpoint2.3.2.4.Trying ls.
Enter ls into your terminal. What is the output? Is it what you expected?
Perhaps unsurprisingly, your terminal should have just listed every single element on your Desktop. If you every forget what files are in your “active” directory (and you don’t want to open your file browswer and navigate to the folder), use ls as a refresher.
You may have noticed that some files/directories show up in your terminal that are not visible on your Desktop. This is because most operating systems by default hide certain files that should not be deleted. They still exist, they are just invisible to prevent accidental deletion.
Subsection2.3.3Adding Files and Directories
Now that we are familiar with navigating our computer with the command line, let’s learn a little more. Suppose you are in your Desktop (which we are) and you want to add a text file. Since you are already in the terminal, you don’t want to open a text editor, create a new file, and save it to your desktop. Or, suppose you have a file on the Desktop that you no longer need and you don’t to open your file browser, navigate to the Desktop, and delete the file. These examples may seem silly, but now suppose you are six folders deep. It would take much more time to navigate to those folders in a file browser than it would in the terminal.
Using touch.
Creating a new file is simple. The command requires three parts:
The word touch,
The file name, and
The file extension.
All touch does is create an empty file with the name and file extension specified by you on the command line. Why touch you ask? Well, everytime you edit (“write to”) the file, you are “touching” it electronically.
Note: Don’t forget to follow Note 1.1.1.1 when creating your files!
Checkpoint2.3.3.1.Trying touch.
(a)
Enter touch test.txt into your terminal. What happened? Did it work as you expected?
(b)
Can you use a command we learned earlier to check if your touch worked?
Oh dear, nothing happened! When you press enter after a touch command, the terminal gives you no output at all. How can we verify that a test.txt file got created? Let’s try ls. Recall from Using ls that we can use ls to give us a list of all the items in our present working directory. (Remember that term? See Using pwd.) Since we are currently on the Desktop and we want to see what files are on the Desktop, we can use ls to see if our file was created. Try it now!
Mac users likely see the TextEdit app open. This is the default app for opening files like this. Windows users will be prompted to determine which application should be used to open the file.
The purpose of this activity is to demonstrate the importance of file extensions. When none are provided, your computer either has to guess what the file’s contents are or simply cannot interpret the file and asks you for help. We would like to avoid computer confusion as much as possible. Common file extensions and their file types are listed in Section 1.2.
Just remember, anytime you wish to create a file, you need touch, a name, and an extension.
Using open.
Once a file is created, opening it is simple: just use the open (Mac) or start (Windows) command. To open or start a file, we use a syntax similar to touch:
The open or start command,
The file name, and
The file extension.
Your terminal will choose the correct software to open the file based on the file extension, once again stressing the importance of Activity 2.1. Changing default programs is not a part of this book, but a Google search should do the trick if you are interested.
Checkpoint2.3.3.2.Trying open.
In Checkpoint 2.3.3.1, we created a new file called test.txt. Open this file with the terminal. If you are on a Mac, don’t close it yet! Windows users may close the file manually.
Closing a file is less trivial. It can be tricky to close a single file, but closing an entire application is not too bad. You need two pieces:
The command killall
The name of the application you wish to close.
A few remarks: The kill all command will quit the entire application, not just a single file. Be sure you want to quit (i.e., you have saved any changes) before using killall. Also, application names are case sensitive; that is, killall TextEdit will work fine, but killall textedit will return an error. Finally, be aware that if an application name has a space in it (e.g., Microsoft Word), you will need to enclose that name in quotes: killall 'Microsoft Word'. You can use single or double quotes as long as you use the same style for both quotes.
Checkpoint2.3.3.3.Trying killall.
Use the terminal to close the test.txt file that we just opened in Checkpoint 2.3.3.2. Remember that the command is case sensitive!
Your computer should have opened up TextEdit (the default text editor for Mac) when attempting to open the previous file. To close this application, you should enter killall TextEdit at the command line.
Using mkdir.
We’ve seen how to use the terminal to create new files; however, sometimes files aren’t enough. With the command line, we can also create new directories (represented by folders) for us with mkdir. The make directory command does exactly that: makes a new directory.
Checkpoint2.3.3.4.Trying mkdir.
(a)
Enter mkdir testdir on your command line. What happens? Is this what you expected after learning about touch?
(b)
Use ls to verify that the command worked.
You should see, with ls, that a new directory was created. Windows users may see that their new “testdir” is a different color (and has a “/”). They both indicate that “testdir” is a directory. Mac users may not see these which thus stresses the importance of Activity 2.1. When ls is used, the file extension is printed (when there is one). Directories do not have file extensions so when interpreting ls output, it can usually be safely assumed that any item without an extension is a directory. Note that for any OS, Note 1.1.1.1 applies for mkdir as well.
It’s time to put your skills to the test! Using your terminal and the knowledge gained from this section so far, complete the following tasks. This activity assumes you have been following along with the checkpoints.
(a)
Navigate into the newly-created directory, “testdir”.
Since we just created this directory, ls should not show any files; no output should be provided. pwd can be used to find the file path which should look like
/Users/<your-username>/Desktop/testdir
or
/c/Users/<your-username>/Desktop/testdir
(c)
Create a new text file with a name of “My Greeting”. Be sure to use proper naming techniques and correct terminal syntax.
Whenever we create a new file (of any type), we use touch. Recall that touch requires three pieces: the command, the file name, and the file extension. The command is touch, the file name is my_greeting (recommended, although there are other possible names), and the file extension is .txt. Put these together, and we get touch my_greeting.txt.
(d)
Open your text file and type a greeting into the first line. Save the file. Remember to only use the command line to open the file!
Over the course of this section, we have added useless files and directories. We do not need our test files or folders anymore, so let’s learn how to delete them (with the terminal, of course!)
Using rm.
Removing a file with rm has the exact same syntax as using touch (Using touch) except that instead of touch, we use rm. To summarize, we would need
The command rm,
The file name, and
The file extension.
Checkpoint2.3.4.1.Trying rm.
(a)
Use ls and pwd to verify that you are still in the testdir directory and that the file my_greeting.txt exists.
Removing files is easy...Too easy. Notice that the terminal provided no output or verification that the process was happening. Also notice that there was no dialog box asking if we were really sure that we wanted to remove the file.
Removing files with the terminal is permanent. There is no recycle bin for these files. There is no “undo” or “restore”. The terminal deletes and forgets. Use extreme caution when using rm; only remove what you are absolutely sure you need to remove. You will not get a second chance and your computer will not ask you if you for verification.
cd Backwards.
Alright! We cleared the contents of our testdir directory. Remember, the ultimate goal of this part is to delete all of the new files and folders we created in this section. But now we’re stuck. The terminal is still in the testdir directory (which now contains no files). In order to delete our other files we have to change directories backwards. Luckily, we can still use cd, but instead of supplying a folder to move into, we give .. instead. Together, cd .. tells the terminal to move to the directory above the one it is currently in (the parent directory).
Checkpoint2.3.4.3.Trying cd Backwards.
Try it! Move backwards into the parent directory of testdir.
As mentioned, cd .. will move you backwards. If you run pwd, you should see that you have returned to the Desktop; ls should give you all of the files we were working with before.
Using rmdir.
You have the tools you need to remove files, but what about directories? If you’d like, try removing our testdir directory with rm. What happens?
You should get an error stating that rm cannot remove directories. Fortunately, there is an aptly named command called rmdir which helps us remove directories. Using rmdir is exactly like using rm except that you can only type names of directories.
Similar to Warning 2.3.4.2, removing directories is too easy. Notice again that the terminal provides no output or verification that the process was happening. Also notice that there was again no dialog box asking if we were really sure that we wanted to remove the directory.
Removing directories with the terminal is permanent. There is no recycle bin for these files. There is no “undo” or “restore”. The terminal deletes and forgets. Use extreme caution when using rmdir; only remove what you are absolutely sure you need to remove. You will not get a second chance and your computer will not ask you for verification.
Note: you can add in an extra layer of security with the sudo command, which requires your computer’s password to be entered.
You now know the basics of using using the command line. In this activity, you will practice what you have learned. Be sure to use the command line for each task.
(a)
Verify that you are still on your Desktop. Remove the rest of the test files we created throughout the chapter. Check to make sure all the files have been removed.
touch food.txt, touch hobbies.docx, touch smells.xslx; ls
(g)
Open each of the three files, type your top three favorites of each category, and close the files again. (Remember, Windows users will have to close the files manually).