0

I want to find and delete non empty directories greater than 3 days.

find . <Path> -mtime +3 -exec rm -rf "{}" \;

I want to delete directories that have files in it and the directory and files are older than 3 days.

/tmp
drwxr-x--- 2 root root 4096 Nov  6 05:05 20191106
drwxr-x--- 2 root root 4096 Nov  7 05:05 20191107
drwxr-x--- 2 root root 4096 Nov  8 05:05 20191108
drwxr-x--- 2 root root 4096 Nov  9 05:05 20191109
drwxr-x--- 2 root root 4096 Nov 10 05:05 20191110
drwxr-x--- 2 root root 4096 Nov 11 05:05 20191111
drwxr-x--- 2 root root 4096 Nov 12 05:05 20191112
drwxr-x--- 2 root root 4096 Nov 13 05:05 20191113
drwxr-x--- 2 root root 4096 Nov 14 05:05 20191114
drwxr-x--- 2 root root 4096 Nov 15 05:05 20191115
drwxr-x--- 2 root root 4096 Nov 16 05:05 20191116
drwxr-x--- 2 root root 4096 Nov 17 05:05 20191117
drwxr-x--- 2 root root 4096 Nov 18 05:05 20191118
drwxr-x--- 2 root root 4096 Nov 19 05:05 20191119

So here greater than 3 days dir. Each directory has some text file.

  • 1
    And what is the question or problem? – Eduardo Trápani Nov 22 '19 at 16:25
  • am unable to find and delete non empty folders. – user383228 Nov 22 '19 at 16:27
  • 1
    find . -mindepth 1 -maxdepth 1 -not -empty -type d will return all all nonempty directories. From here: stackoverflow.com/a/815693/2152558 – Paulo Tomé Nov 22 '19 at 16:31
  • 1
    @user383228: It's generally best to put more information in your question. Simply saying it doesn't work is of very little use. We normally want to know what about it doesn't work. Does it give an error? Does it not delete enough? Does it delete too much? Etc. Also since the find command can vary greatly from distribution to distribution it's probably best to include your find version. – jesse_b Nov 22 '19 at 16:32
  • 1
    Please define "empty". All directories at level 1 only? When dirs at level 1 are filled with empty subdirs, what should happen? When a dir has only non-empty subdirs and they are deleted, what about the then empty dir? – Fiximan Nov 22 '19 at 16:33
2

Could be something like this:

find . -mindepth 1 -maxdepth 1 -not -empty -type d -mtime +3 -exec rm -rf {} \;

This solution will delete level 1 non empty directories who hasn't been touched for more than three days.

Please backup your data before testing.

  • Note that -mtime +3 is at least 4 days old – Stéphane Chazelas Nov 27 '19 at 10:24
  • You could also mention that unless the -daystart option is specified, the -mtime specification amounts to n*24 hours starting now (imagine running the script at 23:00), which may not be what the OP wants. – AdminBee Nov 27 '19 at 10:31
2

In the zsh shell, the non-empty directories in /tmp with a modification timestamp of more than three days ago would be matching the filename globbing pattern

/tmp/*(/DNFm+3)

The glob qualifier (/DNFm+3) means

  • Only match directories (/; this is not strictly needed as the F qualifier would imply the same),
  • Allow matching hidden names (D; works like the dotglob shell option in bash),
  • Expand to nothing if there is no match (N; works like the nullglob shell option in bash),
  • Only include "full" (non-empty) directories (F),
  • Only include entries that has a modification timestamp of strictly more than three days ago (m+3; for directories, this means that there was last something added or removed from the directory that many days ago; like for find's -mtime +3, that means 4 days old or older).

To list all the matches, use

print -rC1 -- /tmp/*(/DNFm+3)

To delete them, use

rm -rf -- /tmp/*(/DNFm+3)

If there are several thousands of such matches and you're running into argument list too long errors, then use a loop:

for dirpath in /tmp/*(/DNFm+3); do
    rm -rf -- $dirpath
done

Or use zargs:

autoload zargs
zargs -- /tmp/*(/DNFm+3) -- rm -rf

Or enable the rm builtin with zmodload zsh/files which will work around that limitation of the execve() system call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.