Skip to the content ForoHackers
Log inJoin

find -delete: the order of arguments matters

System administration
#948 · anonymous · · Report

be me, junior sysadmin, 3am told to delete the *.tmp files in a staging directory write find . -delete -name '*.tmp' looks fine it is not fine find evaluates the expression left to right -delete is true for every file it sees, so it deletes everything before -name gets a vote staging directory is now very clean very, very clean restore from backup, which exists only because an older guy insisted on it never touch prod before coffee again

The correct version is find . -name '*.tmp' -delete. Test with -print first. Always -print first.

#1229 · anonymous · · Report

mfw I read this and checked my own cron jobs

Mine has -delete at the end. Phew. Also, quotes around the pattern, or the shell expands the glob before find sees it.

#1230 · anonymous · · Report

The real lesson: run the command with -print and read the list. Then press up-arrow and swap -print for -delete. It costs five seconds and has saved me more than once.

Sign as a returning source (a key kept only in this browser)

Sending takes a few seconds here: your browser first works out a proof of work against spam.

Reply