Learn to control Linux like an expert from the terminal
Using the Linux terminal is like having the power to control your computer with your fingertips. It’s where you can unleash your true potential as a user and truly customize your experience.
If you want an introduction, see this article first
The console, Shell, Bash or simply terminal of the GNU/Linux OS, is a command interpreter that allows us to interact with the same operating system, that is, we write a series of commands, and immediately we have a result without the need for any Graphic interface.
Any Unix system like Linux distributions or different versions of macOS have their own shell.
Well, even Windows has a very good option
Considerations before you start
- The terminal greets you with something like this
[ricardoveronica@manjaro ~]$
Where ricardoveronica is my username and Manjaro is my machine name. This depends on which distribution you are using and which terminal you have, for example in manjaro with “Gnome minimal LTS”, ZSH comes by default with an “Oh my Zsh” theme, so my terminal ends up looking like this…
- If you want to clean the terminal, press the keys
CTRL + L
- To cancel a command when you made a mistake press
CTRL + C
Terminal has autocompletion, so press the key with a pair of arrows called Tab on the left side of your keyboard just above Caps Lock to autocomplete whatever you’re typing.
What are we going to do
We are going to move in our operating system from the terminal and do basic functions without needing the mouse, that is, we are going to emulate what we would do with the GUI such as creating, copying, deleting files and folders, along with other interesting curiosities that will be useful to you. I assure you.
I recommend that if you want to see the complete magic, you have the file explorer open on one side (in my case it is Nautilus).
Moving in the terminal like a ninja
When you want to see the folders and files that are in your user, you just open the file explorer and see the folders, Documents, Images, Videos, Downloads, and a few more, just as the image above shows.
Come on, open the file explorer and go to your user’s home, there are the folders that your operating system generates by default, but how can we see these folders in the terminal?
Before this, make sure you are in your user’s home /home/ricardoveronica in my case
- pwd — Print Working Directory
pwd
As a result, you will see something like this
Now type the following command, and you will see a list with the files and folders that you can also see in the file explorer.
- ls — List
ls
Very well, it’s time to move between directories, which is what folders are called in GNU/Linux systems, the following command is as if we double-clicked the folder we wanted to open, the command for this is as follows.
- cd — Change Directory
cd Videos
Remember that you can press the Tab key to autocomplete while you type the name of the Directory, in my case I am going to enter the Videos directory, immediately after that I use the previous command ls again to see the content of my directory, this you can do it with double-click in the file explorer.
As you can see I only have 2 directories within Videos, I created these directories to save movies and some recordings, so it is very likely that you do not have them. (Don’t worry, I’ll show you how to create your own directories later.)
Now go back to the previous directory, taking into account that the directory where we are positioned is always “.” and the directory before the current one is always “..” with this in mind, type the following to return
cd ..
We are emulating clicking the back arrow in the file explorer, this is getting interesting, don’t you think?
Creating and modifying files and directories
Excellent, we are now free to move through the directories of our system and be able to see what is in each one of them, it is time to create our own directories and files.
Let’s see the first command, this is to create a directory where we are positioned, let’s call it Test
- mkdir — Make directory
mkdir Test
This is like right-clicking in the file explorer, then another click on new folder and giving it a name, all with just one command in the terminal.
Wonderful, now we want to create a file in our new directory, for that we have the following command at our disposal, which among other functions we have the creation of files.
Remember to first use the cd command to enter the Test directory
- touch
touch index.html
Again, this command saves us from right-clicking new document and choosing a file type and then naming it, I hope you’re already realizing the steps we’re saving with just a couple of words in the terminal, and wait because this is going to improve.
Commands to write and read files in the terminal
I know that editors like VS Code are in fashion, that more than a simple editor it is a complete IDE full of excellent features, also products like those from Jet Brains, which, although they are perfect for saving time and punching buttons on our keyboard, in Personally, I see that they are not so much for people who are starting in the world of programming, because you go directly to a solution that perhaps you do not even know what the problem is that it solves, and unfortunately this occurs very often in this world. I feel like this is bad teaching.
Therefore, let me introduce you to one of the techniques for writing text into a file, used eons ago, when humanity was still in its infancy, and brontosaurus meat was the favorite dish, I present to you the command…
- echo
With it you can do several things, such as write to the standard output of the terminal or more importantly, write to the terminal and pass what you write to a file without the need for any IDE or text editor, go try these commands and see what each one does.
echo "<h1>Hello World</h1>"
echo "<h1>Hello world</h1>" > index.html
As you can see, the first one only prints what you write between “double” or “single” quotes in the same terminal, try to write several words without quotes so you can see the result.
The second command allows you to pass what you wrote by means of a greater than (we will see what this is in another post) to a file that exists within the directory in which you are positioned.
You will surely love what follows, since you do not need any extra software to read what we have just written in our HTML file.
The fastest way to read what is inside a file is directly inside the terminal, these are two commands that can help you do that
- more
more index.html
- cat — concat
cat index.html
Well, try to write something else inside the HTML file in the same way, with the greater than sign, that we used to write the h1, and when you try to read it you will notice something strange…
What you wrote before is replaced by the new you write, if you try it several times you will see that it is always like this, the old is deleted and only the new is written, this is not ok, how about if I want to write another line just below what I already have written in the file?
Well first write the same line we had before
echo "<h1>Hello world</h1>" > index.html
Now, to solve the previous problem, we must use the double greater than
echo [new-line-to-write] >> index.html
Every time you use double greater than, whatever you type will be added as a new line to your file, cool, isn’t it?
Well, I have something even cooler, HTML files or Hyper Text Markup Language, are used to describe the structure of a website, and any web page can be viewed in an internet browser, so you can open your browser from the terminal and pass the file that we just wrote to see it in a tab of Chrome, Firefox or the browser you use, right now I’m going to do it in Chromium
Immediately, typing this command will open your web browser with the following
For chrome, you can try google-chrome index.html or google-chrome-stable index.html, for Firefox you can use firefox index.html
Remember that this command will only work if you write the exact path where your HTML file is.
Commands to move and delete files
Well, every time we have more tools to master the terminal, but in case we want to delete everything we have done, what is the correct way? There are actually many ways to do it just like with the other commands, but as we’ve seen so far, we’re going to use the most direct way.
Let’s start by copying a file into another directory, as if we were using the famous key combinations
CTRL + C — Used to copy a file
CTRL + V — Used to paste the copied file into another directory
To do this, there is a special command called
- cp — Copy
First we need to create a new directory, which is where we are going to copy our HTML file, notice that we are positioned in the Test directory
mkdir other
You can use the command to list what we have inside the Test directory and make sure that the new directory exists.
Once we have both the new directory and the HTML file, we are going to copy the file to the directory with a single command, you can forget about mouse clicks at this point
cp index.html other
With the previous command, we order the system to copy index.html to our new directory other. If you don’t think that much magic is possible, directly list the other directory to ensure that the HTML file is in that directory.
ls other
In case you haven’t noticed, this command takes a copy of the original file, so we have the HTML in Test and in other, what do you think if we delete the original, the one in the Test directory, you can do this with the following command
- rm — Remove
rm index.html
Perfect, do you realize that we do not use the Delete key or right-click on the file and delete at all? These details save you a lot of time while working on your operating system, in addition to making you look like a Hacker
Very nice yes, but what if I want to move a file to another directory, but without copying anything, something like what is done with the keys
CTRL + X — Used to cut a file
CTRL + V — The one we use to paste the file from another directory
Good news, there is a command for that too
- mv — Move
This command has 2 widely used utilities, one is to change the name of the file we are pointing to, for example, we can change the name of our beloved index.html to something else with the following command.
First make sure you enter the new directory other
mv index.html app.html
The second utility is precisely to move a file to another directory, we are going to move app.html to the previous directory, that is to say to Test
mv app.html ..
Done, you can use ls to see that the HTML is gone. Once this is done, let’s change our position to Test with
cd ..
Ok, now we have our other directory completely empty, so we can remove it with the following command. Yes, we can also delete directories with the power of a single command
- rmdir — Remove Directory
rmdir other
Now if what you want is to delete a directory that has other directories or files inside, you can try it with the rmdir command, for example, go to the previous Test directory (you already know how to do this) and try to delete the same Test that has our HTML inside, you will find something like this
This message tells us that the Test directory is not empty and the rmdir command cannot delete directories with some content inside.
For this type of cases we can use the rm command with a couple of flags to delete directories and all their content inside.
rm -rf Test
This means that we want to delete the directory -r of recursively and also -f of forcibly.
Now you are ready to put your mouse aside and tell everyone nothing and no one will stop you from conquering the world, or whatever your evil plan is for the future of humanity.
And well, before carrying out your plans of conquest, you can use man to see the manual of any command that you have questions about or want to delve into how it works.
man rm
Conclusion
The best way to learn is by practicing, so feel free to repeat each of the commands, play around a bit, change their order, if you have any doubts about what happens if you modify any of them, do it, experiment yourself, I recommend you try it in a directory or folder like the one we created here called Test to avoid any problem with an important file in a system folder, for the rest treat yourself and remember that any questions you have about it, you can write to me to give you a hand.
If you like my content, please consider subscribing and supporting my work by liking, commenting or sharing this or any article I’ve written.
Contact me
ricardo_veronica.duran@hotmail.com