Basic Linux commands you should know

6 minutes reading
linux and devops

There are many basic linux commands, from those that tell you which is your user, to others that allow you to run and schedule services periodically. Below I will list the commands that I use frequently, as well as any combination or peculiar feature of any of these that is worth mentioning. I will try to do it in the simplest possible way and without trying to complicate too much the use of the commands.

For these examples I use Debian 9, so if some command does not appear, please note that it may be due to differences with your GNU/Linux distribution.

Another thing, what most people know as Linux, plain Linux, is actually GNU/Linux, however for SEO and simplicity reasons many people use GNU/Linux or Linux interchangeably, which I intend to do as well. But keep this in mind when you read this or other entries.

Click here for the second part of this series of commands and here for the third part of this series.

Little tips from the Linux Terminal

Use tab to complete commands

Before starting with the commands I want to mention a couple of small tips or functionalities. The GNU/Linux terminal is user friendly, just type the first letters of a command, file or folder found in the current folder and then press the TAB key and the system will automatically complete the command, file or folder name.

If there is a conflict, just press the TAB key twice and it will show the available options. A perfect function for lazy people who don't like to type unnecessarily.

Press up to access previously executed commands.

While we are in the terminal, if we press the up key of our GNU/Linux pad it will show us the last command we executed. An ideal function for those moments when we repeat the same command over and over again, hoping that, magically, the result will change.

Use CTRL + C to cancel interrupting terminal processes.

If we did something stupid like executing an infinite loop, or we simply want to stop something we are executing, we will use CTRL + C, yes, as if you were going to do a copy paste, but with the terminal open. This will cancel the execution of the command.

Having explained this, let's start with the commands.

man

The man command (MANual) is what I consider the most important of all the common commands, that's why I put it here first. The reason? because this command shows the user manual of the command you put in front of it. With this command you have access to all the documentation of the command you choose. And, consequently, you can learn the basic use of practically any command that has a manual.

Remember that most of the commands listed in this publication have a manual, so you can see all the options they offer using this command.

For now try using it on itself. Yes, we will inception the man command:

man man
 nos muestra el manual del comando man
man grep
 nos muestra el manual del comando grep

whatis

It shows us a small description of the command that we put next. It is the super summarized version of man.

whatis man
man (7)              - macros para formatear páginas del manual
man (1)              - una interfaz de los manuales de referencia electrónicos

grep

The grep command (Global Regular Expression Print) searches for matching regular expressions in the files. If you don't know what regular expressions are you can use it with ordinary text in quotes, even then it is quite useful. This command becomes especially useful when we want to find text in a large number of files, especially using it in combination with the result of other commands.

grep "texto a buscar" archivo
 busca un trozo de texto o expresión regular en un archivo 
grep -r "texto a buscar" .
 busca un trozo de texto recursivamente en los archivos del directorio actual y sus subdirectorios
man man | grep "texto a buscar"
 utilizamos el caracter pipe "|", seguido de grep para buscar un trozo de texto en los resultados del comando man

history

History shows us the commands we have used, numbered, with the most recent to the bottom and the oldest to the top. There is an easier way to search for commands on which you can read here.

history
1920 cat archivo.txt
1921 ls -la

clear

What if we want to give our terminal a little cleanup to have more clarity when writing code? Clear deletes the contents of the terminal and leaves us with a completely clean terminal.

clear

 limpia todo el contenido anterior de la terminal

pwd

From the acronym "Print Working Directory", which means "print the working directory", this command prints the directory from where we are executing it.

pwd
/home/tunombre

ls

When we need to display the files and directories in the current directory this command is the solution. We can use it to see the permissions, users and groups to which a file belongs.

ls
 nos lista los directorios y archivos en el directorio de trabajo
ls -l
 lista los permisos, el tamaño, grupo y usuario al que pertenecer los archivos

ls -la
 lo mismo que arriba pero incluye además los archivos ocultos

file

File gives us information about the type of file that we pass as an argument. It even works for files with the wrong file extension.

file archivo.txt 
archivo.txt: ASCII text

cd

This command is one of the most commonly used. It is used to change directory, it can be read as "change directory", which means "change directory" in English.

cd nombre_de_directorio
 # nos cambia a un directorio existente desde donde ejecutamos el comando

cd ..
 # retrocede un nivel de directorios

cat

This command is used to concatenate the contents of a file and display it on the screen.

There is a tool that offers more functions than cat, check my post where I talk about bat

cat archivo
 # nos mostrará el contenido del archivo

tail

The tail command, which comes from "cola" in English, prints the last ten lines of a file. Ideal for reading the contents of logs.

tail archivo
ultimas diez lineas

head

Similar to the previous one, the head command, which comes from "head" in English, prints the first ten lines of a file.

head archivo
primeras diez lineas

touch

This command creates a file in the directory where we are executing it.

touch archivo.py
 # crea un archivo llamado archivo.py

rm

Derived from the word "ReMove", this command deletes one or more files. It is often used with its '-r' option, which recursively deletes files.

rm archivo_a_eliminar
 elimina un archivo llamado archivo_a_eliminar
rm -rf *
 borra todos los archivos del directorio de trabajo.

Sometimes we want to be sure not to delete a file unintentionally, we can add the -i option to ask us before deleting a file.

rm -i archivo_a_eliminar.txt
 rm: ¿borrar el fichero regular vacío 'archivo_a_eliminar.txt'? (s/n)

Be careful with the rm command

Another important thing, remember when web surfers were tricking people into deleting their system32 folders in windows, rendering their systems unusable? Well there is an equivalent in GNU/Linux, it is the rm command, followed by the -rf option, used to delete recursively and without asking for confirmation and then the slash representing the root folder "/". If you run it with sufficient permissions it will erase your operating system, leaving it unusable.

I warn you again: PLEASE DO NOT EXECUTE IT:

# NO HAGAS ESTO
rm -rf /
# NO HAGAS ESTO

mkdir

This command is used to create directories. It comes from "MaKe DIRectory".

mkdir directorio 
 # crea un directorio llamado directorio

rmdir

"ReMove DIRectory", removes a directory, in order to use this command the directory to be removed must be empty.

rmdir directorio_a_remover 
 # elimina un directorio vacio llamado directorio_a_remover

cp

The cp command is responsible for copying a file to the location you specify.

cp nuestro_archivo.html carpeta_donde_lo_queremos_copiar/

We can also copy directories with all the content. If the second directory does not exist it will create it.

cp -r directorio_origen/ directorio_destino/

ssh

The ssh command allows us to connect to a remote server. To do this we must specify the user with which we will connect and the ip address, both in that order and joined by an at.

ssh [email protected]

It is common practice for servers to change the default port for security reasons. We can specify a port using the -p option.

ssh -p 1234 [email protected]

After executing the command, if the user name and address are correct, it will ask for the user's password to give us access.

scp

This command is also used for copying, but it will copy from our computer to another one by means of ssh. After typing the command it will ask for a password to be able to copy it. For this command to work the target machine must have ssh installed and its port 22 open.

scp archivo_a_copiar.txt [email protected]:/directorio_remoto/subdirectorio

If you want to pass multiple files, place them next to each other.

scp archivo1.txt archivo2.xml archivo3.html [email protected]:/directorio_remoto/subdirectorio

ip addr

This command will show us the connections of our computer, with their network interfaces and IP addresses.

ip
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s31f6: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
    link/ether 1c:1b:0d:64:6b:aa brd ff:ff:ff:ff:ff:ff

The ip command has many options that I suggest you review calmly, use the man command if you want to delve deeper into the functionalities.

As I'm sure you already know, many important commands have been missing, I haven't placed them here so as not to make the reading so tedious, get up, stretch a bit, get something to drink and skip to my second part of the basic GNU Linux commands for the second part.

Related content