1. Overview

As Linux users, we often need to delete files. This simple task quickly becomes time-consuming when the system has a large number of files and we wish to delete several of them according to specific criteria. However, we can make this process time-efficient and less error-prone by using the find command.

In this tutorial, we’ll discuss various examples of the find command to help us delete files based on their sizes.

2. Setting up an Example

First, let’s use the fallocate command to create a few files of different sizes:

$ touch empty-file.txt
$ fallocate -l 5K 5kb-file.txt
$ fallocate -l 10K 10kb-file.txt
$ fallocate -l 1M 1mb-file.txt
$ fallocate -l 5M 5mb-file.txt
$ fallocate -l 10M 10mb-file.txt

Now, let’s verify that the files have been created correctly:

$ ls -lSrh
total 17M
-rw-rw-r-- 1 jarvis jarvis    0 Dec 25 12:56 empty-file.txt
-rw-rw-r-- 1 jarvis jarvis 5.0K Dec 25 12:56 5kb-file.txt
-rw-rw-r-- 1 jarvis jarvis  10K Dec 25 12:57 10kb-file.txt
-rw-rw-r-- 1 jarvis jarvis 1.0M Dec 25 12:57 1mb-file.txt
-rw-rw-r-- 1 jarvis jarvis 5.0M Dec 25 12:57 5mb-file.txt
-rw-rw-r-- 1 jarvis jarvis  10M Dec 25 12:57 10mb-file.txt

Later on, we’ll see how to delete these files according to their sizes.

3. Precautions While Deleting Files

Removing the wrong file may cause permanent data loss. Doing a bulk file operation can make changes to a lot of files. Therefore, let’s discuss some of the precautions that we can take while deleting files using find.

3.1. Perform a Dry Run Using the -print Action

It’s a good practice to perform a dry run using the -print action before doing the actual delete operation. This action prints the name of the files that match the specified criteria.

Let’s perform a dry run for the file whose name is empty-file.txt:

$ find . -name empty-file.txt -print
./empty-file.txt

After reviewing the result, we can use the -delete action to proceed with the delete operation:

$ find . -name empty-file.txt -print -delete

3.2. Remembering to Use -delete as the Last Action

It’s important to note that the -delete must be the last action of the command. As the command line is evaluated as an expression, using the -delete action first would delete everything from the search path.

For example, the following command deletes all the files from the current directory:

$ find . -delete -name empty-file.txt -print

So to get the correct result, first we can perform the dry run using the -print action. Then we can replace the -print action with the -delete to perform the actual deletion.

3.3. Use the -exec Action Instead of -delete

The find command provides the -exec action that executes the specified command. So to delete a file, we can use the rm command in an interactive mode. This method allows us to provide an additional safety check before the delete occurs.

For instance, we can use the following command to delete a file interactively:

$ find . -name empty-file.txt -exec rm -i {} \; 
rm: remove regular empty file './empty-file.txt'?

It’s important to note that not all implementations of the find command support the -delete action. Therefore, the -exec action is more portable.

4. Deleting Files of the Exact Size

We can use the -size option with the find command to find and delete files of a specific size. This option uses 512-byte blocks as a default space unit. However, we can override this default behavior by using the following space units as a suffix:

  • c – for bytes
  • k – for kibibytes (KiB, units of 1024 bytes)
  • M – for mebibytes (MiB, units of 1024 * 1024 = 1048576 bytes)
  • G – for gibibytes (GiB, units of 1024 * 1024 * 1024 = 1073741824 bytes)

So, let’s use the -size option to delete a file whose size is 5 kibibytes:

$ find . -type f -size 5k -exec rm -v {} \;
removed './5kb-file.txt'

In the above example, the {} placeholder represents the file that matches certain criterion. In this case, the criterion was the regular file of 5 kibibytes size.

Additionally, we used -type f predicate to filter regular files only.

5. Deleting Files Larger Than the Specified Size

In a similar way, we can use the -size option to find and delete files that are larger than the specified size. The only modification needed is that we have to use the plus(+) symbol with the command.

Now, let’s specify the size as +1M to delete all the files that are larger than 1 mebibyte:

$ find . -type f -size +1M -exec rm -v {} \;
removed './5mb-file.txt'
removed './10mb-file.txt'

As we can see, the find command has removed the correct files.

6.  Deleting Files Smaller Than the Specified Size

In addition to this, we can use the hyphen() symbol with the find command to delete all the files that are smaller than the specified size.

So, let’s specify the size as -2M to delete all the files that are smaller than 2 mebibytes:

$ find . -type f -size -2M -exec rm -v {} \;
removed './empty-file.txt'
removed './1mb-file.txt'
removed './10kb-file.txt'

In the above output, we can see that the command has removed the expected files.

7. An Implication of the Size Rounding Up

The behavior of the find command differs if we use the space units with the -size option. When we use this option, the file size is rounded up to the next unit specified in the directive.

For example, if the file size is 4KB and we use the -size -1M directive then the 4KB size is treated as 1M. Let’s understand this with an example.

7.1. Understanding Size Rounding up Behavior

First, let’s create a few files that are smaller than the 1 mebibyte in size:

$ touch empty-file.txt
$ fallocate -l 4K 4kb-file.txt
$ fallocate -l 8K 8kb-file.txt

Next, let’s verify that the files have been created correctly:

ls -lSrh
total 12K
-rw-rw-r-- 1 jarvis jarvis    0 Dec 25 12:57 empty-file.txt
-rw-rw-r-- 1 jarvis jarvis 4.0K Dec 25 12:57 4kb-file.txt
-rw-rw-r-- 1 jarvis jarvis 8.0K Dec 25 12:57 8kb-file.txt

Now, let’s use the -size -1M directive to find out all the files that are smaller than 1 mebibyte in size. Ideally, the command should list all three files as they’re smaller than 1 mebibyte. However, the command lists only an empty file:

$ find . -type f -size -1M
./empty-file.txt

As we can see, the -size -1M directive performs the rounding up and treats 4KB and 8KB sizes as 1M size.

7.2. Disabling the Size Rounding up Behavior

To mitigate such issues, we can specify the same size in bytes to disable the rounding-up behavior.

So, let’s use 1048576 bytes as an argument to find all files that are smaller than 1 mebibyte:

$ find . -type f -size -1048576c
./empty-file.txt
./4kb-file.txt
./8kb-file.txt

In this example, we have used the c prefix to specify the size in bytes and now the command shows the correct result.

Additionally, we can use the arithmetic expansion in Bash to specify the size in bytes. For instance, we can achieve the same result using the following command:

$ find . -type f -size -$((1024*1024))c
./empty-file.txt
./4kb-file.txt
./8kb-file.txt

8. Conclusion

In this article, we saw how to use the find command to delete files based on their sizes.

First, we discussed the precautions that we need to take while deleting files. Next, we discussed how to delete files with the exact sizes. Then, we saw how to delete files that are larger and smaller than the specified size.

Finally, we discussed the implication of the rounded-up size and a way to overcome it.

Comments are closed on this article!