Check disk space in linux using df command


Linux Version & distribution used for this article
[root@localhost workspace]# uname -sr
Linux 2.6.32-279.el6.x86_64

[root@localhost workspace]# cat /etc/redhat-release
CentOS release 6.3 (Final)



1. df  -- It display available file system details like device name, total size, used space and available space

[root@localhost workspace]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/vg_fedo-lv_root
                      18133780   3579124  14370472  20% /
tmpfs                   247288       420    246868   1% /dev/shm
/dev/sda1               495844     33920    436324   8% /boot


2. a  -- include dummy filesystem

[root@localhost workspace]# df -a
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/vg_fedo-lv_root
                      18133780   3591168  14358428  21% /
proc                         0         0         0   -  /proc
sysfs                        0         0         0   -  /sys
devpts                       0         0         0   -  /dev/pts
tmpfs                   247288       420    246868   1% /dev/shm
/dev/sda1               495844     33920    436324   8% /boot
none                         0         0         0   -  /proc/sys/fs/binfmt_misc
vmware-vmblock               0         0         0   -  /var/run/vmblock-fuse
sunrpc                       0         0         0   -  /var/lib/nfs/rpc_pipefs


3. h  -- human readable format. It will display size format as GB, MB, KB

[root@localhost workspace]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_fedo-lv_root
                       18G  3.5G   14G  21% /
tmpfs                 242M  420K  242M   1% /dev/shm
/dev/sda1             485M   34M  427M   8% /boot


4. df -h /  -- Display only certain mount point

[root@localhost workspace]# df -h /
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_fedo-lv_root
                       18G  3.7G   14G  22% /

[root@localhost workspace]# df -h /boot
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             485M   34M  427M   8% /boot


5. df -B 512  -- we can change the size of block its displaying. Below example each block has 512 bytes

[root@localhost workspace]# df -B 512
Filesystem         512B-blocks      Used Available Use% Mounted on
/dev/mapper/vg_fedo-lv_root
                      36267560   7636832  28262360  22% /
tmpfs                   494576       864    493712   1% /dev/shm
/dev/sda1               991688     67840    872648   8% /boot


6. df --block-size=512k  -- display block in KB format. here it will show each block is 512kb

[root@localhost workspace]# df --block-size=512k
Filesystem         512K-blocks      Used Available Use% Mounted on
/dev/mapper/vg_fedo-lv_root
                         35418      7458     27600  22% /
tmpfs                      483         1       483   1% /dev/shm
/dev/sda1                  969        67       853   8% /boot


7. m  -- display block size in MB

[root@localhost workspace]# df -m
Filesystem           1M-blocks      Used Available Use% Mounted on
/dev/mapper/vg_fedo-lv_root
                         17709      3729     13800  22% /
tmpfs                      242         1       242   1% /dev/shm
/dev/sda1                  485        34       427   8% /boot


8. --total -- display the grand total

[root@localhost workspace]# df --total
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/vg_fedo-lv_root
                      18133780   3818416  14131180  22% /
tmpfs                   247288       432    246856   1% /dev/shm
/dev/sda1               495844     33920    436324   8% /boot
total                 18876912   3852768  14814360  21%


9. i  --  list inode information instead of block usage

[root@localhost workspace]# df -i
Filesystem            Inodes   IUsed   IFree IUse% Mounted on
/dev/mapper/vg_fedo-lv_root
                     1155072  138135 1016937   12% /
tmpfs                  61822       7   61815    1% /dev/shm
/dev/sda1             128016      40  127976    1% /boot


10. T  -- It will display additionaly filesystem type like ext3, ext4.

[root@localhost workspace]# df -T
Filesystem    Type   1K-blocks      Used Available Use% Mounted on
/dev/mapper/vg_fedo-lv_root
              ext4    18133780   3818420  14131176  22% /
tmpfs        tmpfs      247288       432    246856   1% /dev/shm
/dev/sda1     ext4      495844     33920    436324   8% /boot


11. t  --  helps to display specific type of file system

[root@localhost workspace]# df -Tt ext4
Filesystem    Type   1K-blocks      Used Available Use% Mounted on
/dev/mapper/vg_fedo-lv_root
              ext4    18133780   3818412  14131184  22% /
/dev/sda1     ext4      495844     33920    436324   8% /boot


12. x  -- exclude specific type of file system

[root@localhost workspace]# df -Tx ext4
Filesystem    Type   1K-blocks      Used Available Use% Mounted on
tmpfs        tmpfs      247288       432    246856   1% /dev/shm


13. df --help  -- to get df command help information


ENJOY & HAVE FUN WITH LINUX!!!!





Vector and Iterator in C++



PROGRAM:

#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char *argv[])
{
        vector<int> myarr;

        cout << "\nSize : " << myarr.size() << endl;

        //put data at vector
        myarr.push_back(5);
        myarr.push_back(10);
        myarr.push_back(15);
        myarr.push_back(20);
        myarr.push_back(25);

        cout << "\nSize : " << myarr.size() << endl;
        for (int i = 0; i < myarr.size(); i++)
                cout << "\tArray values " << myarr[i] << endl;

        //delete first value
        myarr.erase(myarr.begin());

        cout << "\nSize : " << myarr.size() << endl;
        for (int i = 0; i < myarr.size(); i++)
                cout << "\tArray values " << myarr[i] << endl;

        //delete last value
        myarr.erase(myarr.end()-1);

        cout << "\nSize : " << myarr.size() << endl;
        for (int i = 0; i < myarr.size(); i++)
                cout << "\tArray values " << myarr[i] << endl;

        //insert at position
        myarr.insert(myarr.begin()+2, 100);

        cout << "\nSize : " << myarr.size() << endl;
        for (int i = 0; i < myarr.size(); i++)
                cout << "\tArray values " << myarr[i] << endl;

        //delete middle value
        myarr.erase(myarr.begin()+1);

        cout << "\nSize : " << myarr.size() << endl;
        for (int i = 0; i < myarr.size(); i++)
                cout << "\tArray values " << myarr[i] << endl;

        return 0;
}


OUTPUT:

Size : 0

Size : 5
        Array values 5
        Array values 10
        Array values 15
        Array values 20
        Array values 25

Size : 4
        Array values 10
        Array values 15
        Array values 20
        Array values 25

Size : 3
        Array values 10
        Array values 15
        Array values 20

Size : 4
        Array values 10
        Array values 15
        Array values 100
        Array values 20

Size : 3
        Array values 10
        Array values 100
        Array values 20


Introduction to GNU profiler

Introduction:

Profiling is the process of finding the time complexity of program.

Profiler will find the time taken by the parts of the code. It will help to programmer
to reconstruct the code if the parts of the code consumed more time.

There is many profiling tools available. But this article give idea on native GNU profiler(gprof)

Step 1:

Write a simple program called proftest.c

#include <stdio.h>
void firstFunction(int );
void secondFunction(int );

int main(int argc, char *argv[])
{
        int a = 10;
        int *b = &a;
        firstFunction(a);
        return 0;
}

void firstFunction(int a)
{
        a += 10;
        sleep(1);
        secondFunction(a);
        return;
}

void secondFunction(int b)
{
        sleep(5);
        printf("final value : %d\n", b);
        return;
}

step 2:
Compile the program with '-pg' option using gcc

gcc -pg proftest.c -o profout

Step 3:
Execute the program

./profout

It will create the gmon.out file in current directory

step 4:
run the gprof with gmon.out

 gprof -b profout gmon.out

You will get the output as below

Flat profile:

Each sample counts as 0.01 seconds.
 no time accumulated

  %   cumulative   self              self     total
 time   seconds   seconds    calls  Ts/call  Ts/call  name
  0.00      0.00     0.00        1     0.00     0.00  firstFunction
  0.00      0.00     0.00        1     0.00     0.00  secondFunction

                        Call graph


granularity: each sample hit covers 2 byte(s) no time propagated

index % time    self  children    called     name
                0.00    0.00       1/1           main [7]
[1]      0.0    0.00    0.00       1         firstFunction [1]
                0.00    0.00       1/1           secondFunction [2]
-----------------------------------------------
                0.00    0.00       1/1           firstFunction [1]
[2]      0.0    0.00    0.00       1         secondFunction [2]
-----------------------------------------------

Index by function name

   [1] firstFunction           [2] secondFunction

C Program To Reverse Singly Linked List

PROGRAM:
#include <stdio.h>
#include <stdlib.h>

typedef struct _list {
int data;
struct _list *next;
}list;

int main(int argc, char **argv)
{
printf("\n.. Start of singly linked list reverse program .. \n\n");

list *head=NULL, *head_start=NULL, *display = NULL;
list *newnode;
int a = 0;

/* Add 5 elements to linked list */
for (; a<10; a++)
{
newnode = malloc(sizeof(list));
newnode->data = a+1;
newnode->next = NULL;
if (a==0) {
head = newnode;
head_start = newnode;
}
else {
head->next = newnode;
head = head->next;
}
}

C program to check file available in a path


PROGRAM:
#include <stdio.h>
#include <unistd.h>
 
int main(void) {
    if( access("/home/pc/test.txt", F_OK) != 0 )
                printf("File not available\n");
        else
                printf("File available\n");
        return 0;
}
OUTPUT:
File not available