Operating System - Linux
1753664 Members
5724 Online
108798 Solutions
New Discussion юеВ

Re: Calculating byte size in a directory

 
SOLVED
Go to solution
Henry Chua
Super Advisor

Calculating byte size in a directory

Hi guys

Is it possible to write a program in C to calculate the byte size in a directory?

Regards
Henry
6 REPLIES 6
Rajeev  Shukla
Honored Contributor

Re: Calculating byte size in a directory

Yes what you could do is write a program to open the directory read all the files in that directory and then use the stat() function in C which gives you a pointer to structure and get "st_size file size in bytes" add all of them for all the files.
That will return you the total bytes used by the directory, something smiliar to du command

Cheers
Rajeev
Ralph Grothe
Honored Contributor

Re: Calculating byte size in a directory

As said, this boils down to combining these syscalls

opendir(), readdir(), closedir(), stat()
Madness, thy name is system administration
Muthukumar_5
Honored Contributor
Solution

Re: Calculating byte size in a directory

You can use stat() as,

#include
#include
#include

main()
{
int rc;
struct stat buf;
rc=stat("/tmp/block",&buf);
if ( rc==0 ) {
printf ("File size = %d bytes\n",buf.st_size);
} else {
printf ("Error\n");
}
}

cc -o block block.c
./block

change name /tmp/block to some others.

You can also use du -b . Sample example c program is available as,
http://www.phim.unibe.ch/comp_doc/c_manual/C/EXAMPLES/stat.c

hth.
Easy to suggest when don't know about the problem!
Geoff Wild
Honored Contributor

Re: Calculating byte size in a directory

ere's one using awk


# cat /home/root/llsum
#!/bin/sh
#
# Program: /scripts/llsum
# By: Geoff wild
# Usage: llsum
#
# Displays a truncated "ll" listing and displays size
# statistics of the files in the listing
#
# FYI
# %s means we are printing a string
# %d means we are printing an integer
#
# Awk field numbers:
# $1 $2 $3 $4 $5 $6 $7 $8 $9
# -rwx------ 1 root sys 774 Feb 24 12:07 llsum
#
ls -al $* |\
awk 'BEGIN { x=i=0; printf "%-16s%-10s%8s%8s\n",\
"FILENAME","OWNER","SIZE","TYPE" }

# Print owner, size, and type. Then sum the size.
$1 ~ /^[-dlps]/ { # line format for normal files
printf "%-16s%-10s%8d",$9,$3,$5
x = x + $5
i++
}
# If the line starts with a - its a regular file; d is
# directory, etc.
$1 ~ /^-/ { printf "%8s\n","file" } # standard file types
$1 ~ /^d/ { printf "%8s\n","dir" }
$1 ~ /^l/ { printf "%8s\n","link" }
$1 ~ /^p/ { printf "%8s\n","pipe" }
$1 ~ /^s/ { printf "%8s\n","socket" }
$1 ~ /^[bc]/ { #line format for device files
printf "%-16s%-10s%8s%8s\n",$10,$3,"","dev"
}
END { printf "\nThese files occupy %d bytes (%.4f Mbytes)\n",\
x, x / (1024*1024)
printf "Average file size is %d bytes\n", x/i
}'

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Henry Chua
Super Advisor

Re: Calculating byte size in a directory

Hi guys,

How do i used opendir and readdir to list out all the files in a directory...

Best regards
Henry
A. Clay Stephenson
Acclaimed Contributor

Re: Calculating byte size in a directory

Man opendir. It has an example of the use of opendir and readdir.
If it ain't broke, I can fix that.