[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: A question about ls's behaviour (was: A question about rsync's behaviour)



Rodolfo Medina wrote:
> But, now, another strange behaviour from `ls' command:

The ls command is simply reporting what it is reading from the
filesystem.  This isn't strange ls behavior but normal ls behavior. :-)

> just after copying `~/Mail' to `/mnt/pendrive/Mail', I have:
> 
> $ ls -ld Mail
> drwxr-xr-x 92 rodolfo rodolfo 4096 2010-08-12 17:17 Mail
> 
> $ ls -ld /mnt/pendrive1/Mail
> drwxr-xr-x 92 rodolfo rodolfo 8192 2010-08-12 17:17 /mnt/pendrive1/Mail
> 
> Why the difference between 4096 and 8192??

Short Answer: Ignore sizes of directories.

Longer Answer: The size of the directory is dependent upon several
factors.

One is the type of filesystem.  Different filesystems will consume a
differing amount of space.  If your pendrive is VFAT and your Debian
system is ext3 then the implementation of the directory will cause it
to be different sizes.

Secondly it depends upon the history of how many files were in the
directory previously.  This behavior has changed over the last few
decades with the addition of btrees and indexes to directories.  But
you can still demonstrate it today.

  $ mkdir testdir
  $ ls -ldog testdir
  drwxrwxr-x 2 4096 2010-08-12 17:59 testdir
  $ for i in $(seq 1 10000); do touch testdir/$i; test $(stat --format %s testdir) -ne 4096 && break; done
  $ ls -ldog testdir
  drwxrwxr-x 2 12288 2010-08-12 17:59 testdir
  $ ls testdir | wc -l
  340
  $ rm -f testdir/*
  $ ls testdir | wc -l
  0
  $ ls -ldog testdir
  drwxrwxr-x 2 12288 2010-08-12 18:01 testdir

On my Lenny system a newly created directory on ext3 will be 4k in
size.  As files are added eventually it needs to be increased to a
larger size and jumps to 12k after adding 340 files.  This size is not
decreased even after removing the files.  The exact size of
directories and the number of files that can be stored there will
differ on different filesystems and on different operating systems.

Here is the same experiment on ext2.

  $ mkdir testdir
  $ ls -ld testdir
  drwxr-xr-x 2 root root 1024 Aug 12 18:04 testdir
  $ ls -ldog testdir
  drwxr-xr-x 2 1024 Aug 12 18:04 testdir
  $ for i in $(seq 1 10000); do touch testdir/$i;
  test $(stat --format %s testdir) -ne 1024 && break; done
  $ ls -ldog testdir
  drwxr-xr-x 2 2048 Aug 12 18:04 testdir
  $ ls testdir | wc -l
  84
  $ rm -f testdir/*
  $ ls -ldog testdir
  drwxr-xr-x 2 2048 Aug 12 18:04 testdir

On ext2 the smallest directory size is 1k and jumps to 2k after 84
files have been added in this test.

Bob

Attachment: signature.asc
Description: Digital signature


Reply to: