473,738 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

get disk usage

2 New Member

Somebody knows how I can obtain disk space usage and the space that left in disk.
Sep 20 '06 #1
2 24286
144 New Member
On a POSIX system you would use the file system statistics data type and function calls. These are defined in <sys/statvfs.h>.

Here is what the data structure and funtion prototypes should look like in <sys/statvfs.h>

Expand|Select|Wrap|Line Numbers
  1. struct statvfs {
    unsigned long  f_bsize;    /* file system block size */
  2. unsigned long  f_frsize;   /* fragment size */
  3. fsblkcnt_t     f_blocks;   /* size of fs in f_frsize units */
  4. fsblkcnt_t     f_bfree;    /* # free blocks */
  5. fsblkcnt_t     f_bavail;   /* # free blocks for non-root */
  6. fsfilcnt_t     f_files;    /* # inodes */
  7. fsfilcnt_t     f_ffree;    /* # free inodes */
  8. fsfilcnt_t     f_favail;   /* # free inodes for non-root */
  9. unsigned long  f_fsid;     /* file system ID */
  10. unsigned long  f_flag;     /* mount flags */
  11. unsigned long  f_namemax;  /* maximum filename length */};
  13. /* These functions return 0 on success and -1 on failure */
  15. int statvfs(const char *filename, struct statvfs buf);
  16. int fstatvfs(int fd, struct statvfs buf);
This sample code outputs the disk usage and free space in bytes.

Expand|Select|Wrap|Line Numbers
  1. include <sys/statvfs.h>
  3. ...
  5. /* Any file on the filesystem in question */
  6. char *filename = "/home/tyreld/somefile.txt";
  8. struct statvfs buf;
  9. if (!statvfs(filename, &buf)) {
    unsigned long blksize, blocks, freeblks, disk_size, used, free;
  11. blksize = buf.f_bsize;
  12. blocks = buf.f_blocks;
  13. freeblks = buf.f_bfree;
  15. disk_size = blocks * blksize;
  16. free = freeblks * blksize;
  17. used = disk_size - free;
  19. printf("Disk usage : %lu \t Free space %lu\n", used, free);} else {
    printf("Couldn't get file system statistics\n");
Sep 20 '06 #2
1 New Member
Thanks tyreld

Good post..Aftr searching long time got this post..
I have used it as part of my code.
Jan 31 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Peter Mount | last post by:
Hello Are there any good open source examples (or tutorials) for developing jsp pages to display web stats (like number of hits etc) and disk usage for a web site? I'm currently using JSP 2 with jdk1.4.2_05 and Tomcat 5.0.28 Thanks Peter Mount info@petermount.au.com
by: Gh! | last post by:
How can I see how much disk space is being occupied by an InnoDB table? Looking into /var/lib/mysql doesn't seem to work anymore for InnoDB tables. Please tell also if there is a way to see how much space is occupied by a particular row or column of a table. Thanks
by: Matthew Nuzum | last post by:
We've had a chance to talk to some of our competitors who have recently gone out of business. One of the major failings that contributed to their collapse was not accurately understanding how much disk space and bandwidth they were using up. Therefore, they often lost money unexpectedly which, as most of you know, is not sustainable. Therefore, we'd like to avoid falling into the same trap and get a good understanding of how much...
by: Joe | last post by:
Hi Guys, Is it possible to create a disk management system using .NET entirely. What I mean by the above is having an application possibly a .NET windows service monitoring the disk, creating and adding users and giving them access to store a limited amount of data. The only thing which does this in my knowledge is the FileSystemWatcher component. But how do I then monitor
by: Nils Rennebarth | last post by:
I have a table that is essentially a log where new entries are streaming in continually and from time to time I throw old entries away to keep the table from growing. I understand that in addition to issue a DELETE FROM log WHERE date < xxx I also need to issue a VACUUM log so that new entries will use the space of deleted entries. Now I want to reserve a certain amount of disk storage to hold the log table. So I first let the table...
by: UJ | last post by:
I've now got the way to figure out how much disk space is on a disk. Is there any way to get the disk space usage for a specific directory and below? I could write a routine easily but if there is code already out there that does it...... TIA - Jeff.
by: Tom | last post by:
I'd greatly appreciate advice and code snippets on how to create a ram disk within a C/C++ program. I also need to be able to determine the free space. Thanks in advance for any help.
by: fchen00 | last post by:
Hi there, I’m running Postgres on SuSe Linux and some times the database uses so much disk space that we got disk full problems. Is there a way in postgres to specify how much disk space to use? For instance, I’d like it to use no more than 50G. And if there is such parameter, what is the effect? Will postgres simply stop writing to the disk? Your help is greatly appreciated!
by: Sankar | last post by:
Dear all, I am programming in Linux , wherein I need to know a couple of things. 1) Does an API exist that can copy file onto another file ( an API equivalent of 'cp') ? 2) Is there an API that tells me the disk usage ( equivalent for cmdline
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.