2

I have this very silly and very basic question but I thought better to ask before I start pruning my NAS.

I want to delete all directories in which all files are older than 30 days.

For files I could have done:

find /path/to/files* -mtime +30 -exec rm {} \;

But this would lead to empty folders at some places. So I was thinking to rather delete all folders with -mtime +30 but I am not sure if the mtime of folder guarantees mtime threshold for it sub folders and files.

Also, if what I am think is right, what would be the command for the same?

1 Answers1

3

On linux directory modification time change if you:

  • add a new file
  • remove an existent a file
  • or rename a file in it

If you change content of existent file, this is not reflected up to directory.

Because of this, I suggest to use these commands:

    find /path/to/folder -type f -mtime +30 -delete

this command remove recursively file older than 30 days and preserve folder.

    find /path/to/folder -type d -empty -delete

this remove empty directory if any.

Source:

Directory last modified date

How do I delete all empty directories in a directory from the command line?

Lety
  • 6,039
  • 2
  • 29
  • 37