Please wait

The Command Line

The command line can be intimidating, especially if you're new to web development. However, it's one of the most critical skills you can pick up for PHP development.

What is the command line?

The command line is a text-based interface for interacting with a computer's operating system. Instead of using a graphical user interface (GUI) with buttons and menus, the command line allows you to enter text commands to perform various tasks on the computer.

The command line is often used by developers and advanced users who prefer to work with text-based interfaces, as it can be more efficient and allow for more advanced control over the computer's operating system.

You don't need to be an expert

There are dozens of commands available. Luckily, we don't need to be experts with the command line. We're going to focus on a handful of commands. The other commands won't be relevant to us. Being a master of the command line is unnecessary for getting into PHP development.

The commands we're going to learn will be all you'll need. Let's get started.

Opening the command line

The first step to using the command line is to open it. If you're on a Windows machine, search for a program called Powershell. Linux and Mac users can find the command line by searching for a program called terminal.

The Command Line
The Command Line

The command line is a window with light text against a dark background. If the colors of your command line are different than what's shown on the image, that's perfectly fine. Each operating system uses a different color schema.

Why use the command line?

Before the existence of websites or interfaces, computer interactions were performed with the command line. Sending emails, opening files, or playing audio would be performed through commands. You couldn't point and click like you can today.

Developers prefer to use the command line for running programs. Interfaces can consume memory and CPU usage. Whereas the command line will not massively affect the performance of your tools. As developers, we should aim to develop applications as quickly as possible. The command line is our best bet for rapid development.

Initially, you will not be presented with walls of text. Normally, the command line will point to a specific directory. In my case, the command line is pointing to the C:\Users\Luis directory.

You may be in a different location. In some cases, you may see a $ symbol. This symbol separates the file path from your commands. If you see this symbol at the beginning of a code snippet from a tutorial, you do not need to type it out.

If you're in a deeply nested directory, the command line may partially omit the path. The reason is to save space on the command line. It would be annoying to view the full path of a directory on every line.

If you're not sure where you are, there's a command for viewing the current location called pwd.

$ pwd
 
Path
----
C:\Users\Luis

The pwd command is short for print working directory. Upon entering this command, we are presented with the entire system path. If you're ever lost, this command can be helpful in giving you the current location.

List files and directories

Now that we have an idea of where we are let's move around. But where can we navigate to? Before you can navigate around, you have to know where you can navigate to. The next command on our list is called ls. This command is short for the word list.

$ ls
 
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-r---          2/3/2023   7:14 PM                Desktop
d-r---         1/27/2023   1:32 PM                Documents
d-r---          2/5/2023   5:34 PM                Downloads
d-r---        12/16/2022   1:54 PM                Music
d-r---         1/19/2023   9:59 PM                Pictures
d-r---        12/17/2022  12:29 PM                Videos

Executing this command outputs a list of files and directories in the current directory. The list may be formatted differently based on the operating system.

Changing directories

Let's try moving into a directory. To move into a folder, we can run the cd command, which is short for change directory.

Unlike the other commands, you must specify the name of the directory to move into by typing the directory's name after the command. For example, let's say you wanted to move into a directory called music.

cd music

Relative directories only!

It's important to understand that the directories you input after the cd command must be relative to the current working directory. Otherwise, you may receive an error from your system.

Moving into a directory is not our only option. Moving out of a directory can be performed by typing two dot (..) characters. Two dots will tell the command to move up a directory.

cd ..

To move through multiple directories, we can separate the paths with the / character.

cd ../..

This behavior also applies to moving into directories. As an exercise, try moving into deeply nested directories. If you ever get lost, just use the pwd and ls commands to help you understand where you are.

Code editor terminal

Most modern code editors have the option to open the terminal directly from the editor. You should consult your editor's documentation for information on how to open the command line. If this option is not available in your editor, you can always stick with the command line program provided by your operating system.

For Visual Studio Code users, you'll be able to open the command line by going to Terminal > New Terminal.

Opening the command line in VSC
Opening the command line in VSC

Visual Studio Code's terminal automatically points to the current project directory. Thus saving you time from having to navigate to your project.

Key Takeaways

  • The pwd command prints the current directory.
  • The ls command outputs a list of files and directories in the current directory.
  • The cd command changes the current directory.
  • You can use two dot characters (..) to move up a directory.
  • You can move through multiple directories by separating them with a / character.
  • Most modern editors ship with the option to open the command line directly in the editor.
  • It's not necessary to learn other commands. While it wouldn't hurt, they're not going to be used in this book. You've learned all the commands you'll need for this book.

Comments

Please read this before commenting