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.

Azarilh
  • 60
  • 7

1 Answers1

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.
Kamil Maciorowski
  • 69,815
  • 22
  • 136
  • 202