0

I have a folder with thousands of large files and I would like to find the total used disk space that was created in the last X days.

I've tried this command:

find . -type f -mtime -30 -printf '%s\n' | awk '{total=total+$1}END{print total/1024}'

As mentioned here by @k-h
calculate total used disk space by files older than 180 days using find

The output of the command is 9.84347e+09 and I'm not sure how to change the output to TB disk size.

Please advise.
Thanks,
Yaron

yarone
  • 111
  • 3
  • This might help: https://unix.stackexchange.com/questions/44040/a-standard-tool-to-convert-a-byte-count-into-human-kib-mib-etc-like-du-ls1 – Sundeep Jun 08 '20 at 06:48
  • Are you interested in the file-size or the disk-space ... these are two different things. – kvantour Jun 08 '20 at 07:39
  • 1
    Hi and welcome to Stack Overflow. You would like to have your results printed in TB and you have your hands on a command that prints the results in kB (`%s` returns bytes (see `man find`) ) ... So it seems to me that this is a straightforward task. I'm very convinced you can handle this. – kvantour Jun 08 '20 at 09:28
  • ill try @kvantour, thanks! – yarone Jun 10 '20 at 20:16

1 Answers1

0

Well not quite the same logic but I'm here to explain:

{ echo -n \(; find -type f -mtime -30 -printf "%s+"; echo 0\)/1024/1024/1024/1024; } | bc

Let's break this down:

  • echo -n \( - prints ( without a line break.
  • find … - that same ol' command you used with the + sign for calculation.
  • echo 0\)/1024… - we're printing 0 so the expression won't end with a + sign, print the closing parentheses and then dividing by 1024 four times to get to the relevant result.
  • | bc - this is the calculation tool, you can simply pipe the formula to this tool, try running echo 1+2 | bc and play with it to understand its concept.

Have fun, and cool name BTW :)

Yaron
  • 1,199
  • 1
  • 15
  • 35