Delete files older than 30 days
Remove files whose last-modified time is more than 30 days ago, recursively.
How to delete files older than 30 days in each shell
Bashunix
find . -type f -mtime +30 -delete`-delete` is silent. Dry-run first by replacing `-delete` with `-print`.
Zshunix
find . -type f -mtime +30 -deleteFishunix
find . -type f -mtime +30 -deletePowerShellwindows
Get-ChildItem -Recurse -File | Where-Object LastWriteTime -lt (Get-Date).AddDays(-30) | Remove-ItemAdd `-WhatIf` to `Remove-Item` to see what would be deleted; add `-Confirm` to be asked per file.
cmd.exewindows
forfiles /S /D -30 /C "cmd /c del @path"`/D -30` selects files at least 30 days old. Use `/D -30 /C "cmd /c echo @path"` first to preview.
Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.
Gotchas & notes
- `find -mtime +30` matches files **older than 30 days** (mtime). `-mtime 30` is exactly the 30-day-old slice; `-mtime -30` is younger than 30 days. Off-by-ones bite here.
- Always dry-run: `find ... -print` (bash), `Remove-Item -WhatIf` (PowerShell), or replace `del` with `echo` in the `forfiles` `/C` body.
- On networked / case-insensitive filesystems, `-delete` may follow a slightly different walk order than the matching `-print` — never assume "print then delete" is atomic. Use `find ... -mtime +30 -print -delete` to log as it deletes.
- PowerShell `Remove-Item -Force -Recurse` is required if the file is read-only or inside a hidden directory.