HomeUNIXRecursively Delete Certain Files

Recursively Delete Certain Files

Using the find command, you can locate and delete files based on many attributes such as filetype. This tech-recipe will demonstrate how to find and delete files based on part of their filename.


To find and delete all files on the system that end in .log, run this as superuser:

find / -name \*.log -exec rm {} \;

The backslashes (\) are important in this command. Using the -exec option allows an arbitrary command, in this case rm, to be used with the filename substituted in place of the {} characters.

This command would be dangerous to execute. A safer alternative uses the -ok command instead of the -exec. When used this way, find will prompt you before each execution of the command following -ok. To optionally delete all files ending with .tmp in the /var filesystem, use the following:

find /var -name \*.tmp -ok rm {} \;

Quinn McHenry
Quinn McHenry
Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
RELATED ARTICLES

8 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments

Cleveland Furia on How to Clear Google Activity
Sid Wohlfarth on How to Clear Google Activity
Riley Magsayo on How to Clear Google Activity
สมัครรับเครดิตฟรี ทันที on How to improve Remote Desktop Protocol Performance
Neal Wangstad on How to Clear Google Activity
Slavcho Andov on How to install Ubuntu
Slavcho Andov on How to fix bad sectors
Sheraz Ali on How to Backup Android