File Size Split up based on Month

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers File Size Split up based on Month
# 1  
Old 01-11-2018
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
# 2  
Old 01-11-2018
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.
# 3  
Old 01-11-2018
I have a directory named Unix. Inside the Unix directory , there are seven folders like below

Code:
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.

Last edited by rbatte1; 01-11-2018 at 01:00 PM.. Reason: Added CODE tags
# 4  
Old 01-11-2018
Hi,

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

Code:
#!/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.

Code:
$ 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.
# 5  
Old 01-11-2018
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.
# 6  
Old 01-11-2018
Using Red Hat Linux OS
# 7  
Old 01-12-2018
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To Split the file based on column value

Hi Team, I have a requirement in such a way that need to split the file into two based on which column particular value appears.Please find my sample file below. Lets consider the delimiter of this file as either comma or two colons.(:: and ,). So I need to split the file in such a way that all... (2 Replies)
Discussion started by: ginrkf
2 Replies

2. Shell Programming and Scripting

Split the File based on Size

I have a file that is about 7 GB in size. The requirement is I should split the file equally in such a way that the size of the split files is less than 2Gb. If the file is less than 2gb, than nothing needs to be done. ( need to done using shell script) Thanks, (4 Replies)
Discussion started by: rudoraj
4 Replies

3. UNIX for Dummies Questions & Answers

Split file based on column

i have file1.txt asdas|csada|130310|0423|A1|canberra sdasd|sfdsf|130426|2328|A1|sydney Expected output : on eaceh third and fourth colum, split into each two characters asdas|csada|13|03|10|04|23|A1|canberra sdasd|sfdsf|13|04|26|23|28|A1|sydney (10 Replies)
Discussion started by: radius
10 Replies

4. Shell Programming and Scripting

Split the file based on pattern

Hi , I have huge files around 400 mb, which has clob data and have diffeent scenarios: I am trying to pass scenario number as parameter and and get required modified file based on the scenario number and criteria. Scenario 1: file name : scenario_1.txt ... (2 Replies)
Discussion started by: sol_nov
2 Replies

5. Shell Programming and Scripting

Split the file based on column

Hi, I have a file sample_1.txt (300k rows) which has data like below: * Also each record is around 64k bytes 11|1|abc|102553|125589|64k bytes of data 10|2|def|123452|123356|...... 13|2|geh|144351|121123|... 25|4|fgh|165250|118890|.. 14|1|abc|186149|116657|......... (6 Replies)
Discussion started by: sol_nov
6 Replies

6. Shell Programming and Scripting

Split file based on file size in Korn script

I need to split a file if it is over 2GB in size (or any size), preferably split on the lines. I have figured out how to get the file size using awk, and I can split the file based on the number of lines (which I got with wc -l) but I can't figure out how to connect them together in the script. ... (6 Replies)
Discussion started by: ssemple2000
6 Replies

7. Shell Programming and Scripting

Split a file based on pattern and size

Hello, I have a large file (2GB) that I would like to split based on pattern and size. I've used the following command to split the file (token is "HELLO") awk '/HELLO/{i++}{print > "file"i}' input.txt and the output is similar to the following (i included filesize in KB): 10 ... (2 Replies)
Discussion started by: jl487
2 Replies

8. Shell Programming and Scripting

Split file based on size

Hi Friends, Below is my requirement. I have a file with the below structure. 0001A1.... 0001B1.. .... 0001L1 0002A1 0002B1 ...... 0002L1 .. the first 4 characters are the sequence numbers for a record, A record will start with A1 and end with L1 with same sequence number. Now the... (2 Replies)
Discussion started by: diva_thilak
2 Replies

9. UNIX for Dummies Questions & Answers

split files based on size

I have a few txt files in some directory and I need to check their sizes one by one. If any of them are greater than 5mb then I need to split the file in two. Can someone help? Thanks. (6 Replies)
Discussion started by: khanvader
6 Replies

10. Shell Programming and Scripting

Split a large file with patterns and size

Hi, I have a large file with a repeating pattern in it. Now i want the file split into the block of patterns with a specified no. of lines in each file. i.e. The file is like 1... 2... 2... 3... 1... 2... 3... 1... 2... 2... 2... 2... 2... 3... where 1 is the start of the block... (5 Replies)
Discussion started by: sudhamacs
5 Replies
Login or Register to Ask a Question