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.
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!
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
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.
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.
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
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
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.
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.
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.
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/.
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.
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
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.
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!