File Size Split up based on Month

Hi,

I have a directory in Unix and there are folders available in the directory.

Files are created on different month and now i have a requirement to calculate size of the folder on month basis.

Is there any Unix command to check this please??

Thanks

Hi,

Ideally, we'd need a bit more information here to help you. You mention the files are created on a different month, and you need to calculate the size of the folder on a month basis. Does this mean you just need to calculate the total size of the folder each month ? Or rather do you just want to get a total or the files and folders that have been added to that directory in the last month ? Do the files and folders have consistent names with a time or date as part of the name, or can they be called anything ?

If you can explain a bit more about what you actually mean here, and whether you just need to work out the size of the entirely directory and all its contents on a monthly basis or whether you have to limit it to working out the size of only those things added since the last check, then we can probably go on from there and help you out.

I have a directory named Unix. Inside the Unix directory , there are seven folders like below

drwxrwxr-x    4    teacher   class   2048  Jul 16 17.56  XXX
drwxr-xr-x    60   root                 1536  Jul 13 14:18   YYY
drwxrwxr-x      1    teacher   class   4210  May 1 08:27   AAA
drwxrwxr-x    1    teacher   class    1948  May 12 13:42  BBB 
drwxrwxr-x      1    teacher   class   4210  Aug 1 08:27   CCC
drwxrwxr-x    1    teacher   class    1948  Aug 12 13:42  DDD
drwxrwxr-x   1    teacher   class    1948  Sep 12 13:42  ZZZ

If the see the above example, there are seven folders in the Unix directories.

I want the output as , For

July - (Size of the folder)
May - (Size of the folder)
Aug - - (Size of the folder)
Sep - (Size of the folder)

the size should contains the size of the subfolders and the files.

Hi,

OK, thanks, I think I get what you're needing. Try something like this:

#!/bin/bash

if [ "$1" == "" ]
then
        echo "You must provide a month and year, e.g.:"
        echo "Jan-2017, Aug-2015, etc"
        exit 1
else
        date="$1"
fi

if [ "$2" == "" ]
then
        echo "You must provide a directory to generate a report for, e.g.:"
        echo "/home/unixforum/276398"
        exit 1
else
        reportroot="$2"
fi

if ! [ -d "$reportroot" ]
then
        echo "Your specified report root does not exist, try again."
        exit 1
fi


echo "Disc usage within "$reportroot" for "$date":"

for directory in `/usr/bin/find "$reportroot" -mindepth 1 -maxdepth 1 -type d -newermt "01-$date -1 sec" -and -not -newermt "01-$date +1 month -1 sec" -print`
do
        /usr/bin/du -sb "$directory"
done

Here's a sample session of it being run with identical directories to those provided in your example.

$ ls -l
total 32
drwxr-xr-x 2 unixforum users 4096 May  1  2017 AAA/
drwxr-xr-x 2 unixforum users 4096 May 12  2017 BBB/
drwxr-xr-x 2 unixforum users 4096 Aug  1 00:00 CCC/
drwxr-xr-x 2 unixforum users 4096 Aug 12 00:00 DDD/
drwxr-xr-x 2 unixforum users 4096 Jul 16 00:00 XXX/
drwxr-xr-x 2 unixforum users 4096 Jul 13  2017 YYY/
drwxr-xr-x 2 unixforum users 4096 Sep 12 00:00 ZZZ/
-rwx------ 1 unixforum users  663 Jan 11 15:13 report.sh*
$ ./report.sh May-2017 /home/unixforum/276398
Disc usage within /home/unixforum/276398 for May-2017:
4096    /home/unixforum/276398/BBB
4096    /home/unixforum/276398/AAA
$ ./report.sh Aug-2017 /home/unixforum/276398
Disc usage within /home/unixforum/276398 for Aug-2017:
4096    /home/unixforum/276398/CCC
4096    /home/unixforum/276398/DDD
$ ./report.sh Jul-2017 /home/unixforum/276398
Disc usage within /home/unixforum/276398 for Jul-2017:
4096    /home/unixforum/276398/XXX
4096    /home/unixforum/276398/YYY
$ ./report.sh Sep-2017 /home/unixforum/276398
Disc usage within /home/unixforum/276398 for Sep-2017:
4096    /home/unixforum/276398/ZZZ
$ 

So basically as long as you provide the month and year as MMM-YYYY, and the full path to the directory the report is to be generated for, then you'll be fine. If that directory never changes you could hard-code these as variables and do away with having to provide them, but this makes it as interactive as possible as it currently stands.

Hope this helps.

Good thing to mention that some of find command options are exclusive to gnu find.
You haven't mentioned your operating system, so a script provided may or may not work.

Also, there are strict forum rules regarding questions which involve educational institutions.
Since your output contains teacher and class, one would suspect you are not following the forum rules.

Regards
Peasant.

Using Red Hat Linux OS

Hi,

In that case (assuming it's not some ancient version of Red Hat) the script I've provided should run fine. Did it do what you needed ? If not, then if you could explain what about it doesn't meet your needs I'd be happy to have another crack at helping.