Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I programmatically get the free disk space for a directory in Linux

Tags:

c++

linux

People also ask

How do I check disk space on a specific directory in Linux?

Linux command to check disk space using:df command – Shows the amount of disk space used and available on Linux file systems. du command – Display the amount of disk space used by the specified files and for each subdirectory.

Which command is used to display free disk space of a specific directory?

The df command stands for disk free, and it shows you the amount of space taken up by different drives. By default, df displays values in 1-kilobyte blocks.

What command would you use to find the disk-free space in Linux system?

That command is df -H. The -H switch is for human-readable format. The output of df -H will report how much space is used, available, percentage used, and the mount point of every disk attached to your system (Figure 1).

How do I find out how much space a directory takes up?

The du command with the options -s (–summarize) and -h (–human-readable) can be used to find out how much disk space a directory is consuming.


check man statvfs(2)

I believe you can calculate 'free space' as f_bsize * f_bfree.

NAME
       statvfs, fstatvfs - get file system statistics

SYNOPSIS
       #include <sys/statvfs.h>

       int statvfs(const char *path, struct statvfs *buf);
       int fstatvfs(int fd, struct statvfs *buf);

DESCRIPTION
       The function statvfs() returns information about a mounted file system.
       path is the pathname of any file within the mounted file  system.   buf
       is a pointer to a statvfs structure defined approximately as follows:

           struct statvfs {
               unsigned long  f_bsize;    /* file system block size */
               unsigned long  f_frsize;   /* fragment size */
               fsblkcnt_t     f_blocks;   /* size of fs in f_frsize units */
               fsblkcnt_t     f_bfree;    /* # free blocks */
               fsblkcnt_t     f_bavail;   /* # free blocks for unprivileged users */
               fsfilcnt_t     f_files;    /* # inodes */
               fsfilcnt_t     f_ffree;    /* # free inodes */
               fsfilcnt_t     f_favail;   /* # free inodes for unprivileged users */
               unsigned long  f_fsid;     /* file system ID */
               unsigned long  f_flag;     /* mount flags */
               unsigned long  f_namemax;  /* maximum filename length */
           };

You can use boost::filesystem:

struct space_info  // returned by space function
{
    uintmax_t capacity;
    uintmax_t free; 
    uintmax_t available; // free space available to a non-privileged process
};

space_info   space(const path& p);
space_info   space(const path& p, system::error_code& ec);

Example:

#include <boost/filesystem.hpp>
using namespace boost::filesystem;
space_info si = space(".");
cout << si.available << endl;

Returns: An object of type space_info. The value of the space_info object is determined as if by using POSIX statvfs() to obtain a POSIX struct statvfs, and then multiplying its f_blocks, f_bfree, and f_bavail members by its f_frsize member, and assigning the results to the capacity, free, and available members respectively. Any members for which the value cannot be determined shall be set to -1.


With C++17

You can use std::filesystem::space:

#include <iostream>  // only needed for screen output

#include <filesystem>
namespace fs = std::filesystem;

int main()
{
    fs::space_info tmp = fs::space("/tmp");

    std::cout << "Free space: " << tmp.free << '\n'
              << "Available space: " << tmp.available << '\n';
}