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).
Other terms that refer to the Command Line Interface (CLI) are: Terminal, Shell, Command Prompt, Bash.
That black screen or window 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 to the command line, you’ll learn how to navigate around your computer and how to 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 be intimidated by the prospect of using the command line for the first time.
Test Drive Your Terminal
Open a terminal on your computer.
- Windows: Press Windows+s to open up the “Start” box. Type “GitBash” and then click “OK” to open the Git Bash Command Prompt.
- Linux: open the programs menu and search for "Terminal". You can also open the terminal by pressing
CTRL + ALT + Ton your keyboard.
- MacOS: Open your Applications > Utilities folder and find "Terminal".
Before we do anything, take a look at the following text:
This is a terminal command because it begins with a
$ is saying "Hey! Enter what follows in your terminal." This means that we must exclude the
$ when entering any command. In the example above, we would only enter
whoami in our terminal. This is a common indicator so make sure that you aren’t entering
$ before a command. Now that you are aware of what
$ does, take your terminal for a test run! Make sure your terminal is open, type the command mentioned above, and press enter on your keyboard.
It returns your username. Cool!
Why learn this now?
You will be making heavy use of the command line throughout this curriculum, and the upcoming installations project will require 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.
By the end of this lesson, you should be able to do the following:
- 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.
- Before diving into the command line lesson, you’ll want to know how to create a file. You can do so with the
- Open your terminal and enter
lis a lowercase
lswill show you the files and folders in the current directory (or will show nothing if the current directory is empty).
- Create a file called
test.txtby entering this in your terminal:
lsonce again. You should see
test.txtlisted in the output. You can also create more than one file at once using the
touch index.html script.js style.cssand press the enter.
touch index.html script.js style.css
- Then enter
lsonce more. You should see the files in the output. Here is a small way that the terminal reveals its power. How long would it have take to create all three of those files with your mouse? Thanks, terminal!
Now it’s time to take a break for 10 to 15 minutes, and then start reading the 1st chapter of
Conquering the Command Line.
Serve yourself a hot cup of tea or coffee and dedicate the next few hours to discovering and using the Command Line. Make sure to test the commands mentioned in the book on your local terminal.
Read & Execute: Chapter 1 of Conquering the Command Line.
There’s something important that you need to know about programmers. Programmers are lazy. Like, really lazy. If they are forced to do something over and over again, odds are good that they’ll figure out a way to automate it instead. The good news is that you get to benefit from 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).
Duration: 12 minutes
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, you’ll need to use
Cmd+C) to copy and
Cmd+V) to paste. For example, to copy and paste commands from your browser into the command line, you’ll highlight the command text and use
Ctrl+C as usual and 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 that you need to move into a folder that’s far away, something like
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 little 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]W[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 of its files in one go. For example, if you have VS Code installed, you can
cd into the project directory and then type
code . (with the period) to open up all of the project files. This shortcut is also commonly used with Git (which is covered in detail later on) with commands like
git add . to add all of the files inside of a directory into Git’s staging area.
In this exercise, you will practice creating files and directories and deleting them. You’ll need to enter the commands for this exercise 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
- Navigate to the
- Create a new file called
test.txt. Hint: use the
- Navigate back out of the
- Delete the
That’s it–you’re done with command line basics! If you commit to doing most things 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.
Another great resource is this video made by one of our students Kiuri Waddington Negrao that shows how to create a Bash script that initializes a project directory, creates some basic boilerplate for web development and initializes it as a git repository. All this running on a Windows machine:
Duration: 11 minutes
- Beginner’s Guide to the Bash Terminal
- Duration: 70 minutes
- Read this How to Create Bash Aliases guide and learn how you can use the power of aliases in the terminal.
Bash aliases are essentially shortcuts that can save you from having to remember long commands and eliminate a great deal of typing when you are working on the command line.
The online book Learn Enough Command Line to Be Dangerous is a great resource for mastering the command line. Chapter 1 is free and provides 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 purchase and read the rest of the book if you like.
ExplainShell.com is a great resource for if you want to deconstruct a particularly strange shell command or learn how Bash works through guess-and-check.
Unix/Linux Command Cheat Sheet contains a list of important commands that you can refer to regularly as you become familiar with using Linux. You can print it out so you can have a physical copy with you when you’re not at your computer.
Command Line Flashcards by flashcards.github.io.
Video Series from LearnLinxTv contains 24 videos explaining the basics of the command line. Videos are brief enough for beginners but, at the same time, are detailed enough to get started and light your inner curiosity.
This section contains questions for you to check your understanding of this lesson. If you’re having trouble answering the questions below on your own, clicking the small arrow to the left of the question will reveal the answers.
What is the command line?
- The command line is a way to interact with the computer using specific words called “commands”.
How do you open the command line on your computer?
- On Windows: Press Windows+s to open up the “Start” box. Type “GitBash” and then click “OK” to open the Git Bash Command Prompt.
- On Linux: Open the programs menu and search for “Terminal”. You can also open the terminal by pressing
- On Mac: Open your applications folder and find “Terminal”.
How can you navigate to a particular directory?
- You can use the
cdcommand to change directories.
cd on its own navigate you to?
- On Linux and Mac, it will navigate you home.
cd .. navigate you to?
- It will navigate you “up” one folder, that is, into the parent of the current directory.
How do you display the name of the directory you are currently in?
- On Linux and Mac, use the
pwd(print working directory) command.
How do you display the contents of the directory you are currently in?
- On Linux and Mac, use the
ls -lto display the files in a list.
How do you create a new directory?
- You can do this using the
How do you create a new file?
- On Linux and Mac, use the
How do you destroy a directory or file?
- On Linux and Mac, use the
rmcommand. To destroy folders, use
How do you rename a directory or file?
mv folder/old-file.txt folder/new-file.txt.
LOOKING FOR HELP?
When looking for help, try doing so in the following order:
- Did you try everything you could?
- Did you read the documentation?
- Did you Google for it?
- Did you post your question on Slack/Forum?
- Did you ask your fellow students for help?
- Did you ask your Mentors for help?
- Did you leave a comment on the comments section of this page?
- Did you ask your Instructor for help?