Hey geeks! Today we're going to be talking about the command line. If you're on dev.to, chances are you're familiar with the command line and this article may not be super useful for you. But if you're new to coding and are a bit scared to get started with the command line, read on my friend!
The command line is scary. It's scary because it's unknown. When I first started using the command line, I automatically associated it with Linux and ended up actually INSTALLING LINUX so that I could properly learn the command line. You don't need to do that. You can do it on MacOS and Windows too.
You may have heard a few other words like Terminal, Shell, Command Line Interface, or CLI. These all mean the same thing. Sometimes, in Linux communities, these words have very slightly different meanings, but most of the time when people say any of these words they are talking about the same thing.
Go into Finder (Mac) or File Explorer (Windows) and open some folders on your computer. Go backwards and forwards and view all your files. Create a new folder, delete it, open a document. What you're doing right now is exactly what you'll be doing in the command line too, except it's text-based instead of GUI-based. And folders are actually called 'directories' for no reason. That's literally it. It's just another way of navigating through your files.
Honestly, you could potentially go your entire dev career without ever using it. But it would cost you hours, and it would be WAY more difficult than it needs to be. Developers use the command line to install things (like languages, libraries, and frameworks) and to run code. If you go onto GitHub and view almost anything, you'll see that it gives you directions on how to install by giving you commands to run. You run those commands in the command line and voila! You've installed a new library.
To run commands into the command line, type in the command and press Enter. Sometimes, the command might need some extra details. I'll explain a little more about this later.
It's time. Open your command line. On Mac, there is a built-in app called Terminal that you can use, and on Windows it's called cmd.exe, or Command Prompt.
As soon as you open it, you'll probably see something like this:
You may be asking yourself 'What is this? Where am I?' To find out the current directory (folder) that you're in, it depends on your operating system.
Mac OS or Linux? Type
pwd and press enter
echo %20% and press enter
Pwd stands for
present working directory and it will tell you exactly where you are. You can run this command as you're navigating through files if you're ever lost (which is very likely to happen) and it will return the file path of the directory you are in.
echo %20% gives the same response.
echo pretty much translates to
cd variable, which tells it to print the current directory path.
When you run this, the command line will tell you everything that inside your present working directly. You'll see a list of all the directors and documents inside your current directory. This command is operating system specific too.
Mac OS or Linux?
ls stands for list, because you're listing all items in the directory, and
dir stands for, well, directory.
This command allows you to 'move' into another directory, making it your present working directory. It is probably the command you'll use the most in the command line!
cd and it stands for
change directory. It's the same on MacOS, Linux, and Windows.
This is one of the commands that needs more information. If you just typed
cd, the command line would be like "yo, where you wanna go?" So after
cd , you'll type the name of the directory you want to go into. Like
cd NewDirectory. It is based on your current position. What this means is that you need to include the path for the command line to follow to find that directory.
For example, I've just navigated to the Documents directory, and I want to get into the Projects directory. My Projects directory is inside my Documents directory, so I just have to type
cd Projects in order to change into it. However, what if I'm in the Documents directory and I want to change to a directory that is inside the Projects directory called TwitterBot? To do this, I don't have to do
cd Projects and then
cd TwitterBot (although that will also work.) I can instead run
cd Projects/TwitterBot which is the relative file path of TwitterBot to Documents.
You can also use
.. to return to the previous directory. So if I want to change directory out of TwitterBot and back into Projects, I can run
Here are a few tricks to make your command line experience awesome:
- Most command line apps come with an auto-complete feature, normally by pressing tab. So if you wanted to go into a Projects directory and no other directory begins with P, you can type
cd P+ tab and the command line will auto-compete to
- To complete one command immediately after the other, you can type
&&in the middle. So if I wanted to
cdinto Documents and then find everything inside that directory, I can type
cd Documents && ls.
- You can personalize your command line to make it look exactly how you want! There are even more command line apps that you can download.
- You can cancel a command by typing
And there you have it - your first introduction to the command line. It wasn't that scary, was it? For more information and help for those visual learners, check out the YouTube video!