calculate internal fragmentation in directory

hi

sorry for grammar mistakes but i am writting these fro tablet and am not realy used to ot so well yet....

i am in the middle of doing work here and hope some1 ca answear my question asap please :slight_smile:

How to calculate the amount of internal fragmentation using script.

cd directory ...
and???

whould i use du - s and number compare to moething oris there any command to calculate internal fragmentation?

thanks

Definition I found with Google:
Internal Fragmentation is the term used to describe disk space which is allocated to a file but unused because the file is smaller than the allocated space.

As you again omit to mention what Operating System and version you have, and what Filesystem Type you have (e.g. UFS, VxFS , whatever) all answers can only be general.

Comparing the output from "du -s" with the total of the sizes of the files as reported by "ls -la" demonstrates the discrepancy.

Proprietary software may well provide better tools. Depends what you have.

It's a real problem on Windows systems with large discs and large numbers of small files. A disc upgrade can actually reduce the amount of free space!

hey methyl and thanks foryouranswer

i know what internal fragmentation is i think i asked wrong way

what i meant was, how to write a script that will calculate in % how much dsck spaceistakingthat directory where script is executed

will appreciate help!

...filesystem is linux-opensuse ext3

---------- Post updated at 02:21 PM ---------- Previous update was at 02:08 PM ----------

if its stillnotclear i will write exactlyhowi got it...

write a script that will calculate amountof internal fragmentation forall files in directory. assume each block is1024 bytes

Filesystem doesn't matter.

Just:

#!/bin/bash
DIR=/home/username
# du outputs "blocks folder" so we read both and just ignore the folder.
read USED_K FOLDER <<<$(du -s -B 1024 "$DIR" 2> /dev/null)
# du outputs a header line, then "filesystem blocks used available ..."
# so we ignore the header line with 'tail', then read the first three.
read FILESYSTEM TOTAL_K G <<<$(df -B 1024 "$DIR" | tail -n 1)
echo "$DIR uses $(( (USED_K*100) / TOTAL_K ))% of $FILESYSTEM"

Running this gets me

/home/username uses 51% of /dev/sdc5

---------- Post updated at 01:31 PM ---------- Previous update was at 01:27 PM ----------

Which do you want? The space used, or something nebulous about fragmentation?

1 Like

doesnt matter now, I used the code you posted before and hope it will do so
BIG THANKS TO YOU !

It matters if you're doing anything with it... I don't understand why you'd stop caring when the time's up, unless this is homework.

Ah, maybe the OP wants to know the amount of disk space used (blocks allocated * block size) versus the size of the files in bytes.

find . \( -type f -o -type d \) -printf "%b %s\n"  |awk '{ blocks+=$1; bytes+=$2; } END { print bytes/(blocks*BLKSZ)} ' BLKSZ=512

Gives me 0.997 for one directory, and 0.955 for another. Normally BLKSZ is 512, irrespective of the underlying filesystem's concept of a 'block'.

To be sure, just do

mkdir test; cd test;
find . -name . -printf "%b %s\n" | awk '{ print "Block size is " $2/$1 }'

One problem is that this doesn't account for hard-linked files, and therefore whose disk fragmentation would incorrectly be counted double.