Useful commands

In this chapter, we will learn about a few more commands which we may have to use in daily life.

Extracting a tar file

tar is a tool to create and extract archive files. Many times we
will have to download and then extract tar files in our regular day to day work.
$ tar -xzvf files.tar.gz

files.tar.gz file is compressed with gzip, if the file name ends with .tar.bz2, then it is compressed wth bzip2.

$ tar -xjvf files.tar.bz2

Creating a tar file

We can use the same tar command to create a tar file.

$ tar -czvf files.tar.gz hello.c bye.txt
$ ls
bye.txt  files.tar.gz  hello.c

Vim editor

Text editors are tools to edit files. This could be a configuration file, or source code, or an email, or any other kind of text file. Which editor to use, is generally a personal choice, and a lot of good energy has been wasted in the telling of which one, is the one, truem best editor. In this book we will just learn about Vim editor. It’s also known as vi improved editor. In the Fedora Linux distribution, the vi command is actually an alias to vim itself.

If we just type vim, and press enter, we will see the following screen.


:q to exit vim

Press Escape and then type :q to exit vim.


Open a new file or edit an existing file

vim filename is the command to open an existing file. If the file does not exist, it will open a new, empty file for editing.

Different modes of vim

Vim editor starts off in command mode. Every time you open a file, this is the default mode of the editor. You can press the Escape key in any other mode to come back to command mode.

You press i to go into insert mode; we edit documents in the insert mode. If you press Escape, you will return to command mode.


:w to save a file

In command mode, typing :w saves a file. If you want to save and quit the editor, then type either :wq or :x.

:q! to quit without saving

Typing :q!, when you are in command mode, will allow us quit without saving the current file.

Vim is a powerful editor, and we learned only a few, really basic steps in it. It will take a complete book, to explain different features of vim. But, the steps above are sufficient for our book’s scope.

One major thing to remember about any text file, is keeping the newline character as the last line of the file. Because that is how the ` POSIX <>`_ standard defines a line.

Becoming root user

root is the superuser. root has the power to make changes in various parts of a Linux system. That also means if you make any dangerous change (say deleting your user account) as root (by mistake), that can easily cause real damage.

The general rule is, when you need superuser power, use the sudo command to get work done, and use your normal user account for everything else. The su - command will helps you become the root user; use this extremely carefully.

$ su -

Notice how the command prompt changed to # from $, # shows that you are using the root — another visible indication to think about every command you give as root. Press Ctrl+d to log out of the root account. (or any account, for that matter)

Using sudo command

Add the sudo command in front of any other command to execute them as root. For example:

$ less /var/log/secure
/var/log/secure: Permission denied
$ sudo less /var/log/secure
[sudo] password for fedora:
... long output

Environment variables

Environment variables are a way to pass data on to applications. We can set values of different variables, which any application can then access. There are various variables which decide how the shell will behave. To see all the variables, use the printenv command.

$ printenv
... long output

You can execute the same command once as normal user, and once as root, and then check for the differences between the output. You will mostly see they are same, with some (or more) unique ones. That’s because, variables are user specific.

Setting up environment variable values

We can use the export command to create a new environment variable or change an existing one. We use the echo command to print a particular environment variable’s value.

$ export NAME="Kushal Das"
$ echo $NAME
Kushal Das
$ export NAME="Babai Das"
$ echo $NAME
Babai Das

In our example we first created a new variable called name, and then we changed the value of the variable.

locate command

locate is a very useful tool to find files in the system. It’s part of the mlocate package. For example, the following command will search all the files with firewalld in the name.

$ locate firewalld
... long output

You can update the search database by using updatedb command as root.

$ sudo updatedb

This may take some time as it will index all the files in your computer.

Finding date/time in different timezones

The /usr/share/zoneinfo directory contains all the different timezone files. We can use these file names to get current date/time in any timezone. For example, the following command will show the current date/time in US/Pacific timezone.

$ TZ=US/Pacific date
Sun May 20 18:45:54 PDT 2018