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

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • 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. What are the Network Bonding Modes In CentOS / RHEL
  2. How to Re-Create the Yum Cache and/or Force a Fetch of the Package List of the Enabled Repositories
  3. How To Disable Or Extend System Logging Rate-limit on CentOS/RHEL 6
  4. How to kill Processes in Linux using kill, killall and pkill
  5. mumble Command Examples in Linux
  6. How to check the PHP version on Linux
  7. How to allow only specific non-root user(s) to use crontab
  8. cp: omitting directory – error while copying a directory in Linux
  9. bpftrace Command Examples in Linux
  10. fakeroot: command not found

You May Also Like

Primary Sidebar

Recent Posts

  • ncat Command Examples in Linux
  • ncat: command not found
  • nautilus Command Examples in Linux
  • namei: command not found

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright