1

i was wondering how to auto-delete old files in a certain folder but i want it to exclude its subfolders. I know how to automate the process and i know how to delete old files including subfolders:

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

To automate i just let it open at startup once.

  • And how to add more folders to search for? Just copy paste the same line? I mean in the same script, ofc. – Azarilh Jan 10 at 3:12
  • How about something like: find /path/to/files/* -maxdepth 0 -type f -mtime +30 -exec rm {} \; – Pimp Juice IT Jan 10 at 3:14
2

From man 1 find

-maxdepth levels Descend at most levels (a non-negative integer) levels of directories below the command line arguments. -maxdepth 0 means only apply the tests and actions to the command line arguments.

find /path/to/dir1 /path/to/dir2 /foo/bar/fileA -maxdepth 1 -type f -mtime +30 -exec rm {} +

Notes:

  • Sole rm works well with files, not directories, and you want only files to be deleted, hence -type f.
  • -exec rm {} \; was replaced by -exec rm {} +. Consider -delete (see this answer of mine).
  • If your find doesn't support -maxdepth then see how to limit POSIX find to specific depth.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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