size of directory

Hello again;

I have a directories and subdirectories in my current directory and i wanna to find the directories( and subdirectories ) which are larger than what user enters as first parameter.

find . -type d -size +"$1"c -print > directories.dat

I used this command and i am not sure it is working properly.
Because i entered the
./myprog.sh 300
and it found empty directory.

Or i entered the
./myprog.sh 500000
and it found nothing however in the current directory i have directories which are larger than 500000 bytes

i wonder this command are working for directory search or not working

Thanks for your replies..

>cat b2.zsh
echo "find . -type d -maxdepth 1 -size +$1k -print" | zsh
>./b2.zsh 3

Hmm..it works fine for me..that's right command..

if this command does not success, why?
find . -type d -size +"$1"c -print > directories.dat

i cannot understand your code in addition i wrote this in my program and i does not work too.
wanna know that why this code is not working properly and where am i wrong?And how can i fix it?

find . -type d -size +"$1"c -print > directories.dat

have many directories and subdirectories in my current directory and i wanna list them (which are bigger) in directories.dat file.
That is all :slight_smile:

just execute manually..not through script and give a try..

find . -type d -size +<size>c -ls

You folks may not be on the same page here....

A directory entry is a name and an inode number. A directory is a file that contains a collection of these entries. If I create a new directory, it will be rather small. If I cd into it and create 5 100GB files, my directory continues to be small... after all it only has 7 entries at this point.

Please clarify if you are discussing the size of a directory or the collective total of disk space used by a directory and all files and subdirectories contained within it.

The command:

find . -type d -size +"$1"c -print

searches for directories for which the data block which contain the information of the directory have a size greater than the value as specified by the argument.

The command provides no information about the size of the files located within the directory.

It checks for the size as reported by the "ls -l" command

#ls -l
drwxr-xr-x  10 user     group       11264 Feb 27 03:10 dir1
drwxr-xr-x  10 user     group          512 Feb 28  2006 dir2
drwxr-xr-x   2 user     group         1536 Feb 27 04:00 dir3
drwxr-xr-x   2 user     group          512 Nov 10 16:35 dir4

So the size of:
dir1 = 11264
dir2 = 512
dir3 = 1536
dir4 = 512

find . -type d -size +300c -print

Would report all of these directories.

find . -type d -size +50000c -print

Would report none of these directories.

In neither case it wouldn't provide any information about the size of the files located inside the directory.

When a directory is created it has a default size of 512 (1 block).

When we start creating new files within this directory, at some stage the data block doesn't have enough space any more to store all the information about the files located within it.

The directory will increase in size because it will allocate an extra data block to store the information of files located within it.

The size of the directory will then show as 1024.

In the example above, dir1 has a size of 11264 (22 blocks). This means that at some stage an enormose amount of files were located within this directory.

When the number of files within a directory decreases, and the directory wouldn't need as many blocks any more to store the information about the remaining files, the size of the directory remains unchanged. No blocks are released. The data blocks will just have a lot of "empty" space.

So a directory with a big size doesn't need to contain a lot of files. However at some stage it did contain a lot of files. At the present the directory could even be empty.

To calculate the size of all files within a directory we will need a different approach.

du -ok .

reports the size of all directories below . in kbytes

Your command actually reports the size of your current directory if the size is greater than specified and NOT the size of the files/sub directories within it. Remember that the size of dir is different from the size of files within it.

But when you specify "depth" option with find, it acts on the contents of the directory first. Thats why you get the result.

if we have a directory structure:

dir1/dir2/dir3

then

"find" without the "-depth" option will act on the directories in this order:

dir1
dir1/dir2
dir1/dir2/dir3

"find" with the "-depth" option will act on the directories in this order:

dir1/dir2/dir3
dir1/dir2
dir1

However in both scenarios we get information about the size of data blocks related to the directory and NOT of the files located within it.

The "-depth" option is irrelevant and will give wrong results either way, just in a different order.

After all the option "-type d" is specified, so files are totally ignored.

Leaving out the option "-type d", will report about files as well, however on an individual base, and not about the total size of all files within a specific directory or tree.

So using the option "-type d" will not change the outcome.

Agreed. Was just explaining him why his code doesn't work. Using "du -sk" or so is the best way to get the correct result.

Hello friends,
Thanks to all because of your complete and perfect explanations.
have been for 3days. And i have learnt many things( of course little things ) and i like linux and shell programming.

Well, does "du" have size comparision option? i looked at the "man du" but i didn not see any.
Because i wanna compare the size of directories( with subdirectories ) with the value as specified by the argument.

You will need to get the "du -sk *" for all the dirs and files and then use it in a for loop or so to compare the size.

But it did'n return the subdirectories?

Depending your Unix version use "du -ok" if it supports the "-o" option.

i use Ubuntu and it does not support this command

du: invalid option -- o
Try `du --help' for more information.

Use this to get size directory wise recursively

find . -type d | xargs du -sk

Thank you very much :slight_smile:

Yes you can use this.

May be this helps you .

Used to find directory sizes.... give dir. size in MB:

for i in `ls -lrt | awk '{print $9}'`; do du -ks $i  | awk '{printf "%8.2f %s\n",$1/1024,$2}'; done

EXAMPLE:

# ls -lrt
total 22
drwxr-xr-x   2 root       root            96 Sep 30  2005 lost+found
drwxrwxrwt   2 bin        bin             96 Sep 30  2005 home
dr-xr-xr-x   4 bin        bin             96 Sep 30  2005 obam
dr-xr-xr-x   2 bin        bin             96 Sep 30  2005 parmgr
dr-xr-xr-x   4 bin        bin             96 Sep 30  2005 vx
drwxr-xr-x   5 root       other           96 Sep 30  2005 stm
drwxr-xr-x   3 bin        bin             96 Sep 30  2005 X11
drwxrwxrwx   2 bin        bin             96 Sep 30  2005 news
drwxr-xr-x   2 bin        bin           1024 Sep 30  2005 yp
drwxr-xr-x   2 bin        bin             96 Sep 30  2005 rbootd
drwxr-xr-x   3 root       sys             96 Sep 30  2005 dt
dr-xr-xr-x   6 bin        bin             96 Sep 30  2005 uucp
dr-xr-xr-x  12 bin        bin           1024 Sep 30  2005 spool
dr-xr-xr-x  19 bin        bin           1024 Oct 26  2006 opt
drwxr-xr-x   2 root       sys             96 Sep 11  2007 empty
drwxrwxrwx   2 bin        bin             96 Nov 29  2007 preserve
lrwxr-xr-x   1 root       sys             19 Mar 17 17:36 symapi -> /usr/emc/API/symapi
drwxr-xr-x   2 root       root          1024 Jun 13 12:20 ECC
drwxr-xr-x   2 root       sys             96 Jun 20 12:02 emc
dr-xr-xr-x   9 bin        bin           1024 Jul  6 18:58 sam
drwxr-xr-x   2 root       root          1024 Jul  6 19:01 tombstones
dr-xr-xr-x   7 root       sys           1024 Aug  1 01:04 PSS
drwxrwxr-x   2 bin        mail          1024 Aug  6 00:12 mail
dr-xr-xr-x   3 bin        bin           1024 Aug  7 23:59 run
drwxr-xr-x  12 adm        adm           1024 Aug  8 08:47 adm
drwxrwxrwt  10 bin        bin           1024 Aug  8 10:06 tmp
eux181{root}#
eux181{root}# for i in `ls -lrt | awk '{print $9}'`; do du -ks $i  | awk '{printf "%8.2f %s\n",$1/1024,$2}'; done | sort -2
    0.00 ECC
    0.00 X11
    0.00 dt
    0.00 emc
    0.00 empty
    0.00 home
    0.00 lost+found
    0.00 news
    0.00 obam
    0.00 parmgr
    0.00 preserve
    0.00 rbootd
    0.00 run
    0.00 symapi
    0.00 uucp
    0.04 yp
    0.06 vx
    0.14 tmp
    0.38 spool
    0.73 mail
    1.27 sam
    3.47 tombstones
   11.95 PSS
   21.79 stm
 1842.21 adm
 3905.45 opt