Command Line Basics Copy

Introduction

Think of an image: Developers staring at a black screen with white or green text flashing, writing code and trying to hack into the corporate frame. Does this image sounds familiar?

That black screen is the command line interface (CLI).

Other terms that refer to the Command Line Interface (CLI) are: Terminal, Shell, Command Prompt, Bash.

In this CLI is where you enter the commands that the computer will run. Working with the command line is essential skill to learn as a developer. It is the base of our operations meaning that it is from where we can launch other programs and interact with them. The command line has its own syntax, but since you are going to enter the same commands over and over again, then it is a matter of time to learn the most important ones and the ones you will need most.

In this lesson you will learn some basic operations we can do from the command line such as navigate around the computer and manipulate files and directories (also known as folders). The commands that you will learn in this lesson are very simple and straightforward so there is nothing to worry about.

Write Your First Command in the 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.

Launch GitBash

  • 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".

Before we do anything, take a look at the following text:

$ whoami

whoami

Every terminal command starts with a $. This symbol tells our computer to execute what comes after the $. This means that we must excluded it when we enter a command. So in the example above we will only type whoami in our terminal. So open your terminal, type whoami and press enter on your keyboard.

It returns your username. Cool you have just wrote your first command.

Why should you learn the terminal now?

Throughout this curriculum we will use the command line very often. We will going to install various software using the command line and you will use Git within the command line. In addition in your career as a software developer you will use the command line on a daily basis, and thus learning that skill early on it will be an asset.

Learning Objectives

  • Understand what the command line is.
  • Open the command line on your computer and use it to:
    • navigate directories and display directory contents.
    • create a new directory and a new file.
    • rename or destroy a directory and a file.

Practice

  1. First let’s see how to create a file using the command line. You can do so with the touch command.
  • Open your terminal and enter ls (the l is a lowercase L). The ls command will show you the files and folders in the current directory (or will show nothing if the current directory is empty).
ls
  • Create a file called test.txt by entering this in your terminal: touch test.txt.
touch test.txt
  • Now enter ls once again. You should see test.txt listed in the output. You can also create more than one file at once using the touch command.

  • Enter touch index.html script.js style.css and press the enter.

touch index.html script.js style.css
  • Then enter ls once more. You should see the files in the output.

This is a small indication of how powerful the terminal is. It would have taken much more time if we had created those files with the mouse. Thanks, terminal!

Conquering the Command Line

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.

Advanced Command Line Topics

Programmers in general are lazy. If they are using the same command over and over again they will find a way to automate it. For this reason there are a lot of shortcuts out there, that they have been created from developers, that you can use and benefit from.

Watch the next video to learn about common terminal commands and shortcuts and then move on to the next section and read about the capabilities of the command line in more detail.

Duration: 12 minutes

One problem that you might already have encountered is that copying and pasting inside the terminal doesn’t work as usual. In order to copy and paste something you need to use Ctrl+Shift+C(Mac: Cmd+C) to copy and Ctrl+Shift+V(Mac: Cmd+V) to paste. You can test it by highlighting a command text from the browser, use Ctrl+C to copy it and then use Ctrl+Shift+V to paste it in the terminal.

Another useful tip is the tab completion. This tip will save you a lot of time since it helps you to navigate quicker to a folder. For example let’s say you need to move to a folder that is located in ~/Documents/FrontEndDevelopment/Web-Development-101/javascript/calculator/. As you see this is a long command to type out and thus by hitting Tab, the command line will automatically complete commands as you are typing them, once there’s only one option.

Let’s see an example: If you type cd D then you probably have two folders the Documents folder and the Downloads folder. So the command line will show you these two options.

$ cd D
Documents/ Downloads/
$ cd D

However if you continue typing a little more you will be able to write the full path with just typing cd Doc[tab]O[tab]W[tab]j[tab]cal[tab]. Just test it out and see how it works.

There is also another handy shortcut the .. This command will help you open every file within a project. For example if you have VSCode installed you can type cd into the project directory and then type code . (with the period) to open up all of the project files. In addition this shortcut is also used with Git. For example we use the git add . command to add all the files inside of a directory into Git’s staging area.

Class Exercise

Now it’s time for an exercise. You will create directories and folders and then delete them. You will create those files using the terminal.

  1. Create a new directory in your home directory with the name my-directory.
  2. Navigate to the my-directory directory.
  3. Create a new file called my-file.txt.
  4. Navigate back out of the my-directory directory.
  5. Delete the my-directory directory.

If you have successfully done those steps you have understood the basics of the command line. Keep in mind that is more efficient to move and copy files through the command line. As you continue using the command line more often then you will get used to it and you will feel more comfortable.

Additional Resources

In this section you can find a lot of helpful links to other content. This is a supplemental material for you if you want to dive deeper into some concepts.

A 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

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.

Check your Knowledge

Now it is time to check your knowledge and your understanding of this lesson. If you stuck in a question just click the small arrow to the left and an answer will be revealed.

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 CTRL + ALT + T.
    • On Mac: Open your applications folder and find “Terminal”.
How can you navigate to a particular directory?
    • You can use the cd command to change directories.
Where will cd on its own navigate you to?
    • On Linux and Mac, it will navigate you home.
Where will 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 command. Use ls -l to display the files in a list.
How do you create a new directory?
    • You can do this using the mkdir command.
How do you create a new file?
    • On Linux and Mac, use the touch command, e.g., touch new-file.txt.
How do you destroy a directory or file?
    • On Linux and Mac, use the rm command. To destroy folders, use rm -r or rmdir.
How do you rename a directory or file?
  • On Linux and Mac, use the mv command, e.g., mv folder/old-file.txt folder/new-file.txt.
    • Leave a Reply