introduction to terminal

Introduction to terminal

I’m an Ubuntu user for a long time now, a little more than four years. I switched to Linux from Windows and didn’t have a clue about the terminal and how to use it. Since I could do almost everything by pointing and clicking, I did not find the CLI an attraction at first. But, through the years, I’ve grown to give it the love it deserves.

I’m writing this because I think you should learn to use the terminal. You cannot imagine what a great tool it is and how much more and much faster you can do through it. Also, you should learn to use Git, which is a lot easier to learn if you know a couple of terminal commands. I’m looking at you Sven.

In the quest of trying to make this as practical and enjoyable as possible, I’m going to guide through a real-life scenario I do very often. Let’s start.

The Scenario

I’m going to start building a new WordPress theme for a client. To do so I’m going to grab Base, my starter theme from Github and put it inside my themes directory under Public/intro-to-terminal/wp-content/themes/ directory. Afterwards, we start learning.

This is the scenario:

I browse to the base directory and view the files it contains. I check if it contains any hidden files too. After  that, I copy the contents of index.php into another file named home.php. Then, I rename hom.php into home.php because I typed it wrong the first time. Then I create a directory named dev and I move comments.php file into that because I don’t need it. I delete readme.md file altogether since it’s just for Github. After that, I go back one directory, and I copy the base directory into another directory named awesome-project and delete the original base directory and its contents.

Let’s recap. What we are going to learn to do through the terminal is:

  1. Browse to a directory
  2. View contents of a directory, hidden files too
  3. Copy the contents of a file into another file
  4. Rename a file
  5. Create a directory
  6. Move a file into a directory
  7. Delete a file
  8. Go back one directory
  9. Copy the contents of a directory into another directory
  10. Delete a directory

This is the 20% of knowledge you are going to need 80% of the time. Read it through and you won’t regret the time.

Browse to a directory

To browse to a certain directory we use the cd command. It stands for “change directory”.  So, in terminal we write:

cd Public/wp-content/themes/base

And hit Enter. That will run the command. And instantly we will be inside the base directory.

If you find writing the path tedious, it really is. But fear not, the terminal will help you. All you have to do is write the first letter of the directory name and hit tab key. That will auto-complete the name and save you a lot of time. You will have to enter more then one letter if you have directories starting with the same letter.

View contents of a directory

To view the contents of the directory we are in, all we have to do is run the ls command, meaning “list”.

ls

To view the hidden files too, just add the -a argument to the ls and you will be shown the hidden files too. Easy, huh? Yep, it is. Every time I need to show hidden files in Windows, I hit Google to find out how to do it. Thankfully that is not very often.

ls -a

Copy the contents of a file into another file

We now want to copy the contents of index.php into home.php. The command we use in this case is cp, which stands for copy. It takes the original file name as first argument and the target file name as second argument.

cp index.php hom.php

Spelling mistake is intentional — keep reading. You can run ls to check whether the command was successful or not. And sure it was.

Rename a file

Now we want to rename the “mistakenly”  misspelled hom.php into home.php. To do that we use the mv command which stands for move. I guess you expected to use something else, like a shorten version of the word rename. Well, that’s not the case. And renaming is nothing more than moving the contents of a file from an old on to a new one with a new name. Here:

mv hom.php home.php

It’s that easy. Check what happened. You should know what command to check that with.

Create a directory

Now I want to create a directory named dev where I want to move files that are only for development purpose. We create a directory using the mkdir command adding the name of the directory as its first argument.

mkdir dev

That will do. Check it out — ls.

Move a file into a directory

This particular project of mine won’t have comments, so I don’t want the comments.php around, but I don’t want to delete them either, so let’s move them into the newly created dev directory. To do it, we use the rename command, i.e. mv. Makes sense?

mv comments.php dev/comments.php

Isn’t that cool? So, we want to check if the comments.php file really ended inside the dev directory. We can do that from our current location without needing to cd (change directory) into the dev folder.

ls dev/

Remember this trick. You can replace the ls command with other commands without having the need to change directory. You cannot do that with mouse, right?

Delete a file

There is one particular file we don’t want to have around and that is the readme.md that Github uses to show information about the project. To get rid of it, we use the rm command which, as you can guess, stands for remove.

rm readme.md

This won’t take the file into trash, but will instead delete it once and for all. Use with caution.

Go back one directory

Why is going back important? Because the past shapes our future my friend. We want to move one directory up, in other words one directory back, because we want to rename the actual directory. And going one directory back is really easy.

cd ..

Yes, that’s it, two dots. Two dots means exactly one directory back. Run ls to check you really are one directory up.

Copy the contents of a directory into another directory

This is straightforward, right? We already know how to copy the contents of a file into anther file. Half-right, because this is a directory, but the command is the same, cp, it just needs an extra argument, -r, which means recursive, which means copy all the files and folders inside it too.

cp -r base project-name

Isn’t that cool?

Delete a directory

Now we want to get rid of that original directory, right? I guess you now know what we will do here:

rm -r base/

That’s right, we just add the -r because it’s a directory and we are done. Once again, rm won’t take the directory to trash, it will just delete it forever.

Bonus: Why rename directories in two steps

That’s right, we don’t need to. I just wanted to teach you copying and deleting recursively, otherwise, here it is how we would rename a directory in the terminal in the real world.

mv -r base/ project-name/

This is it, the most basic and practical tutorial on how to use terminal you will find on the Internet. We learned how to do routine stuff that we do everyday with a mouse through the terminal and enjoy it.

My final word is, terminal’s got potential, don’t underestimate it.