LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   directories size is always 4K (https://www.linuxquestions.org/questions/linux-newbie-8/directories-size-is-always-4k-391359/)

Milosevic 12-10-2005 03:53 PM

directories size is always 4K
 
Why is the directories size always 4K ?

milosevic@slackws:~$ ls -ldh /
drwx--x--x 25 milosevic users 4.0K 2005-12-10 19:47 //

I mean, shouldn't it be the sum of the sizes of the files that the dir contains? Or does it have anyhing to do with the inode's size?

Cya fellas.

Tinkster 12-10-2005 04:18 PM

I can't tell you why your root directory is 4K in size,
but the fact that it's not owned by root:root is a worry.

Who set-up that box with those incredibly stupid ownerships?



Cheers,
Tink

Milosevic 12-10-2005 04:26 PM

hm... sorry... actually the thing is like this:

milosevic@slackws:~$ ls -lhd /
drwxr-xr-x 19 root root 4.0K 2005-12-09 15:22 //

acid_kewpie 12-10-2005 04:35 PM

it's the block size of the filesystem. the size of a directory and the size of all files within it are a totally different thing.

Tinkster 12-10-2005 04:43 PM

Hmmm ... nice attempt of an explanation, but I don't think
it's correct. On 4 different machines here I get four different
sizes, and they're all under 1K (616,624,704,512) ... so the
"human-readable" didn't quite mess up the results.


Cheers,
Tink

KimVette 12-10-2005 05:09 PM

If you're trying to find out how much space is taken up by say, root (/) and its subdirectories, you will want to use the du (disk usage) command:

Code:

hostname:/# du -h
As far as the listed size of the directories is concerned, let's experiment, watching the directory size of /foo grow as we progress:

Code:

kimp4:/ # ls -l
total 40144
drwxr-xr-x  23 root root      568 2005-12-10 18:13 .
drwxr-xr-x  23 root root      568 2005-12-10 18:13 ..
drwxr-xr-x    2 root root    2920 2005-12-04 19:29 bin
drwxr-xr-x    3 root root      448 2005-12-04 19:49 boot
drwxr-xr-x  11 root root    16140 2005-12-09 15:52 dev
drwxr-xr-x  90 root root    7960 2005-12-09 15:52 etc
-rw-r--r--    1 root root 41041920 2005-12-06 14:03 etc.tar
drwxr-xr-x    3 root root      72 2005-12-04 20:12 home
drwxr-xr-x  11 root root    4136 2005-12-05 02:19 lib
drwxr-xr-x    6 root root      192 2005-12-09 15:52 media
drwxr-xr-x  11 root root      280 2005-12-06 15:43 mnt
drwxr-xr-x  10 root root      264 2005-12-04 23:29 opt
dr-xr-xr-x  123 root root        0 2005-12-09 10:49 proc
drwx------  14 root root      752 2005-12-10 18:10 root
drwxr-xr-x    3 root root    7976 2005-12-04 20:08 sbin
drwxr-xr-x    4 root root      96 2005-12-04 19:10 srv
drwxr-xr-x    2 root root      48 2005-09-09 05:59 subdomain
drwxr-xr-x  10 root root        0 2005-12-09 10:50 sys
drwxrwxrwt  25 root root    1816 2005-12-10 18:00 tmp
drwxr-xr-x  12 root root      344 2005-12-04 19:24 usr
drwxr-xr-x  14 root root      360 2005-12-04 19:14 var
drwxr-xr-x    5 root root      120 2005-12-06 15:38 windows
kimp4:/ # mkdir foo
kimp4:/ # ls -lha
total 40M
drwxr-xr-x  24 root root  592 2005-12-10 18:13 .
drwxr-xr-x  24 root root  592 2005-12-10 18:13 ..
drwxr-xr-x    2 root root 2.9K 2005-12-04 19:29 bin
drwxr-xr-x    3 root root  448 2005-12-04 19:49 boot
drwxr-xr-x  11 root root  16K 2005-12-09 15:52 dev
drwxr-xr-x  90 root root 7.8K 2005-12-09 15:52 etc
-rw-r--r--    1 root root  40M 2005-12-06 14:03 etc.tar
drwxr-xr-x    2 root root  48 2005-12-10 18:13 foo
drwxr-xr-x    3 root root  72 2005-12-04 20:12 home
drwxr-xr-x  11 root root 4.1K 2005-12-05 02:19 lib
drwxr-xr-x    6 root root  192 2005-12-09 15:52 media
drwxr-xr-x  11 root root  280 2005-12-06 15:43 mnt
drwxr-xr-x  10 root root  264 2005-12-04 23:29 opt
dr-xr-xr-x  123 root root    0 2005-12-09 10:49 proc
drwx------  14 root root  752 2005-12-10 18:10 root
drwxr-xr-x    3 root root 7.8K 2005-12-04 20:08 sbin
drwxr-xr-x    4 root root  96 2005-12-04 19:10 srv
drwxr-xr-x    2 root root  48 2005-09-09 05:59 subdomain
drwxr-xr-x  10 root root    0 2005-12-09 10:50 sys
drwxrwxrwt  25 root root 1.8K 2005-12-10 18:00 tmp
drwxr-xr-x  12 root root  344 2005-12-04 19:24 usr
drwxr-xr-x  14 root root  360 2005-12-04 19:14 var
drwxr-xr-x    5 root root  120 2005-12-06 15:38 windows
kimp4:/ # mkdir foo/a
kimp4:/ # ls -lha
total 40M
drwxr-xr-x  24 root root  592 2005-12-10 18:13 .
drwxr-xr-x  24 root root  592 2005-12-10 18:13 ..
drwxr-xr-x    2 root root 2.9K 2005-12-04 19:29 bin
drwxr-xr-x    3 root root  448 2005-12-04 19:49 boot
drwxr-xr-x  11 root root  16K 2005-12-09 15:52 dev
drwxr-xr-x  90 root root 7.8K 2005-12-09 15:52 etc
-rw-r--r--    1 root root  40M 2005-12-06 14:03 etc.tar
drwxr-xr-x    3 root root  72 2005-12-10 18:13 foo
drwxr-xr-x    3 root root  72 2005-12-04 20:12 home
drwxr-xr-x  11 root root 4.1K 2005-12-05 02:19 lib
drwxr-xr-x    6 root root  192 2005-12-09 15:52 media
drwxr-xr-x  11 root root  280 2005-12-06 15:43 mnt
drwxr-xr-x  10 root root  264 2005-12-04 23:29 opt
dr-xr-xr-x  123 root root    0 2005-12-09 10:49 proc
drwx------  14 root root  752 2005-12-10 18:10 root
drwxr-xr-x    3 root root 7.8K 2005-12-04 20:08 sbin
drwxr-xr-x    4 root root  96 2005-12-04 19:10 srv
drwxr-xr-x    2 root root  48 2005-09-09 05:59 subdomain
drwxr-xr-x  10 root root    0 2005-12-09 10:50 sys
drwxrwxrwt  25 root root 1.8K 2005-12-10 18:00 tmp
drwxr-xr-x  12 root root  344 2005-12-04 19:24 usr
drwxr-xr-x  14 root root  360 2005-12-04 19:14 var
drwxr-xr-x    5 root root  120 2005-12-06 15:38 windows
kimp4:/ # mkdir foo/b
kimp4:/ # ls -lha
total 40M
drwxr-xr-x  24 root root  592 2005-12-10 18:13 .
drwxr-xr-x  24 root root  592 2005-12-10 18:13 ..
drwxr-xr-x    2 root root 2.9K 2005-12-04 19:29 bin
drwxr-xr-x    3 root root  448 2005-12-04 19:49 boot
drwxr-xr-x  11 root root  16K 2005-12-09 15:52 dev
drwxr-xr-x  90 root root 7.8K 2005-12-09 15:52 etc
-rw-r--r--    1 root root  40M 2005-12-06 14:03 etc.tar
drwxr-xr-x    4 root root  96 2005-12-10 18:14 foo
drwxr-xr-x    3 root root  72 2005-12-04 20:12 home
drwxr-xr-x  11 root root 4.1K 2005-12-05 02:19 lib
drwxr-xr-x    6 root root  192 2005-12-09 15:52 media
drwxr-xr-x  11 root root  280 2005-12-06 15:43 mnt
drwxr-xr-x  10 root root  264 2005-12-04 23:29 opt
dr-xr-xr-x  123 root root    0 2005-12-09 10:49 proc
drwx------  14 root root  752 2005-12-10 18:10 root
drwxr-xr-x    3 root root 7.8K 2005-12-04 20:08 sbin
drwxr-xr-x    4 root root  96 2005-12-04 19:10 srv
drwxr-xr-x    2 root root  48 2005-09-09 05:59 subdomain
drwxr-xr-x  10 root root    0 2005-12-09 10:50 sys
drwxrwxrwt  25 root root 1.8K 2005-12-10 18:00 tmp
drwxr-xr-x  12 root root  344 2005-12-04 19:24 usr
drwxr-xr-x  14 root root  360 2005-12-04 19:14 var
drwxr-xr-x    5 root root  120 2005-12-06 15:38 windows
kimp4:/ # touch foo/c
kimp4:/ # ls -lha
total 40M
drwxr-xr-x  24 root root  592 2005-12-10 18:13 .
drwxr-xr-x  24 root root  592 2005-12-10 18:13 ..
drwxr-xr-x    2 root root 2.9K 2005-12-04 19:29 bin
drwxr-xr-x    3 root root  448 2005-12-04 19:49 boot
drwxr-xr-x  11 root root  16K 2005-12-09 15:52 dev
drwxr-xr-x  90 root root 7.8K 2005-12-09 15:52 etc
-rw-r--r--    1 root root  40M 2005-12-06 14:03 etc.tar
drwxr-xr-x    4 root root  120 2005-12-10 18:14 foo
drwxr-xr-x    3 root root  72 2005-12-04 20:12 home
drwxr-xr-x  11 root root 4.1K 2005-12-05 02:19 lib
drwxr-xr-x    6 root root  192 2005-12-09 15:52 media
drwxr-xr-x  11 root root  280 2005-12-06 15:43 mnt
drwxr-xr-x  10 root root  264 2005-12-04 23:29 opt
dr-xr-xr-x  123 root root    0 2005-12-09 10:49 proc
drwx------  14 root root  752 2005-12-10 18:10 root
drwxr-xr-x    3 root root 7.8K 2005-12-04 20:08 sbin
drwxr-xr-x    4 root root  96 2005-12-04 19:10 srv
drwxr-xr-x    2 root root  48 2005-09-09 05:59 subdomain
drwxr-xr-x  10 root root    0 2005-12-09 10:50 sys
drwxrwxrwt  25 root root 1.8K 2005-12-10 18:00 tmp
drwxr-xr-x  12 root root  344 2005-12-04 19:24 usr
drwxr-xr-x  14 root root  360 2005-12-04 19:14 var
drwxr-xr-x    5 root root  120 2005-12-06 15:38 windows
kimp4:/ # cd foo
kimp4:/foo # ls -lha
total 512
drwxr-xr-x  4 root root 120 2005-12-10 18:14 .
drwxr-xr-x  24 root root 592 2005-12-10 18:13 ..
drwxr-xr-x  2 root root  48 2005-12-10 18:13 a
drwxr-xr-x  2 root root  48 2005-12-10 18:14 b
-rw-r--r--  1 root root  0 2005-12-10 18:14 c
kimp4:/foo # mv c reallylongfilenameiamgoingtojustrambleonkeepreadingyepyourebored
kimp4:/foo # ls -lha
total 512
drwxr-xr-x  4 root root 176 2005-12-10 18:15 .
drwxr-xr-x  24 root root 592 2005-12-10 18:13 ..
drwxr-xr-x  2 root root  48 2005-12-10 18:13 a
drwxr-xr-x  2 root root  48 2005-12-10 18:14 b
-rw-r--r--  1 root root  0 2005-12-10 18:14 reallylongfilenameiamgoingtojustrambleonkeepreadingyepyourebored
kimp4:/foo # mv b folderisrenamedtoareallylongnamesoiwilljustkeepontypingtotakeupspaceseethesizeofdotnowitshuge
kimp4:/foo # ls -lha
total 512
drwxr-xr-x  4 root root 264 2005-12-10 18:25 .
drwxr-xr-x  24 root root 592 2005-12-10 18:13 ..
drwxr-xr-x  2 root root  48 2005-12-10 18:13 a
drwxr-xr-x  2 root root  48 2005-12-10 18:14 folderisrenamedtoareallylongnamesoiwilljustkeepontypingtotakeupspaceseethesizeofdotnowitshuge
-rw-r--r--  1 root root  0 2005-12-10 18:14 reallylongfilenameiamgoingtojustrambleonkeepreadingyepyourebored

Now, without checking the source for ReiserFS or ls, one can make an educated guess that what is reported by the directory size is the size of the directory itself - that is, the size of the directory name and its contents' names and attributes.

Believe it or not, each directory entry you make takes up disk space - more on some filesystems than on others. Take FAT for example: if you create a new, empty directory that directory takes up space in the file allocation table. If the filename is longer than a FAT-16 8.3 filename, behind the scenes that directory is broken up into multiple directory entries (a FUGLY hack, I might add) and that all takes up at LEAST one cluster (anywhere from 512bytes to 32KB depending on formatting options). EXT2 works in a similar way - each directory takes up a cluster, plus there needs to be meta-data stored, that is, the directory's timestamp, permissions, etc. not to mention the mappings to the files that directory contains. Some other filesystems are a little more efficient and store this info in a b-tree structure (ReiserFS, XFS, and if I recall correctly UFS as well) which resmbles an RDBMS more closely than a conventional filesystem.

I hope this helped shed some light on things rather than confused you more. Filesystems are actually fairly complex structures.

Milosevic 12-10-2005 05:52 PM

file size.
 
Code:

milosevic@slackws:~$ echo a > file.txt
milosevic@slackws:~$ ls -l file.txt
-rw-r--r--  1 milosevic users 2 2005-12-10 21:49 file.txt

If every file takes up at least one cluster of the file system, why the hack the "file.txt" only takes one byte? (i believe the cluster size here is 4k).

Cheers, Milosevic.

KimVette 12-10-2005 05:59 PM

Well some filesystems (ReiserFS) are "zero-slack" which means that if a file is 1 byte, it takes one byte (well, plus a couple of delimiters). Others (ext2, uncompressed NTFS, FAT) require that whether file is 1 byte or 511 bytes on a filesystem partitioned with 512-byte clusters, that the file be allocated a minimum of one FULL cluster - that is, 512 bytes. The wasted space is usually referred to as "slack"

Why does LS report the file size as one byte? Because the file's contents is one byte. You're confusing file size with disk usage. If you dual boot Windows, here's a quick demonstration:

On an uncompressed FAT or NTFS partition, create an uncompressed file which is say, *picks an abitrary number* 42 ;) bytes. Open up that file's properties, and in the properties tab, check out two entries:

* file size
* space used on disk (I forgot exactly what the two fields are called)

The numbers will be different. One will show the size of the file's contents, and the other will show how much space on the disk is allocated to it. The difference between the two is allocated but unusable space called "slack"


All times are GMT -5. The time now is 09:03 PM.