• 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. beep Command Examples in Linux
  2. mailstat Command Examples in Linux
  3. How to Stop and Start Ec2 instance using Jenkins
  4. CentOS / RHEL 6 : How to password protect grub (Password-Protected Booting)
  5. aws ec2: CLI for AWS EC2 (Command Examples)
  6. btrfs subvolume Command Examples in Linux
  7. pvremove Command Examples in Linux
  8. btrfs property Command Examples in Linux
  9. CentOS / RHEL : How to configure vsftpd to use ports other than the default ports 20 and 21
  10. qemu-system-x86_64: command not found

You May Also Like

Primary Sidebar

Recent Posts

  • “aws workmail” Command Examples
  • aws-vault Command Examples (A vault for securely storing and accessing AWS credentials in development environments)
  • “aws sts” Command Examples
  • “aws sqs” Command Examples

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright