DEV Community

loading...
Cover image for Get to Know the Command Line: Basic Commands

Get to Know the Command Line: Basic Commands

torianne02 profile image Victoria Crawford ・5 min read

Two years ago, when I first started learning to code, I found myself intimidated of the command line. This is something that I feel a lot of beginners experience. If you go watch a video tutorial through the eyes of a newbie, it is easy to understand how they can view the command line as something scary. They see an experienced dev effortlessly throwing around git commands, changing directories, or creating, moving, and renaming files and folders via the command line. It looks like magic. The problem is they aren't sorcerers, they're just beginners being exposed to the magic for the first time.

Jesse from Breaking Bad turning 1 finger on each hand into 2 fingers on one hand

Now, I'm not leading up to saying that all devs creating tutorial videos need to stop and explain every command they throw into the command line. I'm creating this series to hopefully help familiarize beginners with the command line so that it isn't quite so intimidating.

For you newbies out there reading this, don't worry! The commands you will be using in the command line will become second nature for you and you'll wonder why you were ever afraid (just like me)!

A quick note before we get started, I will not be covering git commands in this blog post. I believe this can be a whole blog post of it's own, which I will get to next in the series. If you want to learn more about git right this instant there are plenty of great git articles right here on DEV.

With that said, let's get started!

Commands

Create a Directory

For those of you asking yourself what a directory is, it is just a folder so don't be intimidated. In order to create a directory we use mkdir, which stands for make directory, along with the name you wish to assign to this directory. The entire command looks like mkdir NewDirectory.

You can even create multiple directories at once by adding the names you wish to give those directories. This would look like: mkdir Directory1 Directory2 Directory3.

Change Directory

This is the command that I use the most. If you ever want to switch directories all you have to do is use cd, which stands for change directory, with the name of the directory you want to enter. Let's say you want to enter your ~/Development directory, then you'd use the command cd Development/ from within the directory your ~/Development directory exists.

Now if you want to go back to the directory you were in before ~/Development, you can use the command cd ... This command navigates up one directory.

Create a File

Now that we have a working directory and we know how to change into this directory, let's create a new file inside the directory. To do this we use the command touch with the name of the file you want to create. The full command looks like touch NewFile.rb.

List all Files in a Directory

Now what if you are within a directory and you are unsure of the files that exist within that directory? Well this is where the ls command comes in handy. All you need to do is type in ls and press enter.

Note: There are more advanced commands that go along with ls but I will not be discussing those here. If you'd like to get more information on them, this reference is useful.

Print Working Directory

If you are ever questioning which directory you are currently working in, use the command pwd which stands for print working directory. In my case, when I am in my ~/Development directory and I use pwd I receive /Users/vfluharty/Development as my current file path.

Copy a File

If you want to copy a file, all you have to do is use the command cp, which is short for copy, in combination with the file name you wish to copy and the destination of where you want this copy of the file to exist in. This command as a whole looks like cp file_to_be_copied.rb /Desired/Destination.

Move a File

If you want to move a file from it's current location, you'd want to use the command mv, which is short for move, in combination with the files current path and the desired destination path. This command looks like mv ~/Development/File.rb /Desired/Destination/.

Rename a File

If we want to rename a file via the command line, we get to use our newly learned mv command. You might be thinking "how is this possible?" Well, you just move the file to the same location, but you change its name. The command would look like this mv ~/Development/File.rb ~/Development/MyFile.rb.

That isn't the only way to rename a file though! If you are wanting to copy a file and rename the copy you've created, then you make use of the cp command. You'd want to use this command similar to what we discussed above but with one small twist. Instead of just giving the command the file path you want the newly copied file to exist in, you give it all that plus the name you want to give the file. This would look like cp ~/Development/File.rb ~/Development/MyDirectory/File1.rb.

Remove a File

At some point in time you may want to delete a file. In order to accomplish this we use the command rm alongside the name of the file we want to remove. The whole command would look like rm file.rb.

Remove a Directory

In the case of removing a directory, similar to removing a file, we use the command rm -r with the directory name. Let's say I want to delete a folder from my ~/Development folder called /MyFolder. First, I would make sure I am in my ~/Development folder, then the command I would use is rm -r MyFolder.

Final Thoughts

If you are a beginner who finds the command line intimidating, I'm hoping that by this point in the article you feel a little less intimidated. If you still feel really scared, go through the article again with your terminal open playing around with the different commands. The more repetition and practice you get, the easier and less scary it will become.

To anyone out there that wants an awesome interactive resource that'll help you learn each command, take a look at explainshell. This was introduced to me in the discussion below by Jochem Stoel. I wish I would have known about this resource way back when, so I thought I'd share it with everyone who may not read every comment in the discussion thread.

Anyways, you now have some great commands for the command line in your tool belt, go use them!

Old Man Saying "Go Get 'em" to a Kid

Resources

Master the command line: Copying and moving files on the Mac
List of Command Line Commands

Discussion

pic
Editor guide
Collapse
patricnox profile image
PatricNox

Something super useful but not widely known, is " cd - "

// ~/
$ cd docks/proj/src/component

// ~/docks/proj/src/component
$ (something useful)

// ~/
$ cd -

Also, just "cd" is same as "cd ~" which brings you to the home dir.✔️

Collapse
torianne02 profile image
Victoria Crawford Author

As I stated in the other comment, I had no idea cd - was a thing before reading the discussion. I can think of multiple different times I accidentally cded somewhere when I didn't want to and had to type everything out again. This is going to be a nice time saver!

Collapse
abregman profile image
Arie Bregman

Great post :)

It's worth mentioning that you can create several nested directories with -p this way:

mkdir -p /dir1/dir2/dir3

Also, for creating multiple files you can use the following syntax:

touch file{1..3} which will create file1 file2 and file3

Collapse
torianne02 profile image
Victoria Crawford Author

Thank you so much for adding these to the discussion!! I was not aware of these two little tricks.

Collapse
bennypowers profile image
Benny Powers 🇮🇱🇨🇦

Great intro, I will definitely share this when asked.

Now if you want to go back to the directory you were in before ~/Development, you can use the command cd ... This command navigates up one directory.

TIL you can cd - to go back

cd Documents
pwd 
/Users/bennyp/Documents
cd
pwd
/Users/bennyp
cd ..
pwd
/Users

cd
cd Documents
pwd 
/Users/bennyp/Documents
cd
pwd
/Users/bennyp
cd -
pwd
/Users/bennyp/Documents
Collapse
torianne02 profile image
Victoria Crawford Author

I had no idea this existed! This is awesome. Thank you for sharing.

Collapse
bennypowers profile image
Benny Powers 🇮🇱🇨🇦

Yeah I literally looked it up today.

There's pushd and popd also, but I wanted something that works like the back button even if I hadnt pushed.

Google brought me to unix.stackexchange.com/questions/8...

Collapse
martinfeineis profile image
Martin Feineis

One of my favourite aliases is hi=history | grep
makes it super easy to get the commands I know have a certain string, be it the name of the command, a substring of a hostname or an argument to a command.

Collapse
tam360 profile image
Mirza

You can also use ! to execute specific command from the history list like !1986 where 1986 is the command ID Using history with grep and !

Collapse
torianne02 profile image
Victoria Crawford Author

I'm planning on covering aliases in another blog post for this series! They are actually something relatively new to me, so thank you for introducing this one to me and adding to the discussion.

Collapse
jochemstoel profile image
Jochem Stoel

Thank you for the article, Victoria. You and those who read this article might benefit from explainshell, a website where you can enter a shell command (with optional arguments) and it will explain to you what everything means.

Try it out by entering some command with arguments. :)

Collapse
torianne02 profile image
Victoria Crawford Author

That link is absolutely amazing! I wish I would have known about it earlier. I'm going to edit the article and add it. Thank you so much for adding this to the discussion.

Collapse
fidelve profile image
Fidel Sanchez-Bueno

cd and ls are by far the two commands I use the most, especially ls -la

Collapse
sinteticwizard profile image
Ramon Ramirez

Excelent introduction to cmd