Linux system gets hard drive usage information _linux

Source: Internet
Author: User
Tags disk usage

1, DF command

Linux can be used with DF command to obtain the use of hard disk, through the man can get the details of the DF command. The parameters commonly used by the DF command are:

A: Displays all the file systems and the disk usage scenarios for each partition
I: Show the amount of i-nodes used
K: size is represented by K (default value)
T: Displays all partition disk usage for one file system
X: Displays all partition disk usage that is not a file system
T: Displays the name of the file system to which each partition belongs
Common commands: Df-hi

Examples of screenshots are shown below



2. du command

The du command is used to query the disk usage space of the file or directory, and the man gets a detailed description of the du command. The commonly used command parameters are as follows:
A: Displays the disk space for each file in all directories and second directories
B: The size is represented by bytes (the default value is k bytes)
C: Last plus total (default)
S: Show only the sum of each file size (summarize)
x: Only the files of the same file system are counted
L: Calculate the size of all files
Common commands: Du-ah

Examples are shown in the following illustration:



3, STATFS structure and function

Before looking at apue time, in the fourth chapter file and the catalogue, talked about obtains the file information the stat structure, through STAT structure may obtain the file the size, the creation time, the modification time, the user ID, the group ID and so on. The stat structure and operation function of Man are shown in the following figure:



Today, the main summary of learning to get the hard disk information statfs structure, through the STATFS structure of information to calculate the path of the disk usage. Man on Statfs The introduction is as follows:



The Chinese meaning of the STATFS structure is as follows:

Copy Code code as follows:

struct STATFS
{
Long F_type; /* File System type * *
Long f_bsize; /* Optimized transfer block Size * *
Long f_blocks; /* Total number of file system blocks * *
Long F_bfree; /* Usable block number * *
Long F_bavail; /* Non-super user can get the number of blocks * *
Long F_files; /* Total number of file nodes * *
Long F_ffree; * * Available file knot/
fsid_t F_fsid; /* File System ID * *
Long F_namelen; /* Maximum length of file name * *
};



The number of free space blocks in the STATFS structure has two kinds of f_bfree and F_bavail, the former is all the remaining space of the hard disk, the latter is the remaining space of the non-root user, the Ext3 file system gives root user 5% of the exclusive space, so this is a different place. The emphasis here is that the size of each block is generally 4K. Therefore, to achieve the same as the DF result, we have to multiply the number of blocks by 4, so that the used, usable, total block number can be achieved.
The test program looks like this:

Copy Code code as follows:

#include <stdio.h>
#include <sys/statfs.h>
#include <sys/vfs.h>
#include <errno.h>

int main (int argc, char *argv[])
{
struct STATFS disk_info;
Char *path = "/home/";
int ret = 0;
if (argc = 2)
{
Path = argv[1];
}
if (ret = = Statfs (path, &disk_info) = =-1)
{
fprintf (stderr, "Failed to get file disk infomation,\
errno:%u, reason:%s\n ", errno, Strerror (errno));
return-1;
}
Long Long total_size = Disk_info.f_blocks * disk_info.f_bsize;
Long Long available_size = Disk_info.f_bavail * disk_info.f_bsize;
Long Long free_size = Disk_info.f_bfree * disk_info.f_bsize;
Output the length of each block, Linux under the memory block of 4KB
printf ("Block size:%ld bytes\n", disk_info.f_bsize);
Number of output blocks
printf ("Total data blocks:%ld \ n", disk_info.f_blocks);
The size of the disk that the output path resides on
printf ("Total file disk size:%d mb\n", total_size >> 20);
Output the amount of disk space available to non-root users
printf ("avaiable Size:%d mb\n", available_size >> 20);
Output all remaining space on the hard drive
printf ("Free Size:%d mb\n", free_size >> 20);
Number of file nodes on the output disk
printf ("Total file nodes:%ld\n", disk_info.f_files);
Output number of available file nodes
printf ("Free File nodes:%ld\n", disk_info.f_ffree);
Maximum length of output file name
printf ("Maxinum Length of file name:%ld\n", Disk_info.f_namelen);
return 0;
}


The test results are as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.