Linux File size directive & programming

Source: Internet
Author: User
Tags readable disk usage

In the work and daily programming often need to determine the size of the file, some basic view of the way to do a summary.

A. Linux shell Environment

DF can view the first-level folder size, usage scale, file system and its hang-in points, but there is nothing to file.
Du can view the size of files and folders.

it works well with both. For example, use df to see which level of directory is too large, and then use DF to see the size of the folder or file, so you can quickly determine the crux of the problem.

The following is a brief introduction

the DF command can show the available space and usage for all current file systems

-bash-4.1$ df-h

Filesystem Size used Avail use% mounted on

/dev/mapper/vg_sporsay-lv_root

50G 8.9G 38G 20%/

Tmpfs 127G 216K 127G 1%/DEV/SHM

/dev/sda2 477M 42M 410M 10%/boot

/DEV/SDA1 200M 264K 200M 1%/boot/efi

/dev/mapper/vg_sporsay-lv_home

4.7T 1.7T 2.8T 38%/home9306144 38%/Home

The first field of the above command output ( Filesystem ) and the last field ( mounted on ) are the file system and its hang-in point respectively. We can see that this partition of/DEV/SDA1 is hung in the root directory.

    next four fields size used , avail use% freebsd When the hard disk capacity is full, You may see that the percentage that you have used exceeds the 100% Span style= "Font-family:verdana" >freebsd will leave some space for root root when the file system is full, still can write things to the file system, in order to manage.

du: Querying disk usage space for a file or folder

~/net$ du-h--max-depth=1

60K./socket

20K./pyofwebserver

88K./test

29M.

View The size of the Linux file directory and the number of files that the folder contains

Total Statistics Size

Du-sh xmldb/

DU-SM * | Sort-n// statistics current directory size and size sorting

Du-sk * | Sort-n

Du-sk * | grep GUOJF// look at the size of a person

du-m | Cut-d "/"-F 2// look at The text before the second/character

See how many files are in this folder /*/*/* How many files are there?

Du xmldb/

Du xmldb/*/*/* |wc-l

40752

Explanation:

WC [-LMW]

parameter Description:

-L: How many lines

-M: How many characters

-W: How many words

Two Number of characters in the programming statistics file

Admittedly, the file character number can be obtained by reading the file pointer while the file is being counted, but for large files This method is very time-consuming and CPU resources, such as:

unsigned long get_file_size (const char *path)

{

unsigned long filesize =-1;

FILE *FP;

fp = fopen (path, "R");

if (fp = = NULL)

return filesize;

Fseek (FP, 0L, seek_end);

FileSize = Ftell (FP);

Fclose (FP);

return filesize;

}

Another quick way is to get the file size and all the other information through the file's attribute description, using the following method:

#include <sys/stat.h>

unsigned long get_file_size (const char *path)

{

unsigned long filesize =-1;

struct stat statbuff;

if (stat (path, &statbuff) < 0) {

return filesize;

}else{

FileSize = statbuff.st_size;

}

return filesize;

}

The structure is defined as follows:

  1. struct stat
  2. {
  3. dev_t St_dev; /* ID of device containing file- id*/ of the devices on which the file resides
  4. ino_t St_ino; /* Inode number-inode node number * /
  5. mode_t St_mode; /* File type and access Permissions * /
  6. nlink_t St_nlink; /* Number of hard links- How many connections to this file are linked to ( rigid Connection ) * /
  7. uid_t St_uid; /* User ID of Owner-user id*/
  8. gid_t St_gid; /* Group ID of Owner-group id*/
  9. dev_t St_rdev; /* Device ID (if special file)- unit number, for device file * /
  10. off_t st_size; /* Total size, in bytes- file size , Bytes */
  11. blksize_t st_blksize; /* BlockSize for filesystem I/O- the size of the system block * /
  12. blkcnt_t st_blocks; /* Number of blocks allocated- file occupies * /
  13. time_t St_atime; /* Time of last access- recent access times * /
  14. time_t St_mtime; /* Time of last modification- most recent modified * /
  15. time_t St_ctime; /* Time of last status change-*/
  16. };
  17. where st_mode This variable is used to determine the file type.
  18. St_mode is a feature bit to represent a file type, the feature bit is defined as follows:

  1. S_ifmt bit masks for 0170000 file types
  2. S_ifsock 0140000 Socket
  3. S_iflnk 0120000 Symbolic link (symbolic Link)
  4. S_ifreg 0100000 General documents
  5. S_IFBLK 0060000 block Unit (block device)
  6. S_ifdir 0040000 directory
  7. S_IFCHR 0020000 character device (character device)
  8. S_ififo 0010000 Advanced First Out (FIFO)
  9. S_isuid (set User-id on execution) bit of 0004000 file
  10. S_isgid (set Group-id on execution) bit of 0002000 file
  11. s_isvtx sticky bits of 0001000 file
  12. S_irwxu The Mask value of the 00700 file owner ( that is, the ownership limit )
  13. S_IRUSR 00400 file owner with readable permissions
  14. S_IWUSR 00200 file owner with writable permissions
  15. S_IXUSR 00100 file owner with executable permissions
  16. S_irwxg The Mask value of the 00070 user group ( that is, the ownership limit )
  17. S_IRGRP 00040 User group with readable permissions
  18. S_IWGRP 00020 User group with writable permissions
  19. S_IXGRP 00010 user Group with executable permissions
  20. S_irwxo 00007 Other user's matte value ( that is, ownership limit )
  21. S_iroth 00004 Other users have Read permissions
  22. S_iwoth 00002 Other users with writable permissions
  23. S_ixoth 00001 Other users with executable permissions
  24. excerpted from the Linux C Function Library Reference manual

When judging the file type, use the St_mode value of the file to compare with the value given above. For example:

    1. #include <sys/stat.h>
    2. #include <unistd.h>
    3. #include <stdio.h>
    4. int main ()
    5. {
    6. INT ABC;
    7. struct stat buf;
    8. Stat ("/Home", &BUF);
    9. ABC = buf.st_mode & s_ifdir;// with corresponding symbol phase and
    10. if (abc = = S_IFDIR)// result vs. flag bit
    11. printf ("It ' s a directory.\n");
    12. return 0;
    13. }

Linux File size directive & programming

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.