Introduction: Welcome to the Command Line!
Hello there! As a developer, one of the most powerful tools at your disposal is the command-line interface (CLI). While a window with a blinking cursor might seem intimidating at first, you'll quickly discover that it's a remarkably efficient environment, often much faster than clicking through graphical menus for many tasks. Think of it as learning the secret language of your computer—once you know a few key phrases, you can make it do powerful things with just a few keystrokes.
The goal of this tutorial is to give you a hands-on introduction to a handful of essential commands. We'll start with the basics to help you build a solid foundation and the confidence to explore further. Let's dive in!
1. Accessing Your Terminal
Opening Your Command-Line Interface
The command line is accessed through a program called a terminal. How you open it depends on your operating system.
- macOS: The quickest way is to use Spotlight. Press Command + Space, type
terminal, and press Enter. - Linux: You can typically use the keyboard shortcut Control + Alt + t. Alternatively, you can search for the "Terminal" program in your application list.
- Windows 10: This tutorial uses the "Bash on Windows" feature, also known as the Windows Subsystem for Linux (WSL). The classic Command Prompt and PowerShell programs are not compatible, as they use different commands.
2. Understanding the Prompt
Once your terminal is open, you'll see a line of text with a blinking cursor. This is the prompt, and it's where you'll type your commands. It may look cryptic, but it provides valuable information about your current session.
Let's break down a typical prompt: brian@puzzles:~$
Component | Meaning |
| This is your username. |
| This is your computer's hostname (its name on the network). |
| This is your current working directory. |
The tilde (~) is a special character that is a shortcut for your personal home directory. This is where your documents, downloads, and personal settings are stored. When you first open the terminal, you always start in your home directory.
--------------------------------------------------------------------------------
Section I: Finding Your Way - Filesystem Navigation
The very first skill you need on the command line is learning how to move around. Your computer organizes everything in what's called a filesystem, which is simply a hierarchy of folders (which we call directories) and files, all starting from a single root location.
1.1. Where Am I? The Command
If you ever feel lost, the pwd command is your map. It stands for "print working directory," and its only job is to tell you exactly where you are in the filesystem.
pwd
/home/brian
1.2. What's Around Me? The Command
The ls (list) command shows you the contents of your current directory. By itself, it gives a simple list of files and directories. However, its real power comes from adding "flags" (also called "options"), which are short modifiers that change how the command works.
Here are three essential flags for a beginner:
-l: This gives a "long" listing, which provides detailed information about each item, including its permissions, owner, size, and the date it was last modified.-a: This shows "all" files, including hidden files. Hidden files on macOS and Linux have names that start with a dot (.), and they are usually configuration files that you don't need to see all the time.-h: When used with-l(e.g.,ls -lh), this makes file sizes "human-readable." Instead of a large number like4096(bytes), you'll see something much clearer, like4.0K(kilobytes).
1.3. Moving Around: The Command
The cd (change directory) command is how you move from one location to another. To use it, you need to provide a path, which is like a set of directions. There are two types of paths:
- An absolute path is a full address starting from the very beginning of the filesystem, called the root (
/). No matter where you are, an absolute path will always lead to the same place. - A relative path is a set of directions starting from your current location. It's a shortcut for moving to a nearby directory.
To make navigation even faster, there are a few essential shortcuts you should know.
Shortcut | Description |
| Takes you directly to your home directory ( |
| Moves you "up" one level to the parent directory. |
| Takes you to the absolute root of the entire filesystem. |
| Another way to go directly to your home directory. |
1.4. Practice Time: Navigation
Let's put these commands into practice. Try performing these tasks in your terminal:
- Print your current directory.
- Use
ls -ato see all the files in your home directory, including the hidden ones. - Change directory to the root of the filesystem (
/). - List the contents of the root directory.
- Use a single command to return directly to your home directory.
Now that you know how to navigate your filesystem, let's learn how to create and manage your own files and directories.
--------------------------------------------------------------------------------
Section II: Working with Files and Directories
Beyond just moving around, the command line is an excellent tool for creating, viewing, copying, and organizing your files and folders quickly and efficiently.
2.1. Making Directories with
The mkdir (make directory) command is used to create new directories. It's as simple as typing the command followed by the name of the directory you want.
mkdir sharptools
One of the most useful flags for mkdir is -p. This flag tells the command to create any necessary parent directories along the way. For example, if you wanted to create a diagrams folder inside a docs folder that doesn't exist yet, this command would do it all in one step without an error.
mkdir -p files/docs/diagrams
2.2. Creating Files with and
There are many ways to create files on the command line, but two are perfect for getting started.
- Using
touch: This is the simplest way to create a new, empty file. - Using
echo: Theechocommand prints text to the screen. When you combine it with the output redirection symbol (>), you can create a new file that contains that text. The>tells the shell to "redirect" the output of theechocommand into a file instead of the screen.
2.3. Viewing Files with
To quickly view the entire contents of a file, use the cat command. Its name is short for "concatenate" because it can be used to string multiple files together, but for now, just think of it as the go-to command for seeing what's inside a small file.
cat greetings.txt
2.4. Copying Files with
The cp (copy) command does exactly what it sounds like. The syntax is cp [source_file] [destination]. You can use it to copy a file to a new location, like from one project folder to another.
cp files/code/elm/README.md files/code/js/
2.5. Moving and Renaming with
The mv (move) command is versatile and serves two related purposes:
- Moving a file: You can use it to move a file or directory from one location to another.
- Renaming a file: Renaming a file is just "moving" it to the same location but giving it a new name. This is the primary way files are renamed on the command line.
For example, to rename a directory from markdown to employee_handbook:
mv files/docs/markdown files/docs/employee_handbook
2.6. Practice Time: File Management
Time to get your hands dirty with these new commands.
- Create a new directory called
my_project. - Navigate into the
my_projectdirectory. - Create a new, empty file inside it called
notes.txt. - Add the text "My first note" to a new file named
readme.txtusing theechocommand. - View the contents of
readme.txtusingcat. - Rename
notes.txttodraft_notes.txt. - Navigate back to your home directory.
You're now equipped to navigate and manage files. Next, we'll cover how to add new programs to your system and run commands with special permissions.
--------------------------------------------------------------------------------
Section III: Gaining Superpowers - Managing Your System
Some tasks, like installing software that all users can access or creating files outside of your personal home directory, require special administrative permissions.
3.1. Elevating Privileges with
On your computer, there is a special user account, often called the "superuser" or "root," that has permission to do absolutely anything. For security reasons, you don't log in as this user for daily work. Instead, when you need to perform a single administrative task, you use the sudo (short for superuser do) command. This lets you run a single command with elevated privileges.
For instance, trying to create a directory in a system location will fail for a regular user.
mkdir /var/website
mkdir: /var/website: Permission denied
But by prefixing the command with sudo, you tell the system to run it as the superuser. You will be prompted for your password to confirm you have the authority to do this.
sudo mkdir /var/website
3.2. Installing New Programs with a Package Manager
Your operating system has a special program called a package manager, which works like an app store for the command line. It makes it incredibly easy to find, download, and install new software tools. The command you use depends on your operating system.
Operating System | Installation Command |
Ubuntu (or Bash on Windows) |
|
macOS (with Homebrew) |
|
Let's try installing tree, a very useful tool that visualizes a directory's structure in a tree-like format.
- On Ubuntu / Bash on Windows:
sudo apt install tree - On macOS (with Homebrew):
brew install tree
Once installed, you can run the tree command in any directory to see it work!
3.3. Practice Time: System Management
Let's put your new superpower to use.
- Use your system's package manager to install the
treeprogram (if you haven't already). Then, runtreein your home directory to see its output.
--------------------------------------------------------------------------------
Conclusion: Your Journey Has Just Begun
Congratulations! You've taken your first major steps into the world of the command line. It might seem like a lot, but you've just learned the three most fundamental skills for any developer: navigating the filesystem, managing files and directories, and using administrative privileges to install new software.
Mastering these commands is the gateway to becoming more proficient and efficient. This is a skill that will serve you throughout your entire career in technology. Keep practicing, stay curious, and you'll be a command-line pro in no time. Happy coding!
