Basic Linux commands

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.

  • pwd

    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.

  • ls

    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 with ls are probably -l and -a:

  • The option -l not only does display the information that ls 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 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

    It changes the current directory. It will place us in whichever directory we specify after the command.

    There are several directories with shortcut:

    • To go to the default directory, that is, our "home" directory, we will use:
      cd ~
    • To go to the directory we are in, if that makes any sense, we will use:
      cd .
    • To access the directory we were in previously, we will use:
      cd ..
    • To access the directory we were in previously, we will use:
      cd -
  • mkdir

    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

    mkdir -p first/second/third/newdir

    will create them.

  • rmdir

    This command erases a directory, but only if it is empty:

    rmdir emptydir

    erases the directory called "emptydir"..

File managing commands

  • touch

    This command changes the date of a file to the current one. If the file does not exist, it creates it with the current date, and zero size.

  • 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 -1 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.

  • chmod

    This command changes file's permissions in Linux.

    Permissions can be represented thanks to the three sets of three that we obtain 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)

    Examples:

    chmod o+r test

    Add read permission to "test".

    chmod u+x,g+w,o+wx test

    Change full permissions for each trio.

    chmod a+x test

    Adds execution permission to all triplets ("a" = all).

    Octal notation can also be used:

    chmod 762 test

    Permissions: user=7 (rwx), group=6 (rw-), others=2 (-w-).

    Only the file owner (or root) can change the permissions.

  • rm

    Delete a file or directory.

    rm file1

    It will delete the file "file1".

    Common options:

    • -f: force deletion.
    • -i: ask before deleting (default).
    • -r: recursively deletes directories and their contents.

    Caution should be exercised, as files deleted with rm cannot be recovered.

Compression/decompression commands

  • gzip

    This command allows us to compress any file.

    In order to use it we just need to run:

    gzip file

    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.

  • gunzip

    This command decompresses files which have already been compressed with gzip

    Sintax:

    gunzip file.gz
  • bzip2

    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:

    bzip2 file. Its usage is therefore

    During compression, the original and compressed files coexist, and at the end bzip2 deletes the original.

    There is no point in compressing files that are already compressed (.Z, .gz, .mp3, .jpg, etc.).

  • bunzip2

    Unzip files previously compressed with bzip2.

    Example of use:

    bunzip2 file.bz2