I know a command to do the same with files, but what about folders (and all the files in it of course)?
3 Answers
The command you refer is find
I suppose.
You should use the command -exec rm -r {} \;
and add the -depth
option.
The -r
option to rm
remove directories with all the content.
The -depth
option tell find
to elaborate content of folders before the folder itself.
-
That's almost perfect, thanks. Is there a way so it doesn't delete the actual folder I'm searching in? So only folders within the folder get deleted? I'm currently doing "find "/home/user/folder/" -mtime +7 -exec rm -r {} \;", but it deleted the folder called "folder" as well if the time matches. I only want subfolders deleted older than 7 days.– nLinkedDec 10, 2011 at 19:24
-
1
find ./dirc/* -mtime +x -type f -delete
./dirc/*
: is your directory (Path)-mtime +x
: older than x days-type f
: only files-delete
: no surprise. Remove it to test before like rm
-
It seems that you missed that the OP explicitly asked for deletion of folders, not files.– Manu CJNov 20, 2020 at 10:29
Think that’s not realy true, because if you change or create a file in this folder before you try to remove it, it will not be removed. Because mtime is the modification time and the modification will be done at this time you create or change a file in this directory. Afaik but let me know if I’m false at Linux there is no parameter for creation time.