Some Basic Linux Commands I Have Used During My Linux Journey – Part 6

There are some other commands you need to know about in order to get better at the commandline. In this tutorial we will take a look at some practical linux commands I have learned while doing different tasks on my machine.

For example what command would you use if someones asked to extract images from a pdf file? I am sure you don’t know the solution unless you have done this before.

You might want to read our previous parts of this series.

The pdfimages command

To extract images from .pdf files I use a tool which is called pdfimages. Ubuntu and Debian users can install it on their system by using the following command.

sudo apt-get install poppler-utils

Then before extracting any images from a pdf file you need to learn how to use the tool. The following are some  important things you need to know before using the tool:

  • The name of the document you want to extract images from
  • The starting page (specify as integer)
  • The end page (specify as integer)
  • The -j option (Save jpeg images as jpeg)

For example if I have unixmen.pdf and want to extract all images from it I use the following command.

pdfimages -j unixmen.pdf unixmen

The -j option is being used to save images as jpeg. The unixmen at the end of the command is being used to name the images. Every image will start with unixmen. Of course you can put any name you want. It is always up to you.

The tac command

The tac command is similar to ‘cat’ but instead of displaying the file in standard output as it is it will display the last line first. The following example explains it and makes clear for you.

The test.txt is a file on my Desktop. Running the following command displays the file in standard output.

cat test.txt

The file test.txt is displayed like shown below when running the above command.

this is a test
last line

Then using tac shows the difference.

tac test.txt

The last line in text.txt is displayed first when running the above command like shown below.

this is a test
last line

The diff command

The diff command is used to find differences between two files. I use this command when I want to display differences between two files line by line. How to use this command?

The general syntax of diff command as follows:

diff file1 file2

Create two files test1.txt and test2.txt.

Make test1.txt look like shown below.

This is the first file

Make test2.txt look like shown below.

This is the second file

Then run the following command.

diff test1.txt test2.txt

What output does the above command display? In our case it displays the following.

1c1
< this is the first file
---
> this is the second file

The dirname command

The dirname command is used to convert a full pathname to just a path. In other words it can be used to strip last component from file name.

For example run the following command on your console.

 dirname /usr/bin

The above command gives the following output.

/usr

The sleep command

The sleep command is used to delay for a specified amount of time. The syntax of this command is as following.

 sleep NUMBER[SUFFIX]...
 sleep OPTION

Try the following command and see what happens.

sleep 13

The tail command

The tail command can be used to display the last part of files. According to the manual pages this command prints the last ten lines of each file on standard output. Lets try the following example.

tail test1.txt

The above command prints the following on standard output.

this is the first file

Then try the following command.

tail test1.txt test2.txt

The above command gives a very nice output which is shown below.

==> test1.txt <==
this is the first file
==> test2.txt <==
this is the second file

As you can see from the above output when more than one file is being used the tail command precede each with a header giving the file name.

It is a very nice and easy to understand output format, isn’t it?

The who command

The who command is used to show logged on users on the system. The usage is really simple and the following example demonstrates it.

who

Running the above command on your console will show who is logged on. When running the above command on my console I get the following output.

oltjano tty7 2015-06-09 19:14
oltjano pts/5 2015-06-16 13:58 (:0.0)
oltjano pts/9 2015-06-16 19:43 (:0.0)

The wc  command

The we command can be used to print number of bytes, numbers of words and line count in each given file. For example running the following command will print byte, word, and line count on my standard output for the test1.txt file.

wc test1.txt

Running the above command displays the following output.

 1  5 23 test1.txt

There are also some useful options you can use with the wc command. What if you want to print only the number of words of your file and not other stuff such as line count and bytes?

The -w option is the solution to this problem. Run the following command.

wc -w test1.txt

You can use any file you want. I am using test1.txt for this case. The -w option in the above command tells the wc that we want to count and print only the number of words on our system.

Running the above command on my console gives me the following output.

5 test1.txt

I get the number of words in the test1.txt file. As you can see from the above output the number of words in test1.txt is five. But what if you want to print the number of lines?

Just make use of the -l option like shown below.

wc -l test1.txt

And I get the following output.

1 test1.txt

There is only one line in my file so the output is totally correct. You can also print only the byte counts by using the -c option like shown in the following command.

wc -c test1.txt

The above command prints the following on my standard output.

23 test1.txt

The type command

The type command is used to describe a command. In other words it finds out if a command is builtin or external binary file. Run the following command.

type pwd

What does it print to the standard output? Running the above command on my console I get the following output.

pwd is a shell builtin

And run again the type command like shown below.

type cd

A different output is going to be displayed now. I get the following.

cd () 
{ 
 autoenv_cd "$@"
}

If you want to find out all information about a command then use the -a option when making use of the type command. The following example shows you how to do it.

type -a pwd

When running the above command on my console I get the following output.

pwd is a shell builtin
pwd is /bin/pwd

You can also use the -t option with the type command. If the -t option is used, it will print a single word which is one of the following

  • alias (command is shell alias)
  • keyword (command is shell reserved word)
  • function (command is shell function)
  • builtin (command is shell builtin)
  • file (command is disk file)

Type the following command on your console. Run it and see what happens.

type -t pwd

I get the following output when running the above command.

builtin

Why don’t you try yourself some other examples where the type command is being used. For example try to make use of the -p option like shown below.

type -p date

Conclusion

As you can see from the commands I have shown to you so far linux offers an amazing builtin set of commands that can do different things such as listing files, finding the difference between two files, printing a working directory, counting the lines a file and other important stuff such as changing permissions of a file.

Keep practising all the commands we have learned so far! I am very sure it pays in the end.