Tuesday, September 16, 2025

the find command and mtime - finding disk space used by old files

 Asked ChatGPT, "linux commandline to list total disk space used of all files created or modified inside a directory more than one year ago" and got the following - 

You can do this with find + du or find + stat + awk.
Here’s a one-liner that lists the total disk space used by files in a directory older than one year:

find /path/to/dir -type f -mtime +365 -printf "%s\n" | \
awk '{sum+=$1} END {print sum/1024/1024 " MB"}'

Explanation:

  • find /path/to/dir → search inside that directory.

  • -type f → only files.

  • -mtime +365 → modified more than 365 days ago (roughly one year).

  • -printf "%s\n" → print file sizes in bytes.

  • awk → sums up sizes and prints total in MB (divide by 1024/1024).

If you prefer human-readable output directly:

find /path/to/dir -type f -mtime +365 -print0 | du --files0-from=- -ch | tail -n1

This gives you a total in du’s human-readable format.





 Asked ChatGPT, "tree view of files and directories larger than 1 GB from linux command line" and got the following - 

You can combine find, du, and tree-style output to list only files/directories larger than 1 GB in a tree view. Here are a couple of approaches:


1. Using find with tree

find /path/to/search -type f -size +1G -print | tree --fromfile
  • find ... -size +1G → finds files > 1 GB

  • tree --fromfile → prints a tree structure from the list

If you want directories too (based on contents size), use du instead (see below).


2. Using du for directories/files with sizes

du -h --threshold=1G --max-depth=3 /path/to/search | sort -h
  • --threshold=1G → only show entries larger than 1 GB

  • --max-depth=3 → how deep to go

  • sort -h → human-readable sort

This shows sizes and paths (not tree formatting, but size summary is clearer).


3. Pretty tree-like display with du

du -h --threshold=1G --max-depth=3 /path/to/search | sort -hr | 
awk '{print $2}' | tree --fromfile

This gives you a tree structure of directories/files whose total size is ≥ 1 GB.


✅ If you want only the largest offenders (say top 20 big files/directories over 1 GB), you can do:

find /path/to/search -type f -size +1G -exec du -h {} + | sort -hr | head -20

For this, I had to sudo apt install tree

No comments:

Post a Comment