1. Overview

Many commands can list files in Linux. In this quick tutorial, we’ll explore how to list only non-empty files.

2. Introduction to the Problem

Before discussing the solutions, we need to understand non-empty and empty files. If a file has zero size, we call it an empty file. Conversely, a file whose size is greater than zero is a non-empty file.

Next, as usual, let’s understand the problem through an example:

$ tree -hf myDir 
[ 160]  myDir
├── [   0]  myDir/empty1.txt
├── [   0]  myDir/empty2.txt
├── [   5]  myDir/good.txt
├── [   6]  myDir/great.txt
├── [   5]  myDir/nice.txt
└── [ 140]  myDir/subDir
    ├── [   0]  myDir/subDir/empty1_sub.txt
    ├── [   0]  myDir/subDir/empty2_sub.txt
    ├── [   5]  myDir/subDir/good_sub.txt
    ├── [   6]  myDir/subDir/great_sub.txt
    └── [   5]  myDir/subDir/nice_sub.txt

1 directory, 10 files

As the tree command’s output shows, we have a directory myDir and a subdirectory subDir under it. Since we’ve passed the -h option to the tree command, it prints the file’s size in bytes between brackets.

We can see that the filenames starting with “empty“s are empty files. So our task is to list all non-empty files under the myDir directory. As an option, we’d like to list those files recursively or only stop at the myDir directory level.

Next, let’s explore how to achieve it.

3. Finding Non-Empty Files Recursively

The find command is a powerful command to find files by different criteria, such as filename pattern, permissions, update times, and sizes.

Next, let’s see how find lists non-empty files recursively under the myDir directory:

$ find myDir -type f -size +0
myDir/great.txt
myDir/good.txt
myDir/nice.txt
myDir/subDir/nice_sub.txt
myDir/subDir/great_sub.txt
myDir/subDir/good_sub.txt

As the find output above shows, all non-empty files have been listed. Let’s understand the options we pass to find:

  • -type f ‘ – ‘f‘ stands for files, so we ask find to only find files, skip directories
  • -size +0‘ – The file’s size must be greater than zero

The find command searches files recursively by default. Therefore, non-empty files under the subdirectory subDir are listed as well.

As searching empty files is a pretty common use case, the find command has introduced the -empty option to find empty files and directories. So, alternatively, we can use the -type f option and negate the -empty option to solve the problem:

$ find myDir -type f -not -empty     
myDir/great.txt
myDir/good.txt
myDir/nice.txt
myDir/subDir/nice_sub.txt
myDir/subDir/great_sub.txt
myDir/subDir/good_sub.txt

As we’ve seen in the output above, the find command works as expected.

Next, let’s see how only to search non-empty files within the given directory.

4. Finding Non-Empty Files Only Under the Given Directory

As we’ve learned, find will search recursively under the given directory by default. If we want the find command only to scan the given directory, we can pass the -maxdepth 1 option.

So next, let’s take the command with the -empty option as an example to show how -maxdepth 1 works:

$ find myDir -maxdepth 1 -type f -not -empty 
myDir/great.txt
myDir/good.txt
myDir/nice.txt

The example above shows that only non-empty files under the myDir directory appear in the output.

5. Conclusion

In this article, we’ve learned how to use the find command to list non-empty files under a directory. Through the examples, we may have realized that find is a pretty handy utility to search files.

Moreover, usually, after finding the non-empty files, we may likely perform some operations on those files, such as cp, mv, rm, and so on. find is still good at triggering other processes to work on found files, for instance, using the -exec option.