Safely Find and Delete Files in Linux with find

The find command is useful for locating files recursively and then taking action on them. When deleting files, it is safer to list the matches first and delete only after checking the output.

List matching files first

List all .tmp files below the current directory:

find . -type f -name "*.tmp" -print

List all .log files below the current directory:

find . -type f -name "*.log" -print

Delete after checking

After confirming the list is correct, delete the matching .tmp files:

find . -type f -name "*.tmp" -delete

Delete matching .log files:

find . -type f -name "*.log" -delete

For production systems, always run the listing command first. This helps avoid deleting files from the wrong directory or matching a broader pattern than intended.

Leave a Comment