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.
The command line is just another way of navigating and manipulating files
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.
So why do I need the command line?
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.
Running commands
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.
Command #1: Viewing the current directory
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:
Or 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
Windows? Type 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.
On Windows, echo %20%
gives the same response. echo
pretty much translates to print
, and you're giving it a cd
variable, which tells it to print the current directory path.
Command #2: View everything in your current directory
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
Windows? dir
ls
stands for list, because you're listing all items in the directory, and dir
stands for, well, directory.
Command #3: go into another 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!
It is 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 cd ..
.
Tips and tricks
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 tocd Projects
. - To complete one command immediately after the other, you can type
&&
in the middle. So if I wanted tocd
into Documents and then find everything inside that directory, I can typecd 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
Ctrl
+ C.
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!
Top comments (10)
Are there devs who have never touched the command line? The horror and wrist pain from the mouse!
You can do a 99% of everything you need to do as a developer with just VSCode, Chrome, some vscode extensions, and keyboard shortcuts. Barely any need for the mouse :D
Haha beginner devs yeah! A lot of devs just do everything inside an IDE and coming out of that is scary to them. I was the same way when I was starting out!
This begins as a good introduction but appears to cease far too soon.
I would suggest adding in perhaps five basic commands overall for beginners to get stuck into - rather than just three - to give a slightly better overview.
Personally I would add
mkdir
andrm
as these are two very common and useful commands that I'm sure the majority of the community use on a daily basis.Overall this is a good start but with a couple of additions it could be great.
Keep up the good work.
I considered adding in more commands but I wanted this article to be relatively short so that people aren’t overwhelmed. I talked to quite a few people before writing this and the main thing they wanted to know was what the command line IS, not the commands. You can find commands everywhere, but they wanted to hear in simple terms the purpose of the terminal. So the 3 commands were just to help in understanding the purpose.
Thanks for your suggestion though - maybe I’ll try to shorten the article so I can fit in a couple more.
Yes, I understand your approach and I think that
mkdir
andrm
are things that most users will do outside of the command line (creating directories/folders and removing objects).As such, learning that you can do those exact same things via the command line will make it more attractive and/or fascinating to those people because they will be used to using a GUI to perform those actions.
I think these can be added in and the article still be kept relatively short still.
Either way it's a good introduction.
Some comments may only be visible to logged-in visitors. Sign in to view all comments.