Shell script to find size of subdirectories

Hi
I have to find size of subdirectory and automate it in CRON.

Eg: parent directory name is NVBKP inside it there are several subdirectories
I want to get the size of recent two subdirectories.

I have tried

ls -ltr diretory path | tail -2 

But it is providing only size of the folder not the contents of that folder.

For eg if there are 5 subdirectories in NVBKP say

a
b
c
d
e

then i need complete size of d and e.
say complete size of d and e is 25 gb
but when i am using

ls -ltr directory path| tail -2

it is giving me only size of folder d and e i.e 256 mb or something but not its contents.

Kindly suggest.

try

ls -lRt

and see

wrong forum, moving thread...

Hi,
Try this one,

ls -ltr|awk -v l=2 '/^d/{c++;a[c]=$9;}END{for(i=c-l+1;i<=c;i++){print a;}}'|xargs -I '{}' du -sh {}

Cheers,
Ranga:-)

Hi
Thanks for the reply

ls -ltr|tail -2|xargs -I '{}' du -sg {}

.Its giving the following result.

du: drwxr-xr-x    3 oranvd   dba             256 Dec 16 03:30 20121216: A file or directory in the path name does not exist.
du: drwxr-xr-x    3 oranvd   dba             256 Dec 23 03:30 20121223: A file or directory in the path name does not exist.

Its not giving the desired result these folders has files and the size of the same different but its showing only 256 that is not correct.

Hi,

Try this one,

cd dir;
ls -ltr|awk -v l=2 '/^d/{c++;a[c]=$9;}END{for(i=c-l+1;i<=c;i++){print a;}}'|xargs -I '{}' du -sh {}

You can specify the n number of latest files which you want in variable l.

Cheers,
Ranga:)

Thankyou so much Ranga
It worked.
Thanks a lot
Cheers..... :slight_smile:

$ find . -type d -mmin -30| tail -2 | xargs du -h
7.0K    ./d4
7.0K    ./d5

hi want to schedule a script for displaying the size of the partiition.

For example

df -g

/dev/fslv03 2000.00 535.43 74% 179943 1% /nvbkp

we have several mount point like this but i want to schedule script to display size of the above mount point only.

Thanks
Ankit