Skip to main content

Section 2.3 Basic Commands

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.

Subsection 2.3.1 What 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.

Subsection 2.3.3 Adding 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:
  1. The word touch,
  2. The file name, and
  3. 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!
Checkpoint 2.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!
When touching a file, you must put the file extension at the end of the file name. Otherwise, your computer won’t know what type of file to create!
(a)
Enter touch text at your terminal (it’s safe, don’t worry).
(b)
Everything should still work as before. Did the terminal print any output? Try running ls. Does a file named “text” appear?
(c)
Find the text file in your file browser and try to open it (by double clicking.) What happens?
Answer.
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:
  1. The open or start command,
  2. The file name, and
  3. 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.
Checkpoint 2.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.
Answer.
open test.txt or start text.txt

Using killall.

This command is only on Mac.
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.
Checkpoint 2.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!
Solution.
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.
Checkpoint 2.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”.
Answer.
cd testdir should do the trick!
(b)
Determine if there are any files inside of this directory. What is the file path to “testdir”?
Answer.
Use ls and pwd here.
Solution.
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.
Hint.
You can use ls to verify that everything worked.
Answer.
touch my_greeting.txt OR touch myGreeting.txt OR touch my-greeting.txt OR MyGreeting.txt OR MY_GREETING.txt
Solution.
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!
Answer.
open my_greeting.txt OR open <file-name>.txt
(e)
Close your text file. Mac users should use the command line whereas Windows users will have to close it manually.
Answer.
This is a .txt file: killall TextEdit.

Subsection 2.3.4 Removing Files and Directories

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.
Checkpoint 2.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.
(b)
Remove the file my_greeting.txt
Answer.
rm my_greeting.txt
(c)
Use ls to verify that the process was successful.
Warning 2.3.4.2. Removing Files is Permanent.
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).
Checkpoint 2.3.4.3. Trying cd Backwards.
Try it! Move backwards into the parent directory of testdir.
Hint.
If successful, pwd should indicate that you are in the Desktop.
Answer.
cd ..
Solution.
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.
Checkpoint 2.3.4.4. Trying rmdir.
Use rmdir to remove the test directory, testdir.
Answer.
rmdir testdir
Warning 2.3.4.5. Removing Directories is Permanent.
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.
Answer.
Use pwd to check if still on Desktop, rm <file-name> to remove files, and ls to check if the files are gone.
(b)
Navigate backwards one folder level.
Answer.
cd ..
(c)
Navigate to your Documents folder. If you don’t have one, create one and then navigate to it.
Answer.
cd Documents OR mkdir Documents, then cd Documents
(d)
Create a new folder called “My Favorites”. Remember to use proper naming techniques.
Answer.
mkdir my-favorites (could use a different case)
(e)
Navigate into your newly-created directory. Verify that you are there.
Answer.
cd my_favorites, pwd
(f)
Create three files: “Food” (a text file), “Hobbies” (a Word document), and “Smells” (an Excel spreadsheet). Verify that these were created correctly.
Answer.
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).
Answer.
Mac: open food.txt, killall TextEdit; open hobbies.docx, killall 'Microsoft Word', open smells.xslx, killall 'Microsoft Excel'
Windows: start food.txt, start hobbies.docx, start smells.xslx
(h)
Sorry, I know you just edited the documents, but now, it’s time to delete them. Remove all three of the files you edited. Verify they were removed.
Answer.
rm food.txt, rm hobbies.docx, rm smells.xslx; ls
(i)
Navigate back to your Documents folder. Verify you are indeed there.
Answer.
cd .., pwd
(j)
Remove the directory we created in this activity. Verify that is was removed.
Answer.
rmdir my-favorites, ls