Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculate total used disk space by files older than 180 days using find

I am trying to find the total disk space used by files older than 180 days in a particular directory. This is what I'm using:

    find . -mtime +180 -exec du -sh {} \; 

but the above is quiet evidently giving me disk space used by every file that is found. I want only the total added disk space used by the files. Can this be done using find and exec command ?

Please note I simply don't want to use a script for this, it will be great if there could be a one liner for this. Any help is highly appreciated.

like image 824
dig_123 Avatar asked Jul 02 '13 06:07

dig_123


People also ask

How do I find files older than one year in Linux?

You could start by saying find /var/dtpdev/tmp/ -type f -mtime +15 . This will find all files older than 15 days and print their names. Optionally, you can specify -print at the end of the command, but that is the default action.

Which files in the home directory which were modified more than 120 days ago?

Use “-mtime n” command to return a list of files that were last modified “n” hours ago.


2 Answers

Why not this?

find /path/to/search/in -type f -mtime +180 -print0 | du -hc --files0-from - | tail -n 1 
like image 87
user2059857 Avatar answered Sep 20 '22 03:09

user2059857


du wouldn't summarize if you pass a list of files to it.

Instead, pipe the output to cut and let awk sum it up. So you can say:

find . -mtime +180 -exec du -ks {} \; | cut -f1 | awk '{total=total+$1}END{print total/1024}' 

Note that the option -h to display the result in human-readable format has been replaced by -k which is equivalent to block size of 1K. The result is presented in MB (see total/1024 above).

like image 24
devnull Avatar answered Sep 21 '22 03:09

devnull