List of Linux Commands Every Developer Should Know

TEN LINUX commands

At some point in you developer career , you will have to work with Linux Systems and you will be looking for information regarding Linux commands. In this post I will explain the use of 10 Linux commands you should know as a developer.

10 Linux Commands For Developers

Following are the  list of ten commands you will learn about in this post

 man
 touch, cat and less
 sort and grep
 cut
 sed
 tar
 find
 diff
 uniq
 chmod

Lets get started.

1. man

The first command you should learn in Linux is “man”. Using this command you can get the usage and description of all  Linux commands. For example, if you want to know about “ls” command and its options, just execute “man ls” command in the terminal to list its usage and description.

Syntax: man 
man ls
[email protected]:~# man ls
LS(1)                            User Commands                           LS(1)
NAME
       ls - list directory contents
SYNOPSIS
       ls [OPTION]... [FILE]...
DESCRIPTION
       List  information  about  the FILEs (the current directory by default).
       Sort entries alphabetically if none of -cftuvSUX nor --sort  is  speciâ
       fied.
       Mandatory  arguments  to  long  options are mandatory for short options
       too.
       -a, --all
              do not ignore entries starting with .

Recommended: Learn Linux in 5 Days and Level Up Your Career Udemy Course

2. touch, cat and less

Touch command is used to create any type of file in Linux systems with “0” size. As a developer , when working with Linux you might want to create files in the server. You can make use of touch command to do that.

Syntax: touch 
touch demo.txt

[email protected]:~# touch demo.txt
[email protected]:~# ls
demo.txt

Cat command is used to view the contents of a file. You cannot edit the contents of the file using cat. It just gives a view of the file. cat doesn’t support scrolling using keyboard.

Note: To work the commands given below, copy any content to the demo.txt file using your favorite vi or nano editor. For our examples, i have copied the boot.log file contents on to demo.txt file using ” cat /var/log/boot.log >> demo.txt” command. You can also do the same.

Syntax: cat 
cat demo.txt

Less command also gives the view of a file. less is very fast and you can use the arrow keys to scroll up and down to know the start and end of the file. There is also “more” command, which is used to view the file but it allows only forward scrolling using “enter” key. It doesn’t support backward scrolling.

Syntax: less 
        more
less demo.txt
more demo.txt

3. sort and grep

Sort is used to sort the contents of a file. Create a file named test.txt and copy the following contents on to the file to test the sort command.

1 mike level intermediate jan
10 lucy level beginer mar
45 Dave level expert dec
4 dennis start beginner jul
7 Megan employee trainee feb
58 Mathew Head CEO nov

In the above example, second column has the names. So if you want to sort the names alphabetically use “-k” flag with the column location. It would be “-k2”.

Syntax: sort
sort -k2 test.txt
[email protected]:~# sort -k2 test.txt
45 Dave level expert dec
4 dennis start beginner jul
10 lucy level beginer mar
58 Mathew Head CEO nov
7 Megan employee trainee feb
1 mike level intermediate jan

The first column has numbers. If you want to sort numbers, use  “-h” flag. If the numbers are in different column, you can use the “-k” flag along with “-h” flag.

[email protected]:~# sort -h test.txt  
1 mike level intermediate jan
4 dennis start beginner jul
7 Megan employee trainee feb
10 lucy level beginer mar
45 Dave level expert dec
58 Mathew Head CEO nov

The last column has months. You can sort a file based on month using “-M” flag.

[email protected]:~# sort -k5 -M test.txt
1 mike level intermediate jan
7 Megan employee trainee feb
10 lucy level beginer mar
4 dennis start beginner jul
58 Mathew Head CEO nov
45 Dave level expert dec

Note: If you want to eliminate the duplicate lines , you can use “-u” flag along with the sort command.

To sort the file in descending order, use “-r” flag.

[email protected]:~# sort -h -r test.txt
58 Mathew Head CEO nov
45 Dave level expert dec
10 lucy level beginer mar
7 Megan employee trainee feb
4 dennis start beginner jul
1 mike level intermediate jan

Grep:

Grep is a powerful command and will be used by sysadmins quite often. Grep command is used for searching specific string patterns in a file as well as the standard output (STDIN). We will look into few file based operations . Other uses of grep is out of scope of this post.

Syntax: grep ""  
        grep "Mathew" test.txt
[email protected]:~# grep "dennis" test.txt
4 dennis start beginner jul

The above command gives the output including the sub-string. If you want to search for individual words, you need to add “-i” flag to the grep command. Also you can search for a string or a pattern in multiple files using a single grep command. For example,

grep "dennis" test1.txt test2.txt test3.txt

You can also use regular expressions for matching the string.

4. cut

Cut command is used for extracting a portion of a file using columns and delimiters. If you want to list everything in a selected column, use the “-c” flag with cut command. For example, lets select the first two columns from our test.txt file.

cut -c1-2 test.txt
[email protected]:~# cut -c1-2 test.txt
1
10
45
4
7
58

If you want to extract specific strings from a file, you can used the delimiter “-d” flag and “-f” flag to select the field. For example, if you wan to extract all the names from our test.txt file you can use the following command.

cut -d' ' -f2 test.txt
[email protected]:~# cut -d' ' -f2 test.txt
mike
lucy
Dave
dennis
Megan
Mathew

The following example extracts the users from /etc/passd file using ‘:” delimiter.

cut -d':' -f1 /etc/passwd

5. sed

sed is a text-editor which can perform editing operations in a non-interactive way. Sed command gets its input from a standard input or a file to perform the editing operation on a file. Sed is a very powerful utility and you can do a lot of file manipulations using sed. I will explain the important operation you might want to do with text file.

I you want to replace a text in a file by searching it in a file, you can use the  sed  command with substitute “s” flag to search for the specific pattern and change it.

Syntax: sed 's///' test.txt

For example, lets replace  “mike” in test.txt file to “michael

sed 's/mike/michael/' test.txt
[email protected]:~# sed 's/mike/michael/' test.txt
1 michael level intermediate jan
10 lucy level beginer mar
45 Dave level expert dec
4 dennis start beginner jul
7 Megan employee trainee feb
58 Mathew Head CEO nov

In the above example we used “/” as a delimiter for string substitution. You can use any character as a delimiter for substitution. For example, if you want to make changes to a url, you need to have a different delimiter because the url already have slashes. So you can substitute like the following.

echo "http://www.example.uk/main.html" | sed 's_uk/main_com/index_'
[email protected]:~# echo "http://www.example.uk/main.html" | sed 's_uk/main_com/index_'
http://www.example.com/index.html

You can also replace a line by matching a string pattern in the line. “-c” flag is used for replacing text using sed. Lets replace the first line in our test.txt file using the following command.

 sed '/1 mike/c 1 michael start beginner mar' test.txt
[email protected]:~# sed '/1 mike/c 1 michael start beginner mar' test.txt
1 michael start beginner mar
10 lucy level beginer mar
45 Dave level expert dec
4 dennis start beginner jul
7 Megan employee trainee feb
58 Mathew Head CEO nov

6. tar

tar command is used to create and extract archive files. “-cf” and “-xf” flags are used for creating and extracting archives.

Syntax: tar  <file/folder name>

Lets  create a tar archive out of test.txt file

tar -cf test.tar test.txt
[email protected]:~# tar -cf test.tar test.txt
[email protected]:~# ls
test.tar  test.txt

Lets extract the test.tar archive to the destination folder “demo” using “-C” flag.

tar -xf test.tar -C /root/demo/
[email protected]:~# tar -xf test.tar -C /root/demo/
[email protected]:~# cd demo/
[email protected]:~/demo# ls
test.txt

7. find

find command is used for finding files. You can find the files using its name with “-name” flag.

find -name  find -name test.txt
[email protected]:/home/ubuntu# cd ~
[email protected]:~# find -name test.txt
./demo/test.txt
./test.txt

You can also find folder using its name by using”/ -name” flag.

find / -name passwd
[email protected]:~# find / -name passwd
/etc/cron.daily/passwd
/etc/pam.d/passwd
/etc/passwd
/usr/share/lintian/overrides/passwd

8. diff

diff command is used to find the difference between two files. Diff command analyses the files and prints the lines which are different. Lets say we have two files test and test1. you can find the difference between the two files using the following command.

Syntax: diff  
        diff test.txt test1.txt
[email protected]:~# diff test.txt test1.txt
7c7
< 59 sdfsd --- > 59 sdfsd  CTO dec

9. Uniq

uniq command is used for filtering out the duplicate line in a file.

Syntax: uniq 
uniq test.txt
[email protected]:~# uniq test.txt
1 mike level intermediate jan
10 lucy level beginer mar
45 Dave level expert dec
4 dennis start beginner jul
7 Megan employee trainee feb
58 Mathew Head CEO nov

10. chmod

chmod command is used for changing the read/write/execute permissions of a file. Permissions are represented in numbers as follows.

4 - read permission
2 - write permission
1 - execute permission
0 - no permission

To give all permissions on test.txt file, you can use the following chmod command.

chmod 755 test.txt

Tell us what you think about this post in the comments section.

TEN LINUX commands

How To Monitor Linux Servers Using Prometheus Node Exporter

Node exporter is the best way to collect all the Linux server related metrics and statistics for monitoring.

Monitor Linux Servers Using Prometheus

In this guide, you will learn how to setup Prometheus node exporter on a Linux server to export all node level metrics to the Prometheus server.

Before You Begin

  1. Prometheus Node Exporter needs Prometheus server to be up and running. If you would like to setup Prometheus, please see the Prometheus setup guide for Linux.
  2. Port 9100 opened in server firewall as Prometheus reads metrics on this port.

Setup Node Exporter Binary

Step 1: Download the latest node exporter package. You should check the Prometheus downloads section for the latest version and update this command to get that package.

cd /tmp
curl -LO https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz

Step 2: Unpack the tarball

tar -xvf node_exporter-0.18.1.linux-amd64.tar.gz

Step 3: Move the node export binary to /usr/local/bin

sudo mv node_exporter-0.18.1.linux-amd64/node_exporter /usr/local/bin/

Create a Custom Node Exporter Service

Step 1: Create a node_exporter user to run the node exporter service.

sudo useradd -rs /bin/false node_exporter

Step 2: Create a node_exporter service file under systemd.

sudo vi /etc/systemd/system/node_exporter.service

Step 3: Add the following service file content to the service file and save it.

[Unit]
Description=Node Exporter
After=network.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target

Step 4: Reload the system daemon and star the node exporter service.

sudo systemctl daemon-reload
sudo systemctl start node_exporter

Step 5: check the node exporter status to make sure it is running in the active state.

sudo systemctl status node_exporter

Step 6: Enable the node exporter service to the system startup.

sudo systemctl enable node_exporter

Now, node exporter would be exporting metrics on port 9100. 

You can see all the server metrics by visiting your server URL on /metrics as shown below.

http://<server-IP>:9100/metrics

Configure the Server as Target on Prometheus Server

Now that we have the node exporter up and running on the server, we have to add this server a target on the Prometheus server configuration.

Note: This configuration should be done on the Prometheus server.

Step 1: Login to the Prometheus server and open the prometheus.yml file.

sudo vi /etc/prometheus/prometheus.yml

Step 2: Under the scrape config section add the node exporter target as shown below. Change 10.142.0.3 with your server IP where you have setup node exporter. Job name can be your server hostname or IP for identification purposes.

- job_name: 'node_exporter_metrics'
    scrape_interval: 5s
    static_configs:
      - targets: ['10.142.0.3:9100']

Step 3: Restart the prometheus service for the configuration changes to take place.

sudo systemctl restart prometheus

Now, if you check the target in prometheus web UI (http://<prometheus-IP>:9090/targets) , you will be able to see the status as shown below.

prometheus node exporter status

Also, you can use the Prometheus expression browser to query for node related metrics. Following are the few key node metrics you can use to find its statistics.

node_memory_MemFree_bytes
node_cpu_seconds_total
node_filesystem_avail_bytes
rate(node_cpu_seconds_total{mode="system"}[1m]) 
rate(node_network_receive_bytes_total[1m])
prometheus node exporter metric queries

Follow “Integrate And Visualize Prometheus Metrics In Grafana” blog to visualize the node exporter metrics in Grafana.