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

As you read my tutorials on linux commands I am very sure you feel more confident with yourself while you see a console. I have explained many different things so far, from very basic built-in commands such as ls, cd, pwd, mv to more advanced ones such as pdfimages which helps to deal with images in pdf files.

But the learning never ends. I have been a linux user for more than four years now and I learn a new command every day. Knowing many commands does not only help you to perform different tasks through the console of your machine, but also helps to solve a problem in different ways.

Are you ready to learn some new more commands? If so then continue reading this article. There are so many things you need to learn in your way of becoming a linux ninja.

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

The head command

As the name suggests the head command is used to output the first part of a file to standard output. According to the documentation it prints the first 10 lines of each file to standard output. When no file is specified it reads from standard input. But how to use this command on your console?

The basic usage is very simple. Open a new console on your machine and type the following command.

head filename_in_here

 For example the following command displays the first 10 lines of my test.txt file on standard output.

head test.txt

The output of the above command is shown below.

this is 1
this is 2
this is 3
this is 4
this is 5
this is 6
this is 7
this is 8
this is 9
this is 10

The cmp command

The cmp command can be used to find the difference between two files. It shows the  the offsets and line numbers where two files differ. To make it more practical for you guys while explaining this command I am using my favorite text editor to create the two following files shown below.

names.old names.new

Lets run the cat command which you have learned in previous series and see what each files contains.

cat names.old

The above command prints the following to standard output.

oltjano

Then running the following command will show a different output.

cat names.new

The output of the above command is shown below.

jetbird

As you can see from the results of cat command each one of the files contains only one single line of text. Now running the cmp command will show me the difference between the two files. You can easily tell it yourself, but what if you have files with million lines of text?

cmp names.new names.old

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

names.new names.old differ: byte 1, line 1

As you can see from the output shown above the cmp command tells exactly where the two files differ. It shows the difference in a very clear way for the user to understand. In short words it tells the first byte and line number where they differ.

The hostname command

The hostname command can be used to show the system’s hostname. To use it just follow the very simple syntax which is shown in the example below.

hostname

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

baby

The logname command

The logname command prints the user’s login name. Run it as shown below.

logname

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

oltjano

The users command

If you would like to print the names of the users currently logged in then type the following command on your console.

users

The units command

The units command becomes very useful when you want to convert units from one scale to another. The units tool can be used interactively with prompts.

This tool does not come as a builtin on linux you have to install it . Ubuntu user can use the apt package manager to install it like shown below.

sudo apt-get install units

Once you have installed units on your machine run the following example.

units '2 kilometers' 'meters'

What output do you get? Is your output the same as mine?

 * 2000
 / 0.0005

The ping command

The ping command is used to test a network connection. It sends ICMP ECHO_REQUEST to network hosts. Use the following syntax to ping a host.

ping host_here

For example the following command is used to ping google.com

ping www.google.com

And the response of the ping is shown below.

PING www.google.com (74.125.29.104) 56(84) bytes of data.
64 bytes from qg-in-f104.1e100.net (74.125.29.104): icmp_req=1 ttl=42 time=346 ms
64 bytes from qg-in-f104.1e100.net (74.125.29.104): icmp_req=2 ttl=42 time=265 ms
64 bytes from qg-in-f104.1e100.net (74.125.29.104): icmp_req=3 ttl=42 time=289 ms
64 bytes from qg-in-f104.1e100.net (74.125.29.104): icmp_req=4 ttl=42 time=313 ms
64 bytes from qg-in-f104.1e100.net (74.125.29.104): icmp_req=5 ttl=42 time=336 ms
64 bytes from qg-in-f104.1e100.net (74.125.29.104): icmp_req=6 ttl=42 time=182 ms

Use CTRL+C to stop the ping.

The dirs command

The dirs command is used to display a list of remembered directories. Run it like shown in the following example.

dirs

The du command

The du command is used to estimate file space usage. Run the following command on your console and see what happens.

du

The kill command

The kill command is used to send a signal to a process. It is very useful when you want to kill a process that you no longer need. The signals listed below may be available for use with kill.  When known constant, numbers and default  behavior are shown.

 Name Num Action Description
 0 0 n/a exit code indicates if a signal may be sent
 ALRM 14 exit
 HUP 1 exit
 INT 2 exit
 KILL 9 exit cannot be blocked
 PIPE 13 exit
 POLL exit
 PROF exit
 TERM 15 exit
 USR1 exit
 USR2 exit
 VTALRM exit
 STKFLT exit might not be implemented
 PWR ignore might exit on some systems
 WINCH ignore
 CHLD ignore
 URG ignore
 TSTP stop might interact with the shell
 TTIN stop might interact with the shell

To kill a process use the following syntax.

kill PID

You can use as many PIDs as you want in a single command.

The following is an example of using the kill command.

kill 130

The above command is going to kill the process with the PID specified above. In my case it is not going to work as I do not have such a process running on my machine.

The following is displayed on my console when running the above command.

bash: kill: (130) - No such process

As you can see bash tells you that is no such process running on your machine. You can display a list of processes by using the following command.

ps aux

Then you can use piping to grep the process you want.

Or you can do the following.

ps aux | less

The  locate command

The locate command is used to find files by name. This command prints matching files to standard output. According to the documentation in the manual pages locate reads one or more databases prepared by updatedb(8) and writes file names matching at least one of the PATTERNs to standard output, one per line.

locate test.txt

What should you know so far

Reading this series of linux commands has been a long process, but have you ever asked yourself what have you learned so far? In my opinion you should know the following:

  • how to list files using the ls command
  • how to navigate through directories using the cd command
  • how to print the current working directory using the pwd command
  • how to display the current date on your console
  • how to copy files with the cp command
  • how to remove files with the rm command
  • how to kill a process with the kill command
  • how to find information about your computer with the uname command

Conclusion

There are so many commands on your head right now and I know it sounds very hard for you to know how to use all of them. Many of you who are new to linux will feel very confused. I felt the same way when I started learning linux commands. But when I look back I see the progress I have done.

I am not a linux ninja, but I know how to do basic stuff. Keep learning.