Command Line Primer
Now that you’re set up with your command line program let’s practice some commands.
Suggestion: Get a blank sheet of paper, and for each new command we learn, write down the command and a summary of what that command does. This will help solidify the commands in your memory and also provide you with a useful reference.
In the following notes, you’ll follow along with a series of commands. Note that each command is prefixed with a >
to signify that the line is being run in command line. You don’t have to actually type in the >
.
For example, if you see something like this:
> pwd
The command you should run is just pwd
, not > pwd
.
The >
is used to represent the prompt character used in command line. The prompt character is just the character you see before you type in any commands.
Depending on your system, your actual prompt character could be a $
, %
, #
or some other similar character.
Navigating around directories
One of the things you’ll do most in CL is work with files on your computer and navigate around directories. To get started, let’s practice navigating to our computer’s home directory using the cd
(change directory) command, followed by the path ~
which is a shortcut for your home directory.
> cd ~
Next, type in the command ls
(list) which will show you everything in your home directory.
One of the directories you should see listed is your Desktop.
Move to your Desktop folder using the cd
(change directory) command.
> cd Desktop
Use the ls
(list) command again to see the contents of your Desktop:
> ls
On the Desktop let’s create a new, empty directory using the mkdir
command. We’ll call the directory practice
.
> mkdir practice
Now, move into this new directory:
> cd practice
The command pwd
(present working directory) can show you the full path of the current directory you’re in. This can be useful if you want to confirm your location before running a certain command. Example:
> pwd
/Users/Susan/Desktop/practice
Working with text files with Nano
To create new or edit existing text files directly from the command line, you can use the simple CL text editor called Nano by running the command nano
followed by a path/file you want to open. Using Nano, open the example.txt
file you just created:
> nano example.txt
Enter the text This is a test...
into the file.
Then, here are the steps to save your changes in Nano:
- Hit
ctrl
+x
to save your changes. - Nano will ask you to type in the letter
y
to confirm your save. - After typing in
y
, hit Enter.
To confirm your changes were made, you can use the cat
(concatenate) command which will output the contents of any text file directly in the console:
> cat example.txt
This is a test...
Practice: Reopen example.txt
in Nano again, make some edits to the text and save your changes. Use cat
to confirm your save worked.
Nano is a very limited text editor that can be useful for the occasional quick edits on things like configuration files. Typically when working on code in files, you‘ll use a dedicated text we’ll cover in upcoming material.
Deleting text files and using command flags
We’ve created a file, we’ve edited it, now let’s delete it by running this command:
> rm -i example.txt
remove example.text? (type 'y' for yes and hit Enter)
Note the addition of the -i
... this is a flag which is how you send extra instructions when using commands.
In this case the i
flag is short for interactive
, meaning it’ll ask you before deleting files. It’s a good habit to use the i
flag when working with rm
so you don’t accidentally delete anything you didn't mean to.
Deleting directories
Let’s clean up the practice
directory we created.
First, run the following command to move “up” one directory (i.e., out of the practice
directory):
> cd ../
Confirm you’re back on your Desktop:
> pwd
/Users/Susan/Desktop
And now remove practice
:
> rm -ir practice
Note the addition of the r
flag, which is needed for recursively removing directories and their contents.
Navigating directories
When working with directories, you can use absolute or relative paths.
- Absolute paths include the full pathname you’re working with and are not impacted by which directory you’re currently in.
- Relative paths may include abbreviated paths and are relative to the directory you’re currently in.
For example, imagine we had three nested directories on the Desktop, a, b, and c.
If we wanted to move into the c directory from any directory we’re currently in, we could use the absolute path:
> cd ~/Desktop/a/b/c
But let’s say we’re already in the a directory (~/Desktop/a
). We could navigate to the c directory using a relative path:
> cd b/c
(Note how this path does not start with a /
, indicating the path is relative to my current location.)
Similarly, imagine we’re now in the c directory and we want to get back to the a directory. Using an absolute path we could do this:
> cd ~/Desktop/a
Or, we could use the “go up one directory” trick shown above to first go up to the b directory, then the a directory:
> cd ../../
You can use both absolute and relative paths and you’ll find they come in handy in different circumstances.
For example, absolute paths are useful when you’re trying to get to a directory that is far removed from your current directory and you know the full path of where you’re trying to go.
Relative paths are useful when you’re “exploring directories” - jumping around different directories from where you currently are, in search of something.
Misc tips
- Use explainshell.com or an AI program to break down any commands that are unfamiliar to you. Always make sure you understand what a command does before you run it.
- Use the up/down arrows to cycle through previously used commands.
- Using the tab key when you’re typing a path will attempt to autocomplete that path.
- When commands are run they typically execute and release you back to your prompt. Some commands, however, might not immediately exit, preventing you from running additional commands. When this happens, use the keyboard shortcut
ctrl
+z
to exit the command back to your prompt.
In conclusion...
There is a lot more you can do in command line besides working with files and directories. The above exercise was just to get you familiar with working with commands and cover some of the essential actions you’ll need to know for this course.
Quick reference
-
pwd
Find out which directory you’re in (present working directory) -
cd ~/
Go to your home directory -
ls
List the contents of the current directory -
cd /path/to/foobar
Change to a directory -
cd ../
Go back one directory -
cd ../../
Go back two directories -
mkdir foobar
Make a directory in current directory -
rm -i foobar.txt
Remove a file (with confirmation) -
rm -ir foobar/
Remove a directory (recursively, with confirmation) -
touch foobar.txt
Create a new text file -
nano foobar.txt
Edit a text file via Nano -
code foobar.txt
Edit a text file via VSCode