Delete Old Temp Files
Problem statement
Delete .tmp files older than 7 days, the safe way: first print what would be deleted, then delete, then check what is left. Scheduled cleanup of temp files, caches and old uploads is one of the most common cron jobs on any server, and a wrong find -delete is one of the most common ways to lose data.
The script creates these files and sets their age with touch -d:
tmp/
tmp/a.tmp 10 days oldtmp/b.tmp 3 days oldtmp/c.tmp 8 days oldtmp/keep.log 20 days old (not a .tmp file)tmp/sub/d.tmp 30 days old- Dry run: print the
.tmpfiles older than 7 days. - Delete exactly those files.
- Print every file that is left.
Expected output:
== dry run: what would be deleted ==tmp/a.tmptmp/c.tmptmp/sub/d.tmp== what is left ==tmp/b.tmptmp/keep.logHints
-mtime +7 means "last changed more than 7 full days ago". Combine it with -name '*.tmp' and -type f.Approach
Optimal: find -mtime, dry run, then -delete
Covers: find -name, -type f, -mtime +N, -mmin, -print, -delete, touch -d, why the order of find actions matters.
Look before you delete. There is no undo. The safe habit is always the same two runs:
- Run the full command with
-printat the end. Read the list. - Run exactly the same command with
-deletein place of-print.
How -mtime counts days. Every file has a modification time, the last time its content changed. -mtime compares it with now, in whole days, rounding the age down:
| File | Age | Whole days | -mtime +7 (more than 7)? |
|---|---|---|---|
b.tmp |
3 days | 3 | no |
c.tmp |
8 days | 8 | yes |
a.tmp |
10 days | 10 | yes |
d.tmp |
30 days | 30 | yes |
-mtime 7 means "exactly 7 whole days", -mtime -7 means "less than 7". For minutes, use -mmin +60.
-delete must come last. find runs its tests and actions from left to right for every entry. In find tmp -delete -name '*.tmp', -delete comes first and runs on everything, before the name test is even checked. That command deletes the whole folder. Always put the tests first and -delete at the very end, and quote '*.tmp' so the shell does not expand it.
before any test"]:::red end GOOD ~~~ BAD classDef blue fill:#dbeafe,stroke:#2563eb,color:#1e3a8a,stroke-width:2px classDef yellow fill:#fef3c7,stroke:#d97706,color:#78350f,stroke-width:2px classDef green fill:#d1fae5,stroke:#059669,color:#064e3b,stroke-width:2px classDef red fill:#fee2e2,stroke:#dc2626,color:#7f1d1d,stroke-width:2px classDef purple fill:#ede9fe,stroke:#7c3aed,color:#4c1d95,stroke-width:2px classDef gray fill:#f3f4f6,stroke:#6b7280,color:#111827,stroke-width:2px linkStyle default stroke:#94a3b8,stroke-width:2px style GOOD fill:transparent,stroke:#059669,stroke-width:2px style BAD fill:transparent,stroke:#dc2626,stroke-width:2px
Walking through the code. The # Setup: lines only create the files and use touch -d '10 days ago' to fake their ages, so skip past them.
- The dry run prints the three old
.tmpfiles, including the one insub/, because find searches all the way down. - The same command with
-deleteremoves them. find tmp -type f | sortshows the 3-day-old.tmpfile and the.logfile, both untouched.
Edge cases. -type f protects folders from being deleted. A file that is still open by a program is removed from the folder, but its space only comes back when the program closes it (see Deleted Files Still Using Space). If the folder does not exist, find prints an error and deletes nothing.
# Setup: create files and set their ages with touch -d, in a fresh temporary folder
cd "$(mktemp -d)"
mkdir -p tmp/sub
touch -d '10 days ago' tmp/a.tmp
touch -d '3 days ago' tmp/b.tmp
touch -d '8 days ago' tmp/c.tmp
touch -d '20 days ago' tmp/keep.log
touch -d '30 days ago' tmp/sub/d.tmp
echo "== dry run: what would be deleted =="
find tmp -type f -name '*.tmp' -mtime +7 -print | sort
find tmp -type f -name '*.tmp' -mtime +7 -delete
echo "== what is left =="
find tmp -type f | sortRecapThe whole problem in a few lines, for the night before
- Spot it: "clean up files older than N days"
- Idea:
find dir -type f -name '*.tmp' -mtime +7 -print, check, then the same with-delete - Cost: one walk of the folder tree
- Trap: putting
-deletebefore the tests, which deletes everything
Interview follow-ups
Log every file the nightly job deletes.
Ask find to print each path as it deletes it:
find /srv/app/tmp -type f -name '*.tmp' -mtime +7 -print -delete >> /var/log/tmp-cleanup.log. Because actions run left to right,-printwrites the name and then-deleteremoves the file, for the same entry. Add a date line before it withdate >> /var/log/tmp-cleanup.logso you can tell runs apart. Rotate that log too, or it will slowly grow forever.
Frequently asked questions
-mtime rounds the age down to whole days, then compares. A file 7.9 days old counts as 7, and 7 is not more than 7, so -mtime +7 keeps it. A file 8.1 days old counts as 8 and is deleted. If you need an exact cut-off, use minutes, like -mmin +10080 for 7 days, or compare with a reference file: touch -d '7 days ago' /tmp/ref and find ... ! -newer /tmp/ref.
-delete is built into find, fast, and handles any file name safely. -exec rm -f {} + does the same with the external rm, which is useful when you want rm options or on old systems without -delete. Avoid find ... | xargs rm unless you use -print0 and xargs -0, because plain xargs breaks on spaces in names. In all cases, run the -print version first.
Put the command in cron with full paths, because cron has a minimal environment: 30 3 * * * /usr/bin/find /srv/app/tmp -type f -name '*.tmp' -mtime +7 -delete. That runs at 03:30 every day. Many systems also have systemd-tmpfiles, which cleans folders like /tmp by age from a small config file. Log what was removed if you might need to explain it later; the follow-up shows how.