Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 938631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:40:29+00:00 2026-05-15T21:40:29+00:00

I am scanning a file system and wish to determine the allocated size of

  • 0

I am scanning a file system and wish to determine the allocated size of a file on disk.

Using stat() I can get the file size as ( stat(_) )[7] , the blocks allocated on disk can be obtained from ( stat(_) )[12], and the “preferred block size for file system I/O” is ( stat(_) )[11].

If I merely multiply stat column 11 and 12, however, I do not get what appears to be the allocated space on disk (on Solaris 5.10 sparc).

How can I programmatically get the space allocated on disk for a file from the stat() function call in Perl?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:40:30+00:00Added an answer on May 15, 2026 at 9:40 pm

    The value exposed in (stat _)[11] is st_blksize, which is documented as

    A hint as to the “best” unit size for I/O operations. This field is not defined for block special or character special files.

    This is not necessarily the block size of the particular filesystem on which your file resides, but the same manual page contains a convenient definition:

     blkcnt_t st_blocks;   /* Number of 512 byte blocks allocated*/
    

    So you could use code such as

    #! /usr/bin/perl
    
    use warnings;
    use strict;
    
    sub usage { "Usage: $0 file ..\n" }
    
    die usage unless @ARGV;
    
    foreach my $file (@ARGV) {
      my $dir = dirname $file;
    
      my $blocks = (stat $file)[12];
      unless (defined $blocks) {
        warn "$0: stat $file: $!\n";
        next;
      }
    
      print "$file - ", $blocks * 512, "\n";
    }
    

    If you’re concerned that the block sizes of your filesystems aren’t multiples of 512, double-check with either

    df -g <directory>

    or if you have root

    fstyp -v /dev/dsk/...

    For an ordinary file, the size of the file itself, i.e., (stat _)[7], is usually smaller than the total size of all blocks allocated because filesystems allocate whole blocks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

when scanning a file for words and using the built-in hashset class from the
I do scanning of file system and I need identify all 3gp files. Usually,
I have some code reading a text file and scanning for words between brackets:
Using C# (VS2008) and WIA - scanning to TIFF format; When I use the
I'm trying to do barcode scanning using Android emulator. Could anyone please tell me
Anyone got a good handle on a barcode scanning library that can be used
I'm working to create a rudimentary file system in c++ and am having issues
I'm scanning a text file that has a sudoku board with a few separators.
My app scans part of a file system, and my users reported it was
Question: Why do I get this error while scanning a users 'My Documents' folder,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.