Posts Tagged ‘linux’

To change host name in Linux from command line, type:

vi /etc/sysconfig/network

Now, you will see a file such as the one shown below:


To change the hostname , you can change the highlighted to any thing you want. Press ‘i‘ or INSERT to edit the file. After making necessary changes, you can save the file by pressing ESC followed by :wq and hit ENTER.

Now, restart to make necessary changes. You can reboot by typing the following in the command line:


After reboot, login and check your hostname by typing the following in the terminal / command line :



Hope this helps!  🙂


  • Creating a .tar archive:

In Linux, we can create an archive file using the tar program and use gzip to compress the archive. This is similar to the WinZip program in Windows where you can compress multiple files or directories into a compressed .zip archive.

For example, first just create a directory called “testdir

# mkdir testdir

Now, change your working directory to testdir and create files “test1.txt”, “test2.txt”, “test3.txt“.

            # cd testdir

            # touch test1.txt

            # touch test2.txt

            # touch test3.txt

Now, create another directory called “testdir2

            # mkdir testdir2

Now, change your working directory to testdir2 and create files “test4.txt”, “test5.txt”, “test6.txt“.

            # cd testdir2

            # touch test4.txt

            # touch test5.txt

            # touch test6.txt

Now, go back your initial directory

            # cd ../..

To see the list of the created directories and files:

            # tree testdir

Now, to create an tar archive using the “c” (create) and “f” (file) arguments with the tar command as follows:

            # tar cf testdir.tar testdir

Now, to seeif the archive was created. You can type the following command:

            # ls -l *.tar

If you want to see if all the files are inside the archive, or if you want to see the contents of the archive, you use the following command:         

            # tar tf testdir.tar

If you want to see the progress in the archiving process, you can add the “v” argument. For example, the command would then would be:

            # tar cvf testdir.tar testdir

You can also make an tar archive in “Interactive Mode” to select which files needed to be added to the archive and which ones you need 2 exclude. This can be done by using the ‘w‘ argument . You can add a file by typing ‘y’ when prompted and ‘n’ for the file you do not need to add to the archive.

For example, if you wanted to exclude files “test2.txt” and “test4.txt from the archive, all you have to do is , use the following command and type ‘n’ when  prompted for “test2.txt” and “test4.txt” and type “y” for the rest of the files.

            # tar cwf testdir.tar testdir        

  • Extracting a .tar archive:

To extract a .tar archive, you can use tar with ‘-x‘ (for extracting) and “-f” and “-v“(to see what is happening or progress of extraction) arguments.

            # tar xvf testdir.tar

To select which files to be extracted you need to use the ‘w’ argument. For example:

            # tar xvwf testdir.tar

Now, if you want to just want to extract a single file from the archive, for example, “testdir/testdir2/test4.txt”, you can use the following command:

            # tar xvf testdir.tar testdir/testdir2/test4.txt

  • Compressing Files using gzip:

We can compress a .tar archive using gzip and the compressed file will be either a .tgz or .tar.gz file.

For example, if you want to compress “testdir.tar” using gzip, you can do it by using the following command:

            # gzip testdir.tar

The resulting compressed file will be “testdir.tar.gz”. So, the compressed file will be the filename with a “.gz” extension by default.

To see the details of the compressed file, you use the following command:

            # gzip -l testdir.tar.gz

To check if the compressed “.tar.gz” file is proper, you can use the following command:

            # gzip -tv testdir.tar.gz

You can also compress the “.tar” archive to a custom extension using the “-S” argument. For example, if you want to compress the arhive “testdir.tar” to “.gzipped”, by using:      

            # gzip -S .gzipped testdir.tar

  • Decompressing using gzip or gunzip:

We can decompress a compressed file using the “gunzip“. The compressed file should have any of these extensions “.gz”, “.z”, “.Z”, “-z” or “-Z” in order to decompress or it will not work.

For example, if you want to decompress the file “testdir.tar.gz

# gunzip testdir.tar.gz

Another way to decompress is by using the gzip program itself by using the “-d” argument. For example:

            # gzip -d testdir.tar.gz



So, what are run levels? Well, they are like different modes in which you can run Linux and different run levels have different configurations. They define what services or processes must be running on the system after boot. They are different run levels in Linux but you can run only one run level at a time.

For example, you can operate in Run-level 5 which have the graphical interface or in run level 3 which will not have the graphical interface.

In Red hat Linux/Fedora, the run-levels are as shown below:

Run Levels       Details

0                      Halt

1                      Single user mode

2                      Multi-user modes without networking

3                      Multi-user mode without graphical interface

4                      unused or can be user-defined

5                      multi-user mode with Graphical interface as well as console logins

6                      Reboot

  • Changing run-levels:

Run-levels can be changed by editing the /etc/inittab file. First, make a backup of your /etc/inittab file

# cp /etc/inittab /etc/inittab.bak

So, open /etc/inittab in your vi editor or gedit editor.

            # vi /etc/inittab

Now, look for the following line :


Now, you can change the number 5 to any run-level you want. For example, if you want to change the run-level to 3 i.e. to the non-graphical interface, Then just change the change the number 5 to 3.  So, it would be like:


After making the changes, save the file and reboot. Now, your Linux OS will be running in run-level 3. You can press ALT+F2 to see the log-in prompt.

# reboot

  • Alternative Method to change run levels:

You can also change run-levels by typing “init” followed by the “run-level”. For example, if you want to change to run-level 3, in your terminal type:

            # init 3

This will reboot your machine into run-level 3.


There are many ways in which you can get help with commands. One way of getting help with a command is by using the “man” command which provides the manual page.

For example, if you want to know more about how to use the “cp” command, you can read its manual page by using the following command in the terminal:

# man cp

Another way is to use the “–help” command. This will give you a brief outline of the arguments that can used with the command.

For example, if want to find what all options or parameters can be used along with the command “mv” or remove command, you can use it by typing the following:

# mv –help



These are some of the basic Linux commands which can be used in the terminal . To open the terminal, press ALT+F2 and type gnome-terminal and hit ENTER. This will show you the terminal window where you can type in all your commands.

Shows the list of files in the current directory.

pwd is used to show which directory you are in at that time

cd directoryname”  will change current working directory to the inputed directory name.For example, if you want to change to directory “abc”, then you type: “cd abc

cd ..
Change the current directory to the previous directory.

cd ~
Go to the current user directory. For example, if your username is “root”, then it will go the user directory of root.

cat filename
This is used to display the contents of a file.
For example, you want to show the contents of a file named hello.txt , then you use the command “cat hello.txt

Prints the calender of the current month

This will print today’s date

su is used to switch user. You can use it by using the ” su username” .
For example, “su root” will switch current user to root. You will be prompted to enter the password, and after you enter the password you will be switched to user root.

mkdir directoryname
This is used to create a directory . For example : If you want to create a directory named abc, then type “mkdir abc

uname -a
This will print all your system related information like the operating system , kerenel version etc

uname -o
This will print the operating system that is currently in use.

This is used to copy a file to another location. For example you have a file name test1.txt and you want to copy it to /home/documents/ , then you have to type “cp test1.txt /home/documents

cp -R
This is used to copy directories. For example, to copy the folder /home/documents/dir1 to /home/root/ folder,  you use the command “cp -R /home/documents/dir1 /root/documents/

This is used to move a file or folder to another location. For example, if you want to move the folder /home/documents/dir1 to the location /root/, then you use the command “mv /home/documents/dir1 /root/

This command is used to the delete files or directories . For example, to delete the file hello.txt, you use the command “rm hello.txt

vi filename
This is used to create or open  a file in the vi editor. For example, if you want to create a file named “hey.txt” , you use the command “vi hey.txt” . To enter or insert text into the file, press i or INSERT button on your keyboard. After you have entered the text, to save the file and exit , press ESC and type :wq and hit ENTER on your keyboard. This will save your file and exit the editor and will go back to your terminal window.

touch filename
touch is used for creating text files. For example, to create  a text file name hello.txt, you type “touch hello.txt