Sunday, 3 March 2019

comm command in Linux with examples

comm compare two sorted files line by line and write to standard output; the lines that are common and the lines that are unique.

comm command, Linux Tutorial and Material, LPI Learning, LPI Certifications

Suppose you have two lists of people and you are asked to find out the names available in one and not in the other, or even those common to both. comm is the command that will help you to achieve this. It requires two sorted files which it compares line by line.
Before discussing anything further first let’s check out the syntax of comm command:

Syntax :

$comm [OPTION]... FILE1 FILE2

◈ As using comm, we are trying to compare two files therefore the syntax of comm command needs two filenames as arguments.
◈ With no OPTION used, comm produces three-column output where first column contains lines unique to FILE1 ,second column contains lines unique to FILE2 and third and last column contains lines common to both the files.
◈ comm command only works right if you are comparing two files which are already sorted.

Example: Let us suppose there are two sorted files file1.txt and file2.txt and now we will use comm command to compare these two.

// displaying contents of file1 //
$cat file1.txt
Apaar
Ayush Rajput
Deepak
Hemant

// displaying contents of file2 //
$cat file2.txt
Apaar
Hemant
Lucky
Pranjal Thakral

Now, run comm command as:

// using comm command for
comparing two files //
$comm file1.txt file2.txt
                Apaar
Ayush Rajput
Deepak
                Hemant
        Lucky
        Pranjal Thakral

The above output contains of three columns where first column is separated by zero tab and contains names only present in file1.txt ,second column contains names only present in file2.txt and separated by one tab and the third column contains names common to both the files and is separated by two tabs from the beginning of the line.

This is the default pattern of the output produced by comm command when no option is used .

Options for comm command:

1. -1 :suppress first column(lines unique to first file).
2. -2 :suppress second column(lines unique to second file).
3. -3 :suppress third column(lines common to both files).
4. – -check-order :check that the input is correctly sorted, even if all input lines are pairable.
5. – -nocheck-order :do not check that the input is correctly sorted.
6. – -output-delimiter=STR :separate columns with string STR
7. – -help :display a help message, and exit.
8. – -version :output version information, and exit.

Note : The options 4 to 8 are rarely used but options 1 to 3 are very useful in terms of the desired output user wants.

Using comm with options

1. Using -1 ,-2 and -3 options : The use of these three options can be easily explained with the help of example :

//suppress first column using -1//
$comm -1 file1.txt file2.txt
         Apaar
         Hemant
 Lucky
 Pranjal Thakral

//suppress second column using -2//
$comm -2 file1.txt file2.txt
        Apaar
Ayush Rajput
Deepak
        Hemant

//suppress third column using -3//
$comm -3 file1.txt file2.txt           
Ayush Rajput
Deepak     
        Lucky
        Pranjal Thakral

Note that you can also suppress multiple columns using these options together as:

//...suppressing multiple columns...//

$comm -12 file1.txt file2.txt
Apaar
Hemant

/* using -12 together suppressed both first
and second columns */

2. Using – -check-order option : This option is used to check whether the input files are sorted or not and in case if either of the two files are wrongly ordered then comm command will fail with an error message.

$comm - -check-order f1.txt f2.txt

The above command produces the normal output if both f1.txt and f2.txt are sorted and it just gives an error message if either of the two files are not sorted.

3. Using – -nocheck-order option : In case if you don’t want to check whether the input files are sorted or not, use this option. This can be explained with the help of an example.

//displaying contents of unsorted f1.txt//

$cat f1.txt
Parnjal
Kartik

//displaying contents of sorted file f2.txt//

$cat f2.txt
Apaar
Kartik

//now use - -nocheck-order option with comm//

$comm - -nocheck-order f1.txt f2.txt
Pranjal
        Apaar
                Kartik

/*as this option forced comm not to check
 the sorted order that's why the output
comm produced is also
not in sorted order*/

4 . – -output-delimiter=STR option: By default, the columns in the comm command output are separated by spaces as explained above. However, if you want, you can change that, and have a string of your choice as separator. This can be done using the –output-delimiter option. This option requires you to specify the string that you want to use as the separator.

Syntax:

$comm - -output-delimiter=STR FILE1 FILE2

EXAMPLE:

//...comm command with - -output-delimiter=STR option...//

$comm - -output-delimiter=+file1.txt file2.txt
++Apaar
Ayush Rajput
Deepak
++Hemant
+Lucky
+Pranjal Thakral

/*+ before content indicates content of
second column and ++ before content
indicates content of third column*/ 

Friday, 1 March 2019

dmesg command in Linux for driver messages

Linux Study Materials, Linux Tutorial and Material, Linux Certifications

dmesg command also called as “driver message” or “display message” is used to examine the kernel ring buffer and print the message buffer of kernel. The output of this command contains the messages produced by the device drivers.

Usage of dmesg :


When the computer boots up, there are lot of messages(log) generated during the system start-up.
So you can read all these messages by using dmesg command. The contents of kernel ring buffer are also stored in /var/log/dmesg file.

The dmesg command can be useful when system encounters any problem during its start-up, so by reading the contents of dmesg command you can actually find out where the problem occurred(as there are many steps in system boot-up sequence).

Syntax :


dmesg [options]


Options :



-C –clear : clear the ring buffer.
-c –read-clear : clear the ring buffer after printing its contents.
-D –console-off : disable the messages printing to console.
-E –console-on : Enable printing messages to console.
-F –file file : read the messages from given file.
-h –help : display help text.
-k –kernel : print kernel messages.
-t –notime : do not print kernel’s timestamps.
-u –userspace : print userspace messages.

You can check more options here

Since output of dmesg command is very large, so for finding specific information in dmesg output, it is better to use dmesg command with less or grep command.

dmesg | less

or

dmesg | grep "text_to_search"


For example :



This is output of dmesg command when I plugged in USB drive and then unplugged it.
This is part of output of dmesg command, since output is very large, you can try on your Linux terminal

[ 6982.128179] usb 2-2: New USB device found, idVendor=0930, idProduct=6544
[ 6982.128185] usb 2-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 6982.128188] usb 2-2: Product: DataTraveler 2.0
[ 6982.128190] usb 2-2: Manufacturer: Kingston
[ 6982.128193] usb 2-2: SerialNumber: C86000886407C141DA1401A2
[ 6982.253866] usb-storage 2-2:1.0: USB Mass Storage device detected
[ 6982.254035] scsi host3: usb-storage 2-2:1.0
[ 6982.254716] usbcore: registered new interface driver usb-storage
[ 6982.265103] usbcore: registered new interface driver uas
[ 6983.556572] scsi 3:0:0:0: Direct-Access Kingston DataTraveler 2.0 1.00 PQ: 0 ANSI: 4
[ 6983.557750] sd 3:0:0:0: Attached scsi generic sg1 type 0
[ 6983.557863] sd 3:0:0:0: [sdb] 30310400 512-byte logical blocks: (15.5 GB/14.5 GiB)
[ 6983.558092] sd 3:0:0:0: [sdb] Write Protect is off
[ 6983.558095] sd 3:0:0:0: [sdb] Mode Sense: 45 00 00 00
[ 6983.558314] sd 3:0:0:0: [sdb] Write cache: disabled, read cache: enabled, doesn’t support DPO or FUA
[ 6983.560061] sdb: sdb1
[ 6983.563403] sd 3:0:0:0: [sdb] Attached SCSI removable disk
[ 7045.431954] wlp2s0: disassociated from a0:55:4f:27:bd:01 (Reason: 1)
[ 7049.003277] wlp2s0: authenticate with a0:55:4f:27:bd:01
[ 7049.006680] wlp2s0: send auth to a0:55:4f:27:bd:01 (try 1/3)
[ 7049.015786] wlp2s0: authenticated
[ 7049.021441] wlp2s0: associate with a0:55:4f:27:bd:01 (try 1/3)
[ 7049.038590] wlp2s0: RX AssocResp from a0:55:4f:27:bd:01 (capab=0x431 status=0 aid=140)
[ 7049.043217] wlp2s0: associated
[ 7049.063811] wlp2s0: Limiting TX power to 30 (30 – 0) dBm as advertised by a0:55:4f:27:bd:01
[ 7129.257920] usb 2-2: USB disconnect, device number 3

Since output is always large, it is advisable to use dmesg command along with grep command.

For example :

dmesg | grep "usb"

It gives output

[ 5944.925979] usb 2-1: new low-speed USB device number 2 using xhci_hcd
[ 5945.085658] usb 2-1: New USB device found, idVendor=04d9, idProduct=1702
[ 5945.085663] usb 2-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 5945.085666] usb 2-1: Product: USB Keyboard
[ 5945.085669] usb 2-1: Manufacturer:
[ 5945.222536] input: USB Keyboard as /devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1:1.0/0003:04D9:1702.0003/input/input19
[ 5945.282554] hid-generic 0003:04D9:1702.0003: input,hidraw2: USB HID v1.10 Keyboard [ USB Keyboard] on usb-0000:00:14.0-1/input0
[ 5945.284803] input: USB Keyboard as /devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1:1.1/0003:04D9:1702.0004/input/input20
[ 5945.342340] hid-generic 0003:04D9:1702.0004: input,hidraw3: USB HID v1.10 Device [ USB Keyboard] on usb-0000:00:14.0-1/input1
[ 6981.985310] usb 2-2: new high-speed USB device number 3 using xhci_hcd
[ 6982.128179] usb 2-2: New USB device found, idVendor=0930, idProduct=6544
[ 6982.128185] usb 2-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 6982.128188] usb 2-2: Product: DataTraveler 2.0
[ 6982.128190] usb 2-2: Manufacturer: Kingston
[ 6982.128193] usb 2-2: SerialNumber: C86000886407C141DA1401A2
[ 6982.253866] usb-storage 2-2:1.0: USB Mass Storage device detected
[ 6982.254035] scsi host3: usb-storage 2-2:1.0
[ 6982.254716] usbcore: registered new interface driver usb-storage
[ 6982.265103] usbcore: registered new interface driver uas
[ 7129.257920] usb 2-2: USB disconnect, device number 3

Output with options :

For example :

dmesg -t

-t specifies output with timestamps.

Output :

usb 2-2: new high-speed USB device number 3 using xhci_hcd
usb 2-2: New USB device found, idVendor=0930, idProduct=6544
usb 2-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 2-2: Product: DataTraveler 2.0
usb 2-2: Manufacturer: Kingston
usb 2-2: SerialNumber: C86000886407C141DA1401A2
usb-storage 2-2:1.0: USB Mass Storage device detected
scsi host3: usb-storage 2-2:1.0
usbcore: registered new interface driver usb-storage
usbcore: registered new interface driver uas
scsi 3:0:0:0: Direct-Access Kingston DataTraveler 2.0 1.00 PQ: 0 ANSI: 4
sd 3:0:0:0: Attached scsi generic sg1 type 0
sd 3:0:0:0: [sdb] 30310400 512-byte logical blocks: (15.5 GB/14.5 GiB)
sd 3:0:0:0: [sdb] Write Protect is off
sd 3:0:0:0: [sdb] Mode Sense: 45 00 00 00
sd 3:0:0:0: [sdb] Write cache: disabled, read cache: enabled, doesn’t support DPO or FUA
sdb: sdb1
sd 3:0:0:0: [sdb] Attached SCSI removable disk
wlp2s0: disassociated from a0:55:4f:27:bd:01 (Reason: 1)
wlp2s0: authenticate with a0:55:4f:27:bd:01
wlp2s0: send auth to a0:55:4f:27:bd:01 (try 1/3)
wlp2s0: authenticated
wlp2s0: associate with a0:55:4f:27:bd:01 (try 1/3)
wlp2s0: RX AssocResp from a0:55:4f:27:bd:01 (capab=0x431 status=0 aid=140)
wlp2s0: associated
wlp2s0: Limiting TX power to 30 (30 – 0) dBm as advertised by a0:55:4f:27:bd:01
usb 2-2: USB disconnect, device number 3

Friday, 22 February 2019

cron command in Linux with Examples

Cron Command, Linux Tutorial and Material, Linux Certifications, Linux Study Material

The cron is a software utility, offered by Linux-like operating system which automates the scheduled task at a predetermined time. It is a daemon process, which runs as a background process and performs the specified operations at the predefined time when a certain event or condition is triggered without the intervention of a user. To deal with a repeated task frequently is an intimidating task for the system administrator and thus he can schedule such processes to run automatically in the background at regular interval of time by creating a list of those commands using cron. It enables the users to execute the scheduled task on a regular basis unobtrusively like doing the backup every day at midnight, scheduling updates on a weekly basis, synchronizing the files at some regular interval. Cron checks for the scheduled job recurrently and when the scheduled time fields match the current time fields, the scheduled commands are executed. It is started automatically from /etc/init.d on entering multi-user run levels.

Syntax:


cron [-f] [-l] [-L loglevel]


Options:


◈ -f : Used to stay in foreground mode, and don’t daemonize.
◈ -l : This will enable the LSB compliant names for /etc/cron.d files.
◈ -n : Used to add the FQDN in the subject when sending mails.
◈ -L loglevel : This option will tell the cron what to log about the jobs with the following values:
    ◈ 1 : It will log the start of all cron jobs.
    ◈ 2 : It will log the end of all cron jobs.
    ◈ 4 : It will log all the failed jobs. Here the exit status will not equal to zero.
    ◈ 8 : It will log the process number of all the cron jobs.

The crontab (abbreviation for “cron table”) is list of commands to execute the scheduled tasks at specific time. It allows the user to add, remove or modify the scheduled tasks. The crontab command syntax has six fields separated by space where the first five represent the time to run the task and the last one is for the command.

◈ Minute (holds a value between 0-59)
◈ Hour (holds value between 0-23)
◈ Day of Month (holds value between 1-31)
◈ Month of the year (holds a value between 1-12 or Jan-Dec, the first three letters of the month’s name shall be used)
◈ Day of the week (holds a value between 0-6 or Sun-Sat, here also first three letters of the day shall be used)
◈ Command

The rules which govern the format of date and time field as follows:

◈ When any of the first five fields are set to an asterisk(*), it stands for all the values of the field. For instance, to execute a command daily, we can put an asterisk(*) in the week’s field.
◈ One can also use a range of numbers, separated with a hyphen(-) in the time and date field to include more than one contiguous value but not all the values of the field. For example, we can use the 7-10 to run a command from July to October.
◈ The comma (, ) operator is used to include a list of numbers which may or may not be consecutive. For example, “1, 3, 5” in the weeks’ field signifies execution of a command every Monday, Wednesday, and Friday.
◈ A slash character(/) is included to skip given number of values. For instance, “*/4” in the hour’s field specifies ‘every 4 hours’ which is equivalent to 0, 4, 8, 12, 16, 20.


Permitting users to run cron jobs:


◈ The user must be listed in this file to be able to run cron jobs if the file exists.

/etc/cron.allow

◈ If the cron.allow file doesn’t exist but the cron.deny file exists, then a user must not be listed in this file to be able to run the cron job.

/etc/cron.deny

Note: If neither of these files exists then only the superuser(system administrator) will be allowed to use a given command.

Sample commands:


◈ Run /home/folder/gfg-code.sh every hour, from 9:00 AM to 6:00 PM, everyday.
00 09-18 * * * /home/folder/gfg-code.sh
◈ Run /usr/local/bin/backup at 11:30 PM, every weekday.
30 23 * * Mon, Tue, Wed, Thu, Fri /usr/local/bin/backup
◈ Run sample-command.sh at 07:30, 09:30, 13:30 and 15:30.
30 07, 09, 13, 15 * * * sample-command.sh

Tuesday, 19 February 2019

date command in Linux with examples

date command, Linux Tutorial and Material, Linux Certifications, Linux Study Materials

date command is used to display the system date and time. date command is also used to set date and time of the system. By default the date command displays the date in the time zone on which unix/linux operating system is configured.You must be the super-user (root) to change the date and time.

Syntax:


date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

Options with Examples


1: date (no option) : With no options, the date command displays the current date and time, including the abbreviated day name, abbreviated month name, day of the month, the time separated by colons, the time zone name, and the year.

Command: 
$date
Output: 
Tue Oct 10 22:55:01 PDT 2017
Note : Here unix system is configured in pacific daylight time.

2: -u Option: Displays the time in GMT(Greenwich Mean Time)/UTC(Coordinated Universal Time )time zone.

Command: 
$date -u
Output :
Wed Oct 11 06:11:31 UTC 2017

3: –date or -d Option: Displays the given date string in the format of date. But this will not affect the system’s actual date and time value.Rather it uses the date and time given in the form of string.

Syntax:

$date --date=" string "

Command: 
$date --date="2/02/2010"
$date --date="Feb 2 2010"
Output: 
Tue Feb  2 00:00:00 PST 2010
Tue Feb  2 00:00:00 PST 2010

4: Using –date option for displaying past dates:

◈ Date and time of 2 years ago.

Command: 
$date --date="2 year ago"
Output: 
Sat Oct 10 23:42:15 PDT 2015

◈ Date and time of 5 seconds ago.

Command: 
$date --date="5 sec ago"
Output: 
Tue Oct 10 23:45:02 PDT 2017

◈ Date and time of previous day.

Command:
$date --date="yesterday"
Output: 
Mon Oct  9 23:48:00 PDT 2017

◈ Date and time of 2 months ago.

Command: 
$date --date="2 month ago"
Output: 
Thu Aug 10 23:54:51 PDT 2017

◈ Date and time of 10 days ago.

Command: 
$date --date="10 day ago"
Output: 
Sat Sep 30 23:56:55 PDT 2017

5: Using –date option for displaying future date:

◈ Date and time of upcoming particular week day.

Command: 
$date --date="next tue"
Output: 
Tue Oct 17 00:00:00 PDT 2017

◈ Date and time after two days.

Command: 
$date --date="2 day"
Output: 
Fri Oct 13 00:05:52 PDT 2017

◈ Date and time of next day.

Command: 
$date --date="tomorrow"
Output: 
Thu Oct 12 00:08:47 PDT 2017

◈ Date and time after 1 year on the current day.

Command: 
$date --date="1 year"
Output:
Thu Oct 11 00:11:38 PDT 2018

6: -s or –set Option: To set the system date and time -s or –set option is used.

Syntax:

$date --set="date to be set"

Command:
$date
Output: 
Wed Oct 11 15:23:26 PDT 2017
Command: 
$date --set="Tue Nov 13 15:23:34 PDT 2018"
$date
Output: 
Tue Nov 13 15:23:34 PDT 2018

7: –file or -f Option: This is used to display the date string present at each line of file in the date and time format.This option is similar to –date option but the only difference is that in –date we can only give one date string but in a file we can give multiple date strings at each line.

Syntax:

$date --file=file.txt

$cat >> datefile
 Sep 23 2018
 Nov 03 2019
Command:
$date --file=datefile
Output:
Sun Sep 23 00:00:00 PDT 2018
Sun Nov  3 00:00:00 PDT 2019

8: -r Option: This is used to display the last modified timestamp of a datefile .

Syntax:

$date -r file.txt

We can modify the timestamp of a datefile by using touch command.

$touch datefile

$date
Wed Oct 11 15:54:18 PDT 2017
//this is the current date and time
$touch datefile
//The timestamp of datefile is changed using touch command.
This was done few seconds after the above date command’s output.
$date
Wed Oct 11 15:56:23 PDT 2017
//display last modified time of datefile

9: List of Format specifiers used with date command:

%D: Display date as mm/dd/yy.     
%d: Display the day of the month (01 to 31).     
%a: Displays the abbreviated name for weekdays (Sun to Sat).
%A: Displays full weekdays (Sunday to Saturday).
%h: Displays abbreviated month name (Jan to Dec).
%b: Displays abbreviated month name (Jan to Dec).
%B: Displays full month name(January to December).
%m: Displays the month of year (01 to 12).
%y: Displays last two digits of the year(00 to 99).
%Y: Display four-digit year.
%T: Display the time in 24 hour format as HH:MM:SS.
%H: Display the hour.
%M: Display the minute.
%S: Display the seconds.

Syntax:

$date +%[format-option]

Examples:

Command: 
$date "+%D"
Output: 
10/11/17
Command: 
$date "+%D %T"
Output: 
10/11/17 16:13:27
Command: 
$date "+%Y-%m-%d"
Output: 
2017-10-11
Command:
$date "+%Y/%m/%d"
Output: 
2017/10/11
Command: 
$date "+%A %B %d %T %y"
Output: 
Thursday October 07:54:29 17

Sunday, 17 February 2019

basename Command in Linux with examples

Linux Tutorial and Material, Linux Guides, Linux Certifications, Linux Study Materials

basename strips directory information and suffixes from file names i.e. it prints the file name NAME with any leading directory components removed.

Using basename command :


The basename command can be easily used by simply writing basename followed by the file name or the full pathname.

Syntax of basename command :


$basename NAME [SUFFIX]
        or
$basename OPTION NAME

where, NAME refers to the file name or file full pathname and OPTION refers to the options compatible with the basename command and in case, if you want to strip off the suffix of a file you can give the filename followed by the SUFFIX name you want to get rid off.

Example :

/* basename removes directory path and
   returns the file name, kt */
$basename /usr/local/bin/kt
kt

In the first case, the basename command removes the directories name from the full path name given of file kt and in the second case basename removes the suffix .html from kt.html.

Options for basename command :


◈ -a, – -multiple option : This option lets you support multiple arguments and treat each as a NAME i.e you can give multiple file names or full path names with the use of -a option.
/* -a is used for multiple inputs */
$basename -a /usr/local/bin/kt /usr/local/bin/kt.html
kt
kt.html

◈ -s, – -suffix = SUFFIX option : This option removes a trailing suffix SUFFIX,such as a file extension.

/*.html suffix is removed from kt.html
   when followed by -s option */
$basename -s .html kt.html
kt

◈ -z option : This separates the output with NULL rather than a newline.

/* The outputs of the two arguments passed
   are not separated by a newline*/
$basename -az /usr/local/bin/kt kt.html
ktkt

◈ – -help option : This displays the help information and exit.
◈ – -version option : This displays the version information and exit.


Application of basename command :


There are many utilities available which are only useful when it comes to shell scripts and doesn’t have any practical application when used by itself and basename comes under this category.

Example of a shell script which converts gif image files into png image files with the help of basename command :

/*shell script to convert the
gif image file format into
png image file format */

#!/bin/sh
for file in *.gif;do
    #exit if there are no files
    if [! -f $file];then
        exit
    fi
    b='basename $file .gif'
    echo NOW $b.gif is $b.png
    giftopnm $b.gif | pnmtopng >$b.png
done

In the above shell script basename command is used to extract the file names with extension .gif without the extension so that they can be converted into .png files.

Friday, 1 February 2019

SAR command in Linux to monitor system performance

sar : System Activity Report

It can be used to monitor Linux system’s resources like CPU usage, Memory utilization, I/O devices consumption, Network monitoring, Disk usage, process and thread allocation, battery performance, Plug and play devices, Processor performance, file system and more.Linux system Monitoring and analyzing aids understanding system resource usage which can help to improve system performance to handle more requests.

SAR Command, Linux Study Materials, LPI Guides, LPI Certifications

By default SAR command displays result on the output screen, in addition result can also be stored in the file specified by the -o filename option.

Any user can collect information about system performance using system activities flags. The SAR command will show only CPU monitoring activity if any flag is not specifies by user.

Note : sar may not installed by default. We need to install sysstat before using it (For example, in Ubuntu, we can install using sudo apt install sysstat). After installing systat, we need to make sure that data collection is enabled. (For example, in Ubuntu, we can enable data collection by marking ENABLED=”true” in /etc/default/sysstat)

Syntax :


$ sar -[ options ] time_interval number_of_tines_to_display


Examples :


1. To see help

SAR Command, Linux Study Materials, LPI Guides, LPI Certifications

2. To verify the sar version :

hduser@mahesh-Inspiron-3543:~$ sar -V
sysstat version 11.2.0
(C) Sebastien Godard (sysstat  orange.fr)

3. To report CPU details total 5 times with the interval of 2 seconds

hduser@mahesh-Inspiron-3543:~$ sar -u 2 5
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:00:20  IST     CPU     %user     %nice   %system   %iowait    %steal     %idle
04:00:22  IST     all      0.25      0.00      0.00      0.00      0.00     99.75
04:00:24  IST     all      0.25      0.00      0.13      0.00      0.00     99.62
04:00:26  IST     all      0.88      0.00      0.25      1.13      0.00     97.75
04:00:28  IST     all      0.00      0.00      0.25      0.13      0.00     99.62
04:00:30  IST     all      0.25      0.00      0.38      0.12      0.00     99.25
Average:        all      0.33      0.00      0.20      0.28      0.00     99.20

4. To report about amount of memory used, amount of memory free, available cache, available buffers total 3 times with the interval of 1 second.

hduser@mahesh-Inspiron-3543:~$ sar -r 1 3
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:05:12  IST kbmemfree kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty
04:05:13  IST   6067308   2017252     24.95     62300    853612   4303644     35.89   1308856    525628        60
04:05:14  IST   6067308   2017252     24.95     62300    853612   4303644     35.89   1308856    525628        60
04:05:15  IST   6067308   2017252     24.95     62300    853612   4303644     35.89   1308856    525628        60
Average:      6067308   2017252     24.95     62300    853612   4303644     35.89   1308856    525628        60

5. To report about file systems mounted on the device total 5 times with the interval of 2 seconds.

hduser@mahesh-Inspiron-3543:~$ sar -F 2 5
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:02:38  IST  MBfsfree  MBfsused   %fsused  %ufsused     Ifree     Iused    %Iused FILESYSTEM
04:02:40  IST     78181     18727     19.32     24.43   6066698    249334      3.95 /dev/sda11
04:02:40  IST       441        55     11.04     11.04         0         0      0.00 /dev/sda1
04:02:40  IST      2123      1747     45.13     45.13         0         0      0.00 /dev/sdb1
04:02:40  IST     28846    205214     87.68     87.68  29589586    145270      0.49 /dev/sda8

6. To report about block devices details total 3 times with the interval of 1 second.

hduser@mahesh-Inspiron-3543:~$ sar -d 1 3
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:04:34  IST       DEV       tps  rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz     await     svctm     %util
04:04:35  IST    dev8-0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:35  IST   dev8-16      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

7. To report run queue length, number of processes and load average

hduser@mahesh-Inspiron-3543:~$ sar -q 2 5
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:01:54  IST   runq-sz  plist-sz   ldavg-1   ldavg-5  ldavg-15   blocked
04:01:56  IST         0       491      0.21      0.16      0.15         0
04:01:58  IST         0       491      0.21      0.16      0.15         0
04:02:00  IST         0       491      0.19      0.16      0.15         0
04:02:02  IST         0       491      0.19      0.16      0.15         0
04:02:04  IST         0       491      0.18      0.16      0.14         0
Average:            0       491      0.20      0.16      0.15         0

8. To report cpu usage for given core :

hduser@mahesh-Inspiron-3543:~$ sar -P 1 1 3
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:16:38  IST     CPU     %user     %nice   %system   %iowait    %steal     %idle
04:16:39  IST       1      0.00      0.00      0.00      0.00      0.00    100.00
04:16:40  IST       1      0.99      0.00      0.99      0.00      0.00     98.02
04:16:41  IST       1      1.00      0.00      0.00      0.00      0.00     99.00
Average:          1      0.66      0.00      0.33      0.00      0.00     99.00

9. To report about network interface, network speed, IPV4, TCPV4, ICMPV4 network traffic and errors

hduser@mahesh-Inspiron-3543:~$ sar -n DEV 1 3 | egrep -v lo
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:04:00  IST     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s   %ifutil
04:04:01  IST enp0s29u1u2      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:01  IST    enp7s0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

10. To report details about the process, kernel thread, i-node, and the file tables

hduser@mahesh-Inspiron-3543:~$ sar -v 1 3
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:25:26  IST dentunusd   file-nr  inode-nr    pty-nr
04:25:27  IST     43219      7584     46874        17
04:25:28  IST     43219      7584     46873        17
04:25:29  IST     43219      7584     46873        17
Average:        43219      7584     46873        17

11. To report messages, semaphores and processes details for all processors and system-wide.

hduser@mahesh-Inspiron-3543:~$ sar -mu -P ALL

12. To report statistics about swapping

hduser@mahesh-Inspiron-3543:~$ sar -S 1 3
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:08:09  IST kbswpfree kbswpused  %swpused  kbswpcad   %swpcad
04:08:10  IST   3906556         0      0.00         0      0.00
04:08:11  IST   3906556         0      0.00         0      0.00
04:08:12  IST   3906556         0      0.00         0      0.00
Average:      3906556         0      0.00         0      0.00

13. To report details about I/O operations like transaction per second, read per second, write per second

hduser@mahesh-Inspiron-3543:~$ sar -b 1 3
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:08:41  IST       tps      rtps      wtps   bread/s   bwrtn/s
04:08:42  IST      0.00      0.00      0.00      0.00      0.00
04:08:43  IST      2.00      0.00      2.00      0.00     64.00
04:08:44  IST      0.00      0.00      0.00      0.00      0.00
Average:         0.67      0.00      0.67      0.00     21.33

14. To report statistics about context switching, number of processes created per second, number of swap per second

hduser@mahesh-Inspiron-3543:~$ sar -w 1 3
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:09:42  IST    proc/s   cswch/s
04:09:43  IST      0.00    480.00
04:09:44  IST      0.00    637.00
04:09:45  IST      0.00    859.00
Average:         0.00    658.67