DEV Community

Cover image for The Command Line for First-Timers
Zoe-KL
Zoe-KL

Posted on

The Command Line for First-Timers

This is a post for those who are right at the beginning of their coding journey, who have come across the command line perhaps whilst following a tutorial, and have been slightly intimidated by it.

You can do a lot at the command line, but what we are concerned with here is getting a general idea of what it is and a feel for working with it. This post is not just about giving you a list of basic commands.

Please note that I am myself a newbie, so I am approaching this subject through that particular lens. Also, as I have not had personal experience with Linux yet, this post covers the use of Microsoft Windows 10 and Mac operating systems.

Do I really need to know this?

As a beginner, you don't need to do much at the command line, but knowing what it is will make it slightly more familiar when you do come across it. You could be following a tutorial, for example, which asks you to install a program or run a file from there.

It is certainly worth taking a look at, in order to gain a concept of it and how it works, for when you do hear or read about it later.

Terminology

I will loosely define some terms, so you can refer back here if you need check a definition whilst reading this post:

  • Console - the window where you type text commands into your computer. It can also be called the command line interface.
  • Command - an instruction you type in for the computer to act upon.
  • Prompt - the text or character you see at the command line that is prompting you to type something in. It might end with a $ dollar sign, or have a blinking square.
  • Command Prompt - the name of the Windows command line interface.
  • Terminal - the name of the Mac command line interface.
  • Graphical User Interface (GUI) - the usual interface you use to interact with your computer using windows, icons and buttons.
  • Directory - a location for storing files, such as a folder or drive.
  • Path - the unique location name of a file/folder/directory on your computer.

(please note there do exist longer, more specific definitions of these, but I'm aiming to keep things as simple as possible here)

Where is it?

Why not open the console/command line interface on your computer right now and take a look at it?

With Windows, type cmd into your search bar at the bottom left of your screen. You can also press Windows + R, and then type cmd into the Run window.

With Mac, open your Applications folder, then Utilities & double-click on Terminal, or press Cmd + spacebar to launch Spotlight & type Terminal.

What does it do & why is it used?

The command line is basically a text-based way of talking to your operating system and the applications on your computer.

You can complete the tasks you normally do with your GUI by using menus or clicking icons in different windows. You can install programs, look inside directories, create and run files, and much, much more.

This is a quicker way to complete certain tasks, particularly if they are labor-intensive, for example renaming a whole batch of files. Often system administrators and IT professionals use it to get larger tasks done, and it can also be accessed remotely by them.

How do you use it?

Certain commands are typed into the console via your keyboard (no using the mouse here!) and you simply press Enter. You may get a reply, depending on the command. It could provide information you ask for, tell you it has completed a task, occasionally ask you a question, or it may give you an error message, if it doesn't understand your command.

Sometimes you might not get a reply at all, as it makes a change behind the scenes, for example when you are creating a new folder, or deleting one, which we will cover in a moment.

Some things to try

Here are a few things you can learn to do right now:

Moving around inside your computer

One of the simplest things you can learn to do at the command line is how to navigate around inside your computer.

When you first open the console window, the command line should tell you where in your computer it is sitting at that moment. This is also known as a path.

When I open mine I see:

C:\Users\zoela

So I know I am currently sitting in a folder on my computer called zoela, in a folder called Users, on my C: drive. (Yours obviously won't be exactly the same - it will tell you where it is sitting in your computer.)

To help you understand where you are, you can open your File Explorer (Windows) or Finder (Mac) in your GUI and navigate to the same place. The command line directories mirror the ones in the GUI.

Think of the directories as part of a tree - here my C: drive is the trunk, and Users is a branch, and zoela is further along the branch.

In order to see what is inside the directory you are in, for Windows, type:

dir (directory)

For Mac, type:

ls (list)

When you use this command, the screen will print a list of everything in that directory. Again, you can look in the equivalent place in your GUI and you will see all the listed files and folders in graphical, icon form.

Now, you can try to move about, or change directory, by moving further along the branch. For both Windows & Mac, type cd and then the name of the directory you want to move to. It could be any folder that is within the folder you are currently in - you will have a list of them on the screen from the previous command you ran. So, if I want to move to my desktop, I type:

cd Desktop

Now the command prompt will change and show you being in this new directory, with an updated path.

(Note that the command line tends to not be case-sensitive, so you could type Desktop or desktop and you would still get to the same place.)

Mine now says:

C:\Users\zoela\Desktop

This shows I have moved further along the imaginary tree branch, with another step further away from the trunk.

If you want to navigate the other way and back out of a directory, type:

cd ..

This will take you back one folder/directory, or one step closer to the trunk of your tree again.

Remember, at any point, you can type dir (Windows) or ls (Mac) to screen print a list of what is inside that directory, which can help you get your bearings.

We have looked at how you move up and down a branch one directory at a time, which is nice and simple when you are just starting to do it, but there are some quicker ways of moving from one directory to another.

A useful command is:

cd /

(Note this is a forward slash, not a backslash)

This will take you right back to what is officially called the root directory - though I personally find it easier to imagine this as the trunk of a tree.

So my path now says simply:

C:\

Moving all the way back here makes it easier to now navigate up a different branch to a different directory using cd and the next directory name, as we did before.

However, you can also take a leap (like a squirrel!) from one branch to another, without having to go back to the trunk/root directory.

In order to do this, you type cd, then \ (backslash), then the path of the location you want to go to, including all the relevant backslashes.

If you aren't sure how to write it out, navigate to that folder in your Explorer or Finder with your GUI, and you should be able to see and copy the directory path from that window.

Here is an example. I am currently at:

C:\Users\zoela\Desktop

I want to go to:

C:\Windows\System32\Microsoft

So I type:

cd\Windows\System32\Microsoft

And it takes me there.

The command prompt will tell me that I am now sitting in that directory.

Note that I didn't have to type C: as part of the path, because we were already on that part, the trunk, (or root directory). You only need to trace the path back to the first location that is shared with the branch you are currently sitting on, in this case the C: drive.

It is worth opening your console window and giving this a try as you read it. It will be much easier to understand if you actually do it.

Have your Explorer or Finder GUI window open at the same time in another window, if it helps you picture what you are doing.

Practice moving up and down directories, from the branches to the trunk and back, and maybe up and down a different branch, until you get the idea. Get the console to print a list of what is in the directory, if you want help to figure out where to move to next (as covered above).

So now you know how to move around your computer. Being able to do this is helpful in understanding how things are laid out, and how they mirror the GUI, even if you don't do much with it at this point.

Navigating around the command line window

Some good commands to know are ones that help you when working in the console window itself, because you will just be using the keyboard and not your mouse.

  • To clear the console window (a busy screen can be distracting):

cls (Windows)

clear (Mac)

  • Use the up arrow to cycle up through the previous commands you've typed, to save you re-typing something. Just press enter when you get to the line you want. The down arrow cycles back through the opposite way.

  • Ctrl + C or Cmd + C gets you back to the command prompt if you end up inside a different program inside the console. You will know this has happened if you don't see your directory name, as you would normally expect to, at the prompt. It will be showing something different, eg. >>>> In that event, try this.

  • Ctrl/Cmd + A - will move the cursor to the beginning of the line

  • Ctrl/Cmd + E - will move the cursor to the end of the line

  • Ctrl/Cmd + U - clears the entire line, in the event you make a mistake and want to retype it

  • exit - exits the console completely and closes the window

Creating & deleting a folder

In order to make a folder in a certain directory, you need to be inside it first, so the first thing to do is to navigate there, as you learned earlier.

When you give commands to your computer at the command line, it will execute them in the directory it is currently sitting in, unless instructed otherwise.

For both operating systems, the command for creating a new folder is the same: (please note - where I have put name_of_folder, type the name of your folder)

mkdir name_of_folder

This will create a folder, with this name, in the directory you are currently sitting in.

You will not get a response from the command line to say it has been done, so you can check it is there by asking for a list of your current directory contents - typing dir or ls, as you learned earlier.

You can also look in the GUI directory to see that it is mirrored in these too.

The command for removing a folder is also the same for both operating systems:

rmdir name_of_folder

This deletes the folder. (Be careful with this command - you don't want to delete something you need!) Once again, you can check it has been done by asking for a list.

This is just a simple example of how you can change things within your computer from the command line.

There is much more you can do, so if you feel confident, you can check out other tutorials covering this subject.

Installing from the command line

You might come across a tutorial where it asks you to install applications and programs from the command line. If you do, follow the tutorial and type in exactly what they suggest. If there is a $ sign, type in whatever text comes after that.

You might find, however, that some commands don't work, as they may be for a different operating system. This has happened to me a few times.

If they aren't working for you, type them into Google, or another search engine, with the name of your operating system, eg. Windows 10, Mac etc.

I have found this is the best way to work out what to type into the console, if the tutorial you are using hasn't provided you with alternatives.

Version Control

Lastly, something else that is often done at the command line is version control. Newbies won't really need to do this, but you may be curious to understand how pull requests etc. work when you sign up to GitHub, like I was.

I would recommend if you are just starting out with GitHub and experimenting with version control, that you download and use the GitHub desktop GUI at first, as that takes care of the command line part of the process for you. Once you understand the version control process itself, it will be easier to tackle it all from the command line.

And finally..

Well done for getting this far!

I hope that this post has given you a feel for the command line and also shown you that you don't need to feel intimidated when you open the console.

Thank you for reading. Good luck with your coding journey!

Latest comments (11)

Collapse
 
jabo profile image
Jabo

Well written! I love that you described every detail👏

Collapse
 
zoe_kl profile image
Zoe-KL • Edited

Thank you!

I needed someone to describe things really, really simply to me when I first learned this, so I tried to write something like that.

Collapse
 
svenaelterman profile image
Sven Aelterman • Edited

When you say "it is not possible to do a big leap from one directory (branch) to another this way," do you mean like this:
cd ..\..\Windows\System32 (executed from your example starting directory)
Because that is, in fact, possible ;)

Collapse
 
zoe_kl profile image
Zoe-KL

I really appreciate the feedback - I will review and edit the post to reflect this.

Thank you!

Collapse
 
redeving profile image
Kelvin Thompson

Congrats on your first article on Dev! Excellent! Cheers!

Collapse
 
dthompsondev profile image
Danny Thompson

This is really well done! Love how you broke everything down!

Keep doing amazing things Zoë!

Collapse
 
zoe_kl profile image
Zoe-KL • Edited

It took a long time because of that, but it was worth it! I'm pleased with it.

Collapse
 
moopet profile image
Ben Sinclair

It's cool that you're showing how to do things on different systems.

Can I suggest that when you say cd.. you add a space, to make it cd ..? The former will only work on Windows, and I've seen people who are used to it get frustrated that it doesn't in *nix shells.

Collapse
 
thefluxapex profile image
Ian Pride

You beat me to it lol I was getting ready to post the same thing when I seen your reply.

Collapse
 
zoe_kl profile image
Zoe-KL • Edited

Thank you! I will edit the post accordingly.

Collapse
 
zoe_kl profile image
Zoe-KL • Edited

Thank you so much for highlighting that, Ben. I hadn’t picked up on that.

I have now amended it within the text.