Find old files using find, du and awk

My friend provides a shared disk space among trusted friends. Everybody are allowed to upload files to and download files from this space. The problem is that free space run out so fast. I have to help him identify oldest files to remove. If you are on *nix-based operating system, you might know about "find", "du" and "awk". To simplify this tip, I will demonstrate how to use find effectively.

  • You want to find files modified exactly 7 days ago.
    find . -ctime 7
  • You want to find files modified 7 days ago or older.
    find . -ctime +7
  • If you want to discard files inside sub-directories, you may use -maxdepth option.
    find . -maxdepth 1 -ctime 7
    find . -maxdepth 1 -ctime +7
  • In addtion, you might want to know how big they are for each entry you found.
    find . -maxdepth 1 -ctime +7 -exec du -s ’{}’ \;
  • Below command is an extension to summarize spaces taken by all old files.
    find . -maxdepth 1 -ctime +7 -exec du -s ’{}’ \;|awk ’{ i = i+int($1); } END { print i; }’

Good luck!

Technorati Tags: , , , , , , , , ,

Post new comment