2

I know a command to do the same with files, but what about folders (and all the files in it of course)?

3 Answers 3

3

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.

2
  • 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.
    – nLinked
    Dec 10, 2011 at 19:24
  • 1
    Solved using: rm -rf "/target/directory with spaces/"*
    – nLinked
    Dec 10, 2011 at 20:12
0
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

Auto delete files older than 7 days

1
  • It seems that you missed that the OP explicitly asked for deletion of folders, not files.
    – Manu CJ
    Nov 20, 2020 at 10:29
0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .