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
Creating text files
In our new practice directory, let’s create a new file called example.txt with the touch command:
> touch example.txt
Use the ls (list) command again to see that the file was created:
> ls
example.txt
Now let’s edit this file...
Editing text files with Nano
To edit 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+xto save your changes. - Nano will ask you to type in the letter
yto 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: Try opening example.txt in Nano again, making some edit to the text and saving again. 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 a later video.
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 on 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 I had three nested directories on my Desktop, a, b, and c.
If I wanted to move into the c directory from any directory I’m currently in, I could use the absolute path:
> cd ~/Desktop/a/b/c
But let’s say I’m already in the a directory (~/Desktop/a). I 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 I’m now in the c directory and I want to get back to the a directory. Using an absolute path I could do this:
> cd ~/Desktop/a
Or, I 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 to look up the details for any command.
- 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+zto 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
-
pwdFind out which directory you’re in (present working directory) -
cd ~/Go to your home directory -
lsList the contents of the current directory -
cd /path/to/foobarChange to a directory -
cd ../Go back one directory -
cd ../../Go back two directories -
mkdir foobarMake a directory in current directory -
rm -i foobar.txtRemove a file (with confirmation) -
rm -ir foobar/Remove a directory (recursively, with confirmation) -
touch foobar.txtCreate a new text file -
nano foobar.txtEdit a text file via Nano -
code foobar.txtEdit a text file via VSCode