Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file size on disk on linux?

Tags:

c++

c

linux

I want to find the size of file on disk on linux OS . I know command to do so: du -s -h

Is there any way to find it using c/c++ code ?

like image 779
Ashish Avatar asked Apr 26 '11 15:04

Ashish


People also ask

How do I see file size in Linux?

Using the ls Command–l – displays a list of files and directories in long format and shows the sizes in bytes.

How do I tell the size of a disk?

Right-click the file and click Properties. The image below shows that you can determine the size of the file or files you have highlighted from in the file properties window. In this example, the chrome. jpg file is 18.5 KB (19,032 bytes), and that the size on disk is 20.0 KB (20,480 bytes).

How do I see file size in bash?

Another method we can use to grab the size of a file in a bash script is the wc command. The wc command returns the number of words, size, and the size of a file in bytes.

How do you check the size of all files in a directory Linux?

How to view the file size of a directory. To view the file size of a directory pass the -s option to the du command followed by the folder. This will print a grand total size for the folder to standard output. Along with the -h option a human readable format is possible.


1 Answers

Yes, use the stat(2) system call:

#include <sys/stat.h>
...
struct stat statbuf;

if (stat("file.dat", &statbuf) == -1) {
  /* check the value of errno */
}

printf("%9jd", (intmax_t) statbuf.st_size);
like image 153
Blagovest Buyukliev Avatar answered Oct 17 '22 04:10

Blagovest Buyukliev