Most of the time this question arises, how will you list the empty file and folders when you are working on the Linux-based operating system? Empty files and directories are those which have no data or sub-directories within them, respectively. If you want to learn how to do it, then this tutorial is meant for you. You have to go through each step defined in this tutorial to list the empty folders or files in the terminal. First, you must have sudo privileges of a Linux-based system to use it. After logging in from the system, you have to open the command terminal from the Applications. We will have a look at some of the examples for listing empty folders.
Using “Find” Command
There are a lot of ways to use the ‘find’ command in our examples to list the empty folders and files in the command shell. We will discuss each one of them.
Example 01: List Empty Directories
So assume you are at your home directory of the Linux system, you need to look at all the empty directories within it. Use the ‘find’ command along with the ‘-type’ flag that specifies the directory type search using the keyword ‘d’. The word ‘-empty’ has been used as a flag to search only empty directories within the home directory as stated below. The dot means the current location which is the home directory of a Linux-based system. The output shows all the empty directories within the home directory and its sub-directories.
Example 02: List Empty Files
Now, it’s time to list all the empty files within the home directory using the same above command with a slight change. We will be using the “f” flag to specify that the searched item must be the file type. Execute the below command in the shell and you will get a list of empty files residing in the home directory and its sub-directories as presented in the snapshot.
Example 03: List Empty Files
Suppose somebody wants to list the empty directories only that are residing in the home directory of the Linux system without the empty directories in the sub-directories of the home directories, then they can also use the “find” command. In this command, you have to define the depth of the tree you want to search by listing empty directories using the “-maxdepth” flag. You can specify the tree depth by a number as we have used 1 in the below command. This means it will only search for the empty directories which are directly residing in the home directory of the system. Try to execute the below-stated query in the terminal shell. The output shows the list of all the empty directories, which means all of these listed directories have no data within them.
We were listing all the empty directories or files in the home directory. Now, it’s time to have a slight change. We will be looking at the empty files and folders within some other directories.
Example 04: List Empty files
For that purpose, we have to define a path of that particular directory within the instruction. The remaining command will be as it is. Try the below command to search the empty files within the folder ‘Documents’. The output is elaborating that the directory ‘Documents’ have only two in it which are currently empty e.g., one.txt and scnd.txt.
Example 05: List Empty folders
Now let’s alter this command to see empty directories within the directory “Documents”. To do this, we have to write “d” instead of “f” as displayed below. Try executing the below query to show empty folders. The output shows that we have currently no empty files in the directory “Documents”.
Example 06: List Empty files Count Number
You have seen how to list the empty files and folders. Now, we will be looking at the count number of empty files and folders located in a certain folder. For that, we will be using the same “find” command. All the old parameters will remain the same in the query with a slight change. We are using the “wc –l” parameter in the command to count the empty files residing in the current location. Execution of the stated command gives us the output of 18. This means that the home directory has only a total of 18 empty files in it.
Example 07: List Non-Empty Files Count Number
As we learned how to count the number of empty files within a particular directory. It’s time to count the number of non-empty directories of files within some directory. For this particular purpose, we will be using the “-not” flag parameter in a query. The remaining query will be the same from start to end. So, let’s run the below ‘find’ command in the terminal shell as shown in the snapshot. The output shows the count of non-empty files within the home directory and its sub-directories which is “6474”.
Example 08: List Empty Files With Size
Last but not least, we will be using the “find” command along with the keyword “size” to search the files according to the size specified. Now, we will be listing the files from the home directory that have zero data within them. We have defined the value of keyword size as “0”. The output is shown below with the list of files having the size “0”.
Conclusion
Finally! We have done all the necessary commands to list or show the empty files within some directory.