Skip to main content
Bash and Linux

Delete Old Temp Files

easyfind and xargs Must-do

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/

TEXT
tmp/a.tmp 10 days old
tmp/b.tmp 3 days old
tmp/c.tmp 8 days old
tmp/keep.log 20 days old (not a .tmp file)
tmp/sub/d.tmp 30 days old
  1. Dry run: print the .tmp files older than 7 days.
  2. Delete exactly those files.
  3. Print every file that is left.

Expected output:

TEXT
== dry run: what would be deleted ==
tmp/a.tmp
tmp/c.tmp
tmp/sub/d.tmp
== what is left ==
tmp/b.tmp
tmp/keep.log

Hints

Hint 1: -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:

%%{init: {"flowchart": {"padding": 18, "nodeSpacing": 30, "rankSpacing": 40, "htmlLabels": true}, "themeVariables": {"fontSize": "18px"}}}%% flowchart TB A(["find ... -print"]):::blue --> B{{"is the list right?"}}:::yellow B --> C(["same command with -delete"]):::red B --> D["fix the tests first"]:::gray 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
  1. Run the full command with -print at the end. Read the list.
  2. Run exactly the same command with -delete in 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.

%%{init: {"flowchart": {"padding": 18, "nodeSpacing": 30, "rankSpacing": 40, "htmlLabels": true}, "themeVariables": {"fontSize": "18px"}}}%% flowchart LR subgraph GOOD["tests first"] direction TB G1["-name '*.tmp' -mtime +7"]:::green --> G2["-delete"]:::green end subgraph BAD["-delete first"] direction TB B1["-delete"]:::red --> B2["runs on everything
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.

  1. The dry run prints the three old .tmp files, including the one in sub/, because find searches all the way down.
  2. The same command with -delete removes them.
  3. find tmp -type f | sort shows the 3-day-old .tmp file and the .log file, 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 | sort
RecapThe 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 -delete before 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, -print writes the name and then -delete removes the file, for the same entry. Add a date line before it with date >> /var/log/tmp-cleanup.log so 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.