Delete Files Recursively by Pattern

terminalintermediatelinuxmacos

Find and delete files matching a specific pattern recursively through directories using the find command

#terminal#find#delete#recursive#file-management

Delete Files Recursively by Pattern

Overview

Find and delete files matching a specific pattern recursively through directories using the find command

Prerequisites

  • Terminal access
  • Bash knowledge

Instructions

If your current directory is ~/dir1/ and you want to find all files that are called something.bak, even if they are in ~/dir1/dir2/dir3/, then you should run this command:

find . -name "*.bak" -type f

I took for granted that you all understand that with something.bak I mean any file with the extension .bak :)

Now that you checked all the files that are called like that, you can delete them all with adding the option -delete, just this way:

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

Note: I recommend to use this command with precaution. With the first command you check the files you are about to delete. Also, make sure that -delete is the last argument in your command. If you put it before -name *.bak, it will delete everything.

Troubleshooting

If you encounter any issues:

  1. Verify all prerequisites are installed
  2. Check command syntax carefully
  3. Ensure you have necessary permissions

Related Utilities

  • Column Removal