Photo by Arthur Reeder on Unsplash
The terminal is one ( if not the ) most important and useful tool with Linux as it allows us to interact with the OS without the need for a graphic user interface. When we access a server through SSH, or we want to interact with our own machine, we can make use of the Terminal and execute what is called a command to tell the OS what to do. ( This post won't make a difference between Terminal, Shell, and bash and focus on what the commands are useful for.
Here is the list of commands i am going to talk about in this post :
Before we begin did you know you can open a terminal using
ctrl
+alt
+t
Commands
man
The first command I'm going to show you is a command I use ( and I think many developers use) a lot is man. The man command is like inside terminal documentation to help you understand what the command does and how you can use it, as well as what parameters it accepts.
$ man ls
Here you can see an example applied to the command ls which we will see later in the post. To quit the documentation, press q. If you want to know more about this command you can also use man itself.
$ man man
Before we dive deeper, I want to share my vision and how I visualize those commands. The file system in Linux is a tree.
When you open a terminal ctlr
+alt
+t
( Linux, Debian ) you're assigned a "cursor". That means you're placed at a certain position in the tree. By default, you're cursor point to the user's home directory ( /home/YOUR_USERNAME ). The commands I'm going to show you are used to either move your cursor in that tree or add/delete nodes, which as you guess are directories or files.
pwd
The second command is pwd. It stands for Print Working Directory which means it will show you your current location in the tree structure.
$ pwd
If I change the current directory to Documents
$ cd Documents
$ pwd
cd
cd stands for Change Directory, as you can see above it is used to change the current location in the tree structure. You can see your working location as a pointer to a node in that tree. cd will move the pointer either deeper in the tree or shallower.
In this picture, you can see the tree structure. To go down use cd with the path to the node wanted.
In the picture, the pointer represents the working directory
$ cd Downloads
To go back to previous directories ( or go up, if you see it as a tree ), use ..
$ cd ..
.. means the previous directory so to go two directories back use ../.. ( use as many .. as you want to go back. Separate the dot grouping by pair with slashes )
If you enter cd alone will head you to your home directory.
ls
The command ls is used to list files, directories, and pieces of information. The basic command is ls, it takes an optional path as a parameter but if you omit it, it will take the working directory.
$ ls
It will show you what your working directory contains.
As you can see folders are highlighted, and on my terminal the colour is blue. ( The colour depend on your configuration as you can modify it ( see .bashrc file )
What about hidden files and directories? Hidden files and directories start with a dot. To list everything use the command with -a as a parameter.
$ ls -a
Sometimes you want to see the user right on files and directories, either to be able to execute a file or access a folder. This can be useful for example if you try to execute a file but you don't understand why you can't and it says that you do not have permissions.
$ ls -l
mkdir
This command ( make directory ) is used to create a directory into the working directory at the location given in the parameter. As we learned about ls, we will make use of it right now ( see it is useful ^^ )
$ mkdir test
As you can see the first ls command has printed nothing, and the second printed the new folder created with mkdir.
rmdir
rmdir, which stands for Remove Directory, is used to delete an empty directory. This is important otherwise it will not be deleted and print an error message.
$ rmdir folder
You can see on the picture above that there is a directory named aFolder which is empty, you can see it by running the ls command inside of it. rmdir removed that folder.
But what if I have a folder that is not empty I want to remove? For that case, you will need to delete all the files first then come back to the console and run this command for every folder. ( of course not ;) ). There is a command for this and it is the next command.
rm
rm is the command that can remove everything, yes everything, that is why this command is dangerous if misused. You could remove Linux files that are used to run your OS. So be sure to know what is inside your folder before using it.
The basic form is for removing files.
$ rm filename
This will remove a file. To remove a folder you will need to use a parameter that will apply the rm command recursively, the -r parameter.
$ rm -r folder
touch
touch is a command that you can use to create files. If you run the documentation on touch command, you will see that its main purpose is different and that the creation of a file is like a consequence of its main feature. But I only use this command to create my files, so that is what I will show you. You can check the documentation of course.
$ touch index.js
cp
cp stands for copy. It means that it will copy a file into a location specified with a path.
Remember there is a relative and absolute path. Relative means that the path starts from your working directory ( /folder1/folder2 ) and the absolute path start with ~/ which means your home directory ( ~/Documents will refer to /home/USERNAME/Documents )
$ cp file ../anotherFolder/newName
You can set the same name or create a new name. And what if you want to rename your file or just move it?
mv
mv stands for move. It will move a file or directory or if you put the same path ( which means it does not move ) then it is rename.
$ mv file ../anotherFolder/file
$ mv file changedFile
There are two commands left. Both do the same, they print the content of a file to the terminal, but with a difference.
cat
cat is the command to print the whole file content to the console.
$ cat file.txt
less
less is the command that lets you enter in a read mode and will show you only the start of a file. Then you can use keys like arrows to go down or up.
$ less file.txt
To exit the reading mode press q.
So what is the difference? You want a piece of information in a file that is 10 000 lines. If you use cat it will print everything on the terminal and overload it which will take time. Less will only load chuck after chuck.
Of course, you can do much more than that, you can redirect the output and input or search for a term and so on.
Conclusion
Those are the basic commands I use most of the time on Linux. They are good to know and will allow you to interact and do most of your tasks on your machine only using a terminal. I hope you enjoyed this post. If something is wrong or unclear let me know, any feedback would be great.