1. Overview

As a system administrator on a Linux system, we can spend quite a bit of time cleaning things up. This will often include finding and deleting files recursively in a directory tree.

In this tutorial, we’ll look at a few ways to remove batches of files based on file “extensions”, or filename patterns. We will also discuss ways to avoid some common pitfalls of batch deletes.

2. Warning

When we delete a file via the CLI on Linux, it’s gone for good. Therefore, we should observe a couple of best practices as we delete multiple files with one command.

First, we should verify that we are deleting the correct files. To help with this:

  • We might use pwd to check our current directory, or verify that our path to the files is correct
  • We should verify that we are only deleting what we want to delete, by using ls to list and review the files
  • We might preview which files will be deleted by using the command without the delete part enabled

Secondly, we should consider making a backup of the directory before deleting from it. The Linux tar command is useful for making backups of directories.

3. Verify and Backup Files

For our examples, let’s say we’re editing some text files with the Emacs text editor. Emacs creates a backup of each edited file with a ‘’ (tilde) character at the end of its filename. After editing some text files, we want to remove any files ending with “*.txt”*.

For all of our deletion examples, we’ll be using the Linux find command to identify all files to delete. find is the most popular and effective tool to search for files in Linux.

3.1. Verify Location

Let’s first check that we’re in the right place by using pwd:

$ pwd
/tmp/d

3.2. Find and Verify Files

Now, let’s double-check which files we want to delete by composing the find command that will locate them, and using it to list them instead of delete them:

$ find . -type f -name '*.txt~' -print
./john.txt~
./middle/ieou.txt~
./b.txt~
./left/u.txt~
./left/up/asdf.txt~
./left/t.txt~
./left/down/kjhl.txt~
./a.txt~

Here, we’re using the options -type f to select files, -name ‘*.txt~’ to match the filename, and -print to echo the filename to the terminal.

3.3. Backup Files

Next, let’s make a backup of the directory with tar. This is not a required step, but we should make a backup unless we are 100% sure we have the correct files to delete:

$ tar -cf backup.tar .

To see a list of files that tar is backing up, we can add the -v option:

$ tar -cvf backup.tar .

4. Remove Files With find -delete

The easiest way to delete the files is to tell find to delete them for us. We can use the -name option with find to specify a glob expression. In the following examples, we are using the glob expression to search for files with a certain extension. Globbing is useful for finding much more than just file extensions, as we can see from the man page.

$ find . -type f -name '*.txt~' -print -delete
./john.txt~
./middle/ieou.txt~
./b.txt~
./left/u.txt~
./left/up/asdf.txt~
./left/t.txt~
./left/down/kjhl.txt~
./a.txt~

In order to delete files with find, we just add -delete to the end of the find command.

Or, if we don’t want to print the files while deleting them:

$ find . -type f -name '.txt*~' -delete

5. Remove Files With find -exec

Let’s say we need to use rm with some additional options, or a different command (such as trash) to remove the files:

$ find . -type f -name '*.txt~' -exec rm -f {} \;

Or:

$ find . -type f -name '*.txt~' -exec trash {} \;

Using find with -exec allows us to use any command and options to remove the files. It also allows us to do other bulk operations on a set of files.

6. Remove Files With find and xargs

Finally, we might find that our distro provides us with a version of the find command that does not support the -delete option. In that case, we can use find with xargs:

$ find . -name "*.txt~" -print0 | xargs -0 rm

When we use xargs, it’s important to use null terminators so that we don’t have issues with spaces in filenames.

7. Conclusion

In this article, we saw three ways to recursively delete files using find and a couple of other commands. We also looked at the importance of verifying what we are about to delete and the value of taking a quick backup before deleting multiple files.