📓 1.0.0.19 Practice: Command Line
Warm Up​
Ask yourself the following questions before moving on:
- What is the difference between a graphical user interface and a command line interface?
- What makes a home directory a home directory instead of just a directory or subdirectory?
Code​
Command Line Practice​
Now that you have some of the basics of the command line down, let's practice using those new skills to navigate in the terminal. Complete the following quick exercises in your computer's Terminal shell:
Open a terminal shell. This will be your home directory. You should have a
$
at the end of the prompt. In our lessons we begin the code meant for the command line with a$
. You will only type what follows the$
, not the$
itself. You are now ready to use the command line!Run the command
$ pwd
. The computer will print the pathname to the directory that you are currently located in. For example, you might see/Users/Guest
.Now let's figure out what directories reside in our home directory by running the command
$ ls
to list all of the directories and files in our current location.Move into the
Desktop
directory by running the command$ cd Desktop
. Now run$ ls
again to see all the files and folders located on your desktop.Now that we have entered a few commands, use the up and down arrow keys to navigate to previous commands you've entered.
Let's make a new folder on the desktop. Run
$ pwd
to make sure you are in theDesktop
directory. If not, navigate to theDesktop
directory by running$ cd Desktop
.Then run
$ mkdir test
. This will make a new directory on your desktop calledtest
. Take a look and make sure it is there! You can make multiple directories at the same time by putting a space between the directories names. For example,$ mkdir test test2
. We've already run$ mkdir test
so go ahead and just run$ mkdir test2
.Move into the
test
directory through the command line. Do you remember how? It's the$ cd
command plus the name of the directory we'd like to move into ($ cd test
).Run
$ touch file1 file2
to create two new files within thetest
directory calledfile1
andfile 2
. The$ touch
command creates a new, empty file. If you're using Windows PowerShell, you can use the command> New-Item
or the alias> ni
instead of$ touch
. (By default Windows will only let you create one new file at a time.)Type
ls
to make sure the files are there. Just like making directories, you can make multiple files at the same time.Run
$ touch test2/file1 test2/file2
to make files in thetest2
directory. Oops! That didn't work! Why? Well the terminal can only access the directory above the current working directory and any files/directories in the current working directory. Sincetest2
is a directory in theDesktop
directory, we cannot access it from thetest
directory. To do that we need to run the command$ touch ../test2/file1 ../test2/file2
. This path tells the terminal to go up to theDesktop
- using the..
command, then back down to thetest2
folder. Notice we just made two files in a directory that we were not currently in! This works because we supplied the directories we wanted those files to be created in.Let's navigate back to the
Desktop
.Desktop
is the parent of our current working directory, so we can use the command$ cd ..
. If we wanted to go back to our home directory we can always use the command$ cd ~
.Let's remove the
test
directory. Run$ rm test
.You should get the error
rm: test: is a directory
. Whenever we want to delete a directory, we need to add the-r
flag. In this case, we'll run the command$ rm -r test
. This will use the recursive option to instruct the computer to delete everything in thetest
folder, including the folder itself. In some cases, we need to use$ rm -rf test
. This adds the force flag to make sure the directory is removed. However, you must be very, very careful. This is potentially one of the most dangerous commands you can run. If you accidentally delete a directory you want to keep (or a directory with files the system needs, there is no way to retrieve those files.Now let's exit the terminal by typing
$ exit
.
Even More Practice​
Next up try to practice the same workflow, but with fewer step-by-step instructions.
- Specifically, make a folder on your Desktop containing 3 subfolders with names of your choice.
- Each subfolder should have 3 individual files within it (again, your choice of name).
Get comfortable doing this step by step, but then try to do it in as few commands as possible.
Then go through and practice deleting files and folders. Make sure you're comfortable navigating around and creating and deleting both files and directories before moving on.