Introduction
Feeling scared of the command line? You’re not alone. We have this image of developers staring intently at a black screen with white or green text flashing across as they wildly enter incomprehensible commands to hack into the corporate mainframe (no doubt while guzzling soda and wiping neon orange Cheetos dust off their keyboard).
That blank screen or window with a prompt and blinking cursor is the command line interface (CLI), where you’re able to enter commands that your computer will run for you. While there’s no need for you to reenact the scene above, working with the command line is a critical skill for you to learn as a developer. The command line is like our base of operations, from which we can launch other programs and interact with them. It has a syntax of its own to learn, but since you’ll be entering the same commands dozens of times, you’ll quickly pick up the commands you need most.
In this introductory lesson on the command line, you’ll learn how to navigate your computer and manipulate files and directories (also known as folders) directly from the comfort of the command line. You’ll soon see that this isn’t as difficult as you may think. The commands you will learn in this lesson are very straightforward, so don’t let the prospect of using the command line for the first time intimidate you.
Lesson overview
This section contains a general overview of topics that you will learn in this lesson.
- Describe what the command line is.
- Open the command line on your computer.
- Use the command line to navigate directories and display directory contents.
- Use the command line to create a new directory and a new file.
- Use the command line to rename or destroy a directory and a file.
- Use the command line to open a file or folder in a program.
Test drive your terminal
Open a terminal on your computer.
-
Linux: Open the programs menu and search for “Terminal”. You can also open the terminal by pressing Ctrl + Alt + T on your keyboard.
-
macOS: Open your Applications > Utilities folder and find “Terminal”. You can also use Spotlight search to open Terminal. Press Cmd + Space to open Spotlight, and search for “Terminal”. Press Enter to open it.
The window that opens will be mostly blank, with the exception of some text that will vary based on your operating system. On Linux and older Macs, the line will end with $
and on newer Macs, the line will end with %
. This symbol - called the prompt - indicates that the terminal is waiting for you to enter a command. Let’s try that now. Type whoami
and press Enter.
It returns your username. Cool!
Often, guides and instructions for using the terminal will indicate commands by putting the symbol first, like $ whoami
. This tells you to type the command in your terminal, but don’t enter the $
. And remember, if you are using a newer Mac, the %
is the same as $
.
Why learn this now?
You will be making heavy use of the command line throughout this curriculum, and the upcoming installations project will need you to install many different software programs using the command line. Additionally, you will primarily be using Git within the command line (more on this later). As part of the bigger picture, you may well be using the command line on a daily basis in your career as a software developer, making it an indispensable skill in your toolset.
A note on typing passwords in the terminal
When using a command in the terminal that requires you to enter your password for authentication (such as sudo), you will notice that the characters aren’t visible as you type them. While it might seem like the terminal isn’t responding, don’t worry!
This is a security feature to protect confidential information, like how password fields on websites use asterisks or dots. By not displaying the characters you type, the terminal keeps your password secure.
You can still enter your password as normal and press Enter to submit it.
Use the command line like a pro
There’s something important that you need to know about programmers. Programmers are lazy. Like, really lazy. When forced to do something over and over again, the odds are good that they’ll figure out a way to automate it instead. The good news is that you get to take advantage of the many shortcuts they’ve created along the way. It’s time to learn how to use the command line like a pro (which is to say, in a really lazy way).
First, you might have already noticed that copying and pasting inside the command line doesn’t work the way that you’d expect. When you’re inside the command line, use Ctrl + Shift + C (Mac: Cmd + C) to copy and Ctrl + Shift + V (Mac: Cmd + V) to paste. For example, to copy and paste commands from your browser into the command line, highlight the command text and use Ctrl + C as usual, then paste it in your terminal using Ctrl + Shift + V. Test it out!
Second, you need to learn about tab completion. Seriously, this tip will save you so much time and frustration. Let’s say that you’re in the command line and need to move into a distant folder, something like ~/Documents/Odin-Project/foundations/javascript/calculator/
. That’s a long command to type out, and everything needs to be exactly right in order for it to work. Nope, we’re way too lazy for that! Basically, by hitting Tab, the command line will automatically complete commands that you’ve started typing once there’s only one option. For example, it’s pretty common to have a Documents
folder and a Downloads
folder in the home directory. If you’ve typed cd D
and then press Tab, the command line will let you know that it’s not sure which one you want by showing you the different options that match what you’ve typed so far:
$ cd D
Documents/ Downloads/
$ cd D
But once you’ve typed in a bit more, it will complete the name for you, making it possible to write out the full file path above by typing as little as cd Doc[tab]O[tab]f[tab]j[tab]cal[tab]
(depending on what other folders exist on your computer). Test it out, and get comfortable with how this works. You’re gonna love it.
Third, there’s a really handy shortcut for opening everything within a project directory: .
Once you’ve installed a text editor, you can use this shortcut to open up an entire project and all its files in one go. This shortcut is also commonly used with Git (later on it’s covered in detail) with commands like git add .
to add all the files in a directory to Git’s staging area. For example, if you have VS Code installed, you can cd
into the project directory and then type code .
(with the period). It will launch VS Code and open up the project folder in the sidebar. See the next section of this lesson for a more detailed example.
Opening files in VSCode from the command line
-
Linux: You can open VSCode from the command line by typing
code
, and you can open folders or files by adding the name of the location after it:code my_awesome_project/
. -
macOS: Some setup is required. After installing VSCode, launch it any way you’re comfortable with. Once it’s running, open the Command Palette with Cmd + Shift + P. In the little dialog that appears, type
shell command
. One of the choices that appears will beShell Command: Install 'code' command in PATH
. Select that option and restart the terminal if you have it open. -
WSL2: Opening up VSCode from the command line in WSL2 is just as easy as it is in Linux. Just enter
code
, which will open VSCode in WSL2.
Assignment
Some of the following resources assume you have a Desktop folder located in your system. If, however, you do not have one located anywhere in your system, then please do the following: Open your terminal and make sure you’re located in the home directory by typing cd ~
into your command line. Then, type mkdir Desktop
and press enter
. To ensure your command worked, type ls
and check if your Desktop folder is shown amongst your other directories.
Note for WSL2 users: Use the wget
command along with the link given in the Download files
section to get the zip file in your WSL2 installation (wget https://swcarpentry.github.io/shell-novice/data/shell-lesson-data.zip
). You will also have to install unzip by using the command sudo apt install unzip
and then unzip shell-lesson-data.zip
to unzip the file. Keep in mind that throughout the course linked in the first step below, your terminal output may look slightly different from what is shown in the lessons. Anytime the course asks you to go to the Desktop, you will instead be going to the home directory which can be done by using the cd command (cd ~
).
Many of these resources assume you’re using a Mac or Linux environment. If you did our previous installation lesson, you should already have Linux installed in dual-boot or a virtual machine. Or, you might be using macOS. If you don’t have macOS, or any official Ubuntu flavor installed, please return to the Installations lesson.
-
Visit The Unix Shell course designed by the Software Carpentry Foundation. There, you will find a full complement of lessons on using the CLI, but for now, just focus on completing the following lessons:
- Download files - only follow the instructions in this section. You don’t need to install any software and can move onto the next bullet point in this list.
- Introducing the Shell
- Navigating Files and Directories
- Working With Files and Directories
-
With your newly discovered CLI superpowers, practice creating a folder and a few files using the
mkdir
,touch
, andcd
commands introduced in the previous step. As an example, a basic website might have a mainindex.html
file, a CSS stylesheet file calledstyle.css
, and a folder forimages
. Think about how you could create these files with the commands and put it into practice! -
Let’s practice creating files and directories and deleting them! You’ll need to enter the commands for the steps below in your terminal. If you can’t recall how to open a terminal, scroll up for a reminder.
- Create a new directory in your home directory with the name
test
. - Navigate to the
test
directory. - Create a new file called
test.txt
. Hint: use thetouch
command. - Open your newly created file in VSCode, make some changes, save the file, and close it.
- Navigate back out of the
test
directory. - Delete the
test
directory.
That’s it—you’re done with practice! If you commit to doing most tasks from the command line from here on out, these commands will become second nature to you. Moving and copying files is much more efficiently done through the command line, even if it feels like more of a hassle at this point.
- Create a new directory in your home directory with the name
Knowledge check
The following questions are an opportunity to reflect on key topics in this lesson. If you can’t answer a question, click on it to review the material, but keep in mind you are not expected to memorize or master this knowledge.
- What is the command line?
- How do you open the command line on your computer?
- How can you navigate to a particular directory?
- Where will
cd
on its own navigate you to? - Where will
cd ..
navigate you to? - How do you display the name of the directory you are currently in?
- How do you display the contents of the directory you are currently in?
- How do you create a new directory?
- How do you create a new file?
- How do you destroy a directory or file?
- How do you rename a directory or file?
Additional resources
This section contains helpful links to related content. It isn’t required, so consider it supplemental.
- The online book, Learn Enough Command Line to Be Dangerous, is a great resource for mastering the command line. Chapters 1 and 2 are free and provide a good introduction to command line tools. The rest of the book is not free and goes into more depth than you really need at this point, but feel free to buy and read the rest of the book if you like.
- Command Line Flashcards by flashcards.github.io.