• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to Find and Delete Empty Directories and Files in Linux

by admin

1. Find empty directories in the current directory using find -empty:

$ find . -type d -empty

2. Use the following command to remove all empty directories under the current directory:

$ find . -type d -empty -exec rmdir {} \;

3. Find empty files in the current directory using find -empty:

$ find . -type f -empty
Note: Typically empty files are created by some programs as place holders, or as lock files, or as socket files for communication.

4. How many empty files are located under the current directory (and sub-directories)? To count the number of empty files under the current directory, pipe the find command to wc -l.

$ find . -type f -empty | wc -l

5. How many non-empty files are located under the current directory (and sub-directories)?

$ find . -type f -not -empty | wc -l
Note: Find option -not reverts the option that follows it.

In all the above examples, replace the ( . ) dot with any other directory path under which you would like to search the files.

Filed Under: DevOps, Linux, Shell Scripting

Some more articles you might also be interested in …

  1. javadoc Command Examples
  2. flatpak-builder Command Examples in Linux
  3. aws history: Print the command-line history for AWS CLI commands (Command Examples)
  4. sysctl setting for high load and prevent DDoS
  5. How to extract RPM package without installing it
  6. How to resolve Docker Search Command Error – “getsockopt: no route to host” in CentOS / RHEL / Fedora
  7. finch: command not found
  8. gsettings: command not found
  9. “java” command does not run the JVM that has been installed
  10. “docker stats” Command Examples

You May Also Like

Primary Sidebar

Recent Posts

  • Vanilla OS 2 Released: A New Era for Linux Enthusiasts
  • mk Command Examples
  • mixxx Command Examples
  • mix Command Examples

© 2025 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright