Basic Linux commands
In this page you will find a selection of Linux commands briefly explained, whose knowledge is almost essential for a minimal managing of files and directories.
However, users are encouraged to inform themselves in greater detail, for an optimal use of the provided resources, of the way Linux and its commands work. For this there is a vast amount of information available in the Internet, easily accessible and findable.
Information commands
- man
This command allows us to ibtain information (the MANual) on the rest of the existing commands. It is therefore a command which everyone needs to know. If we, for instance, want to understand how the pwd
command works (which we will anyway comment it below), we have run
man pwd
While its function is simple and basic, very often the information displayed is not digestible for the recently innitiated Linux users, which is why we will attempt to soften the transition with a brief explanation of several important commands in what follows.
It displays us the directory where we are currently placed.
At first glance it seems like very unimportant command, however when we're working it is very common having more than one shell open, and it is sometimes confusing knowing which directory we are in, in each case.
Probably the most used command in Linux. It allows us to list the content of a directory, or the properties of a specific file. If we execute it as is, the content of the directory where we are currently placed will be displayed.
The two most frequently used option are probably -l
and -a
:
-
The option
-l
not only does display the information thatls
provides us, but it does it in extended format, such that we can also see properties of the listed files. - The option
-a
shows us all files (regular files, directories, etc), included those whose name begins with the character ".", that is, the ones considered hidden for Linux.
These options can be combined altogether, therefore we can execute
ls -la
or instead
ls -al
for they can be executed in whichever order we please.
One of the entries displayed by this command (with the -l
option at least) could be
-rw-r--r-- 1 alumn chemistry 19960 oct 29 12:31 total
Notice that "total" is a regular file. This we know because the first character obtained is a dash (-). If the entry was a directory, in the first position we would find the character (d) instead of (-).
Followed by this dash we have three trios, that is, nine characters, indicating the permissions on this file.
In the first trio we have
rw-
This means that the owner of the file can read (r), modify or write (w), but not execute (-) the file. If this file could be executed there would be an (x) instead of (-).
The second trio is:
r--
meaning that the members of the group to which the file belongs to can read this file (r), although not modify (-) it nor execute it (-).
Lastly, in the third trio we have:
r--
whose meaning is analog to the ones we have already discussed before, but applied in this case to the system users not being the owner nor members of the file's group. In this case, as in the previous one, these users can only read it.
Going on with the file's description, we have the owner and the group of the file in the third and fourth columns, respectively.
In the fifth column we have the size of the file, and in the sixth, seventh and octave we find the creation/modification date (month day hour).
Directory-related commands
- cd
- If we execute
cd
with no other arguments, we will be positioned in the default directory, that is, our "home" directory. - In order to place us in our current directory, if that was useful for some reason, we will execute:
cd .
- To place ourselves in our "parent" directory,
cd ..
- To access the last directory we visited, i.e. if we have executed
cd directory1
followed bycd directory2
, if we executecd -
we will go back to directory1.
- mkdir
It changes the current directory. It will place us in whichever directory we specify after the command.
There are several directories with shortcut:
This command creates a directory, whose name we feed as an argument.
mkdir newdir
creates the directory called "newdir".
We can also use a more ellaborate sintax, for example
mkdir first/second/third/newdir
such that, if the directories "first", "second", "third" exist, the directory is created in the latter one.
If said directories were non-existent, the option -p
will create them,
mkdir -p primero/segundo/tercero/nuevo
This command erases a directory, but only if it is empty:
rmdir emptydir
erases the directory called "emptydir".
File managing commands
- touch
- tail
This command displays the last ten lines of a file.
We can specify a different number of lines to be shown, for instance
tail -3 thesis
so that we are shown just the last three lines of "thesis".
We can also execute
tail -f messages.log
such that we will be shown the last lines of the file "messages.log" as they are generated, that is, if the file keeps growing we will see the lines as they are added.
This command displays the first ten lines of a file. We can, as in the case of tail
, specify the number of lines displayed:
Muestra las diez primeras líneas de un fichero. También podemos especificar el número de líneas que queremos que muestre, por ejemplo:
head -1 thesis
so that we are shown just the last three lines of thesis.
This command changes file's permissions in Linux.
While the best way to use it is by means of octal notation, we will first explain how can we change the permissions in an initially simpler (although more cumbersome once the octal notation is understood) way.
Remember that the permissions on a given file (understood as either regular files, directories, symbolic links, devices, etc) can be represented with the three trios available when executing ls -l
.
The first trio references the permissions of the owner, u.
The second trio references the permissions of the group, g.
The third trio references the permissions of the rest, o.
Using this we can add, for instance, reading permissions to a "test" file in the "others" trio typing:
chmod o+r test
We can also make a full change adding options for each of the trios, like
chmod u+x,g+w,o+wx test
We can further add a permission to all the trios with
chmod a+x prueba
were we have added execution permission to all, "a".
If we wanted to remove a specific permission, we would substitute the + character for -.
The idea is that this way of changing permissions is very intuitive, however it has some disadvantages as there is a lot to write, increasing the probability of making a mistake. At the same time, we also need to know the initial permissions of the file in order to assign or remove permissions.
To avoid these problems, we can use octal notation (which is just binary notation in disguise), by asssigning the values 4, 2, 1 to the permssions r, w, x, respectively
r ⇒ 4
w ⇒ 2
x ⇒ 1
This way, if we say that we're giving permission 5 to the owner trio, what it means is that we are giving reading (r ⇒ 4) + execution (x ⇒ 1) permissions, as any other combination will never yield 5.
When it comes to give permissions with this notation, we just need to indicate the three trios, that is
chmod 762 test
such that we've given reading, modification and execution permission (4 + 2 + 1 = 7) to the owner, reading and modification (4 + 2 + 0 = 6) to the group and modification permission (0 + 2 + 0 = 2) to the rest of users.
Notice that this numerical combination can never give a different result, therefore permissions will necessarily be left as explained, independently of the original permissions.
The user allowed to change the permissions is its owner. This security measure can not be overridden by any user but root, who can change the permissions of any file in the system.
This command erases a file (or directory). We will just need to execute this command followed by the name of the file to be erased, for instance
rm file1
will erase "file1".
Bear in mind that upon executing this command we will not be able to recover the erased information, there is NO recycle bin associated with this command.
The most common options of rm
are
- The option
-f
forces the deletion. Activating it will not require confirmation after the execution of the command. - The option
-i
asks if we are sure about erasing the indicated files. This is the default option. - The option
-r
erases recursively. If we want to erase a directory and its content, we will use this option such that first its content is erased, then the directory itself.
Compression/decompression commands
- gzip
In order to use it we just need to run
gzip file1
and we will obtain as a result the file "file.gz", which in principle will occupy less space than the original one.
If we use gzip to compress files which are alredy compressed, like .bz2, .Z, .jpg, .mp3, etc, the compression ratio will be a bit meaningless, or even obtaining bigger files than the original ones.
During the compression, there will be two files in the working directory; one is the original one and the other one is the file being generated via compression.
At the end gzip deletes the original as to leave just the resulting compressed file.
This command decompresses files which have already been compressed with gzip
The sintax is
gunzip file.gz
This command is analog to gzip, but with a higher compression rate.
Usually gzip is kept in operative sytems for compatibility reasons (UNIX does not support bzip2), though in general bzip2 is the command used in GNU/Linux system to carry out file compression.
Its usage is therefore
bzip2 file
and, as we said when discussing gzip, during the compression, the original file and the file being generated will coexist until the end of the process, when the bzip2 command deletes the original file, leaving just the compressed one.
And as well, it is not convenient to compress files already compressed.
This command, analogous to gunzip, decompresses .bzip2 files:
bunzip file.bz2