The Linux Terminal

Nowadays, despite the frenetic evolution faced by graphical interfaces, the Terminal is still a very important tools on whichever Operative System we are working on. Mastering the usage of Terminal simplifies our life and shortens waiting times.

There are cases in which is not possible to use the graphical interface, nor the Remote Desktop services. Here become mandatory the abilities to use the terminal to send commands remotely. The usage of SSH is a good example.

Why this list of notes?

All administrators use the Terminal command history. How many times did you spend a couple of seconds by pressing the arrow-up on your keyboard searching for the command that you already wrote in the past and that resolve the operation that you have in your mind?

Often this useful history is not sufficient to fill our lack of memory. Along the years I have noted down most useful commands that help me in the execution of my IT technician mansions.

In this articles I will write about most useful commands used on the Linux terminal.In questo articolo parlerò dei comandi più frequenti utilizzati su terminali Linux, most of those commands can be also executed on the MacOS terminal by Apple.


Before starting

A little note before proceeding with the basic commands:

For all the commands explained, when I am working on a folder I can use relative or absolute paths. If I have to act on a file in the same folder currently displayed by my terminal, I can omit the whole path and specify only the name of the file. Let’s imagine we are in the /home/teodoro folder and want to create a test.txt file.

teodoro@teodoro-laptop:/opt$ touch test.txt

If I am in the user’s home folder or in other folders different from the one used in the previous example (/opt) I can use the absolute path to create the file:

teodoro@teodoro-laptop:~$ touch /opt/test.txt

Most frequent commands

Divided by category, I recommend you learn these commands.


File management

Important because we will have to manage files for which the user that we use to log-in to our system does not have the permissions to modify. In this case, knowing which commands to use on the terminal is the fastest method.

Alternatively a much raw solution could be to start our file manager from the terminal with administrator permissions.
As an example, on Ubuntu:

sudo nautilus

But I would not suggest such methodology, so let’s start learning something new.

Create a file

Often I have had to create configuration files working on a remote system, many editors accept as a parameter a file that does not exist and create it when saving. Alternatively I often use the following:

touch filename.txt

That command will create a file with name filename and extension .txt .

Copy a file

For example in the configuration of Apache VirtualHosts I often copy a source file in a second with a different name. By proceeding that way I will find the correct file contents without having to search all the parameters every time. The source file will remain unchanged.

cp /original/path/original_file.ext /destination/path/copied_file.ext

If I don’t wan’t to rename file while copying it I have to specify only the destination folder as the second parameter. Remember to put the / (slash) after the name of the last folder:

cp /original/path/original_file.ext /destination/path/

Move a file

Lo spostamento di un file equivale al comando taglia delle nostre interfacce grafiche. Ci troveremo quindi vedere scomparire il file_originale.est e lo vedremo comparire nel nuovo percorso e con il nuovo nome file_spostato.est.

mv /original/path/original_file.ext /destination/path/copied_file.ext

If I don’t wan’t to rename file while moving it I have to specify only the destination folder as the second parameter. Remember to put the / (slash) after the name of the last folder:

mv /original/path/original_file.ext /destination/path/

I often use that command on old Mac computers where the Finder doesn’t support the Cut command.

Rename a file

The same command that we used to move a file can be used to rename it:

mv /path/original_file.ext /path/copied_file.ext

Delete a file

rm is the command used to remove a file. You have to specify the file name as parameter of the command rm and your file will be deleted.

rm /path/file_name.ext

Folders management

When managing folders, some command are common to file managements adapted with special parameters, other commands are specific. Let’s check how they works.

Change your working directory

cd command means Change Directory, it is used to move inside our folders tree.

If you want to move to the folder /destination/path you have to digit cd and the destination path as parameter:

cd /destination/path

Create a new directory

mkdir command is the one used to create a folder inside the current directory or inside a specified path. As explained at the beginning I can use a relative or an absolute path. Let’s make an example using an absolute path:

mkdir /path/to/new_folder

It will create a new_folder under the path /path/to.

Copy a folder

Same command used for files, but we must remember to add the -r parameter to it. The -r parameter means recursively. So let’s see how cp works in that case:

cp -r /path/to/original_folder /destination/path/to/copied_folder

Same usage we saw for files, if you don’t want to rename the folder you can specify the destination folder and end the path with / (slash):

cp -r /path/to/original_folder /destination/path/to/

In the previous example I will copy the folder orignal_folder within the path /destination/path/to. The result will be the folder orignal_folder copied here: /destination/path/to/orignal_folder.

Move a folder

mv command used for folder has exactly the same usage as for files. No additional parameters are required.

mv /path/to/original_folder /destination/path/to/moved_folder

Or, if you don’t want to rename the original folder while moving it, just add the / (slash) at the end of the path:

mv /path/to/original_folder /destination/path/to/

Creando così la cartella /destination/path/to/original_folder while deleting the folder /path/to/original_folder.

Deleting a folder

The delete command for folder is again the same that we saw for files, we just have to add the -r parameter: rm -r

rm -r /path/to/folder_to_be_deleted

Run an application

In order to run an application the Terminal offer us two main methods:

source

source was the first command implemented to run a program or a script under Unix systems. Its syntax is very simple:

source /path/to/executable

. (dot)

Since all programmers try to simplify as much as possible our lives, time after times the command source have been shortened with the command “.“. It can be used with the same syntax of source:

. /path/to/executable

Example:

The example below show how to create a file test.sh with inside one line of instruction: echo “Ciao Teodoro!”. Once created we can execute it using both methods explained before.


Manage permissions

A key part of using a Linux Kernel-based operating system is the permission management. My initial idea was to write an excerpt of the permission management logic and the basic commands here below. While writing, I went through several fundaments that I couldn’t ignore, so I decided to move it in a dedicated article: Linux – Understanding the permission management system.


Help!

Sometimes can happen that we forgot parameter needed by a certain command, or we remember the command, but we don’t remember exactly tis purpose. In that case we must always refer to the parameter –help.

Let’s think about the case where we have in our mind the command cd, but we can’t remember the purpose or the usage. Let’s digit the following:

cd --help

We will see the following output:


Useful tools

We have a couple of additional tools that we must use to accomplish our daily work.

sudo

sudo help us to obtain temporally administration rights. It means Super User DO.

It is installed as an application and it is an optional module on some distributions. As an example on Debian the sudo command is not present by default, if you want to use it you have to install and configure it. I would suggest reading the permissions article for more details about that command.

apt

Packages manager available both under most of Linux distributions and MacOS. Using that command you can install application, services and dependencies required during the development of our applications and during the configurations of our systems.

vi

vi is a Text Editor that you can use from command line. You must know how to work with it while working on file and configuration remotely.

ssh

ssh is also known as Secure Shell and you can use it to access remotely to a system using the shell. Using SSH you can write commands on the SSH shell displayed on your screen and execute them on a remote system.

Conclusions

After reading this article you must have learnt all basics command to start using your terminal. If you have any doubts don’t hesitate to contact me.