Delete only Directories (not files) older than x days in Linux

2.9k views Asked by At

I am trying to list the directories (but not files) older than x days. I used below command to do it. But it is listing directories and files. Could anyone help me in resolving it? Thanks.

find /path/to/base/dir/* -type d -ctime +10 -exec ls {} \;
2

There are 2 answers

0
V. Michel On

First to test if all is alright :

find /path/to/base/dir -type d -ctime +10 -exec ls -d {} \;

When all is ok :

find /path/to/base/dir -type d -ctime +10 -exec rm -fr {} \;

Explanations :

ls -d :  list directories themselves, not their contents
rm -f :  ignore nonexistent files and arguments, never prompt
0
Suresh Raju On

Try this

find /path/to/base/dir -maxdepth 1 -type d -ctime +10 -exec rm -r {} \;