Operating System - HP-UX
1837672 Members
3493 Online
110117 Solutions
New Discussion

finding directories of certain size

 
SOLVED
Go to solution

finding directories of certain size

I need to list only the directories (current directory and it's all subdirectories, actualy recursively) which are larger than a given size.

i tried
find . -type d -size +$1c -print
but it doesn't work.

i have a hint that it may be done with du command but no idea how.

anyone has an idea?
10 REPLIES 10
James R. Ferguson
Acclaimed Contributor
Solution

Re: finding directories of certain size

Hi:

By example:

# du -x /path|awk '{if ($1 > 10000) {print}}'

...would find all directories whose size in 1K *blocks* is greater than 10,000.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: finding directories of certain size

First, you need to define what "size" means. As you used it above it refers to the number of bytes in the directory itself (not the size of the files). Your fundamental problem above is missing {}'s.

find . -type d -size +${1}c
should work, proovided that you pass the size as ${1} into your script.

You may want the total size of the fimes in a directory and for that indeed, du is more appropriate but you will have to apply an additional filter to limit the sizes. Note that du reports in 512-byte blocks.
If it ain't broke, I can fix that.

Re: finding directories of certain size

no. the find command does not work properly for the directories. it is working only for the regular files. let me rewrite it properly.

i will give the size as a parameter to the program. let's assume that size type is bytes.

i will run the program like:
./program.sh 10000

and the program's output will be only the directory's names, which are larger than 10000 bytes. the output shall not include the sizes.

the search for the directories should start from the current directory and recursively search all other subdirectories...

the find command does not work properly so i need a script with du command.
A. Clay Stephenson
Acclaimed Contributor

Re: finding directories of certain size

I would probably use Perl's File::Find module but the above du example is rather close.

#!/usr/bin/sh

typeset -i STAT=${?}
typeset PROG=${0}
if [[ ${#} -ge 1 ]]
then
du | awk -v sz=${1} '{if (($1 + 0) * 512 >= sz) {print $2}}'
STAT=${?}
else
echo "Usage: ${PROG} size" >&2
STAT=255
fi
exit ${STAT}

Note that the actual granulariry is 512 bytes -- but how much do you want for free?

If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: finding directories of certain size

Hi (again):

> the find command does not work properly so i need a script with du command

See my original suggestion using 'du' and an 'awk' pipe, above.

Regards!

...JRF...

Re: finding directories of certain size

mister ferguson, thanks for your help.

but there are some points I do not undertand.

du -x /path|awk '{if ($1 > 10000) {print}}'

is it possible if I don't write the /path?
because program may run in different directories, not in a fixed directory and it's subdirectories...

and I don't understand what the parameter $1 stands for. in my program, I need to enter size as a paramater to the $1. if I enter 12000 the statement is ...if(5000 > 10000) and this doesnt make any sense. can you help please?

Re: finding directories of certain size

sorry if enter 12000 the stament will be
...if(12000 > 10000)... :=)

I need a list of all directory names which are largen than 12000 byte, from the current directory to all subdirectories, recursively...
James R. Ferguson
Acclaimed Contributor

Re: finding directories of certain size

Hi:

Clay and I have offered the same thinking. His script is a bit more refined for continued use. Mine is designed for command line use.

You asked:

> is it possible if I don't write the /path?
because program may run in different directories, not in a fixed directory and it's subdirectories...

Yes, change '/path' to respresent whatever directory you want to begin your search in.

> ...and I don't understand what the parameter $1 stands for.

The "$1" is 'awk's notation for the first field of a record. In the 'du' output piped to it, that's the size (in *1K blocks*).

Clay's script better accomodates your need. He converts the default 512-byte block count to bytes and allows you to pass the threshold as an argument.

Regards!

...JRF...









in my program, I need to enter size as a paramater to the $1. if I enter 12000 the statement is ...if(5000 > 10000) and this doesnt make any sense. can you help please?

Re: finding directories of certain size

thank you all :)

I am really a newbie, started to learn shell a couple of days ago. So I can't use Mr. Clay's code, too complicated for me, but thanks anyway. :)

du -x .|awk '{if ($1 > $2) {print}}' suits for me. I tried it and it seems working fine. I am grateful for your help. Thanks :)...
Cem Tugrul
Esteemed Contributor

Re: finding directories of certain size

Osman Merhaba,

Welcome to itrc and this is the really greatest forum probably that you have never
seen before...i have been learning since
i registered here and still keeping to learn.
Anyway,here is the script normally does not match your actual question but may need such a kind script in the future.
more warn_user.sh
#######################################################################
#Kullanicilarin egreksiz dosyalarini silmesi icin uyari mesai atar!!!!#
#Cem Tugrul(1480)!!!BAC-I dept #
#######################################################################
#!/usr/bin/ksh
cd /users;
if [ $? -ne 0 ]
then
echo "/users directory si bulunamadi.."
exit 1
fi
for user in `du -ks * |sort -n|egrep -v "bsp|rvs|hp|oracle|picolog|baycom|pic412|mailuser|htnm"|awk '{if ($1 >= 50000) print $2 }'`
do
echo "LUTFEN BaaN uzerindeki gereksiz dosyalarinizi SILINIZ!!!Bilgi icin--->Cem Tugrul(1480)"|mailx -s "BaaN Kullanimi" $user@mntra
nkmail01
done

#script explanation in Turkish!!!
Yukarıdaki script bsp,rvs vb kullanıcıların dısındaki kullanıcıların home directory lerinin size 50Mb astiginda onları gereksiz dosyları silmeleri konusunda onlara mail atar.

Again Welcome,
Tekrar Hosgeldin,
Good Luck,
İyi Sanslar,
Our greatest duty in this life is to help others. And please, if you can't