Thursday, August 24, 2017

How to Delete Empty Files and Directories in Unix or Linux Server - find Command Example

Deleting empty file and directory in Unix
Many times we need to find and delete empty files or directories in UNIX/Linux. Since there is no single command in Unix/Linux which allows you to remove empty files or empty directories rather we need to rely on find command and xargs command. In this UNIX and Linux example, we will see How to delete empty files and directories. Before removing empty files and directories we need to find those files and there are lots of option available to search for empty directories like find, grep , awk, etc. You just need to know the correct option. Like in any other operating system empty files and directories in Unix are those whose size is zero. Empty files don't contain any content while empty directories do not contain anything at all e.g files or sub-directories.

As discussed in the previous post 10 frequently-used find command examples we can also use find command to search and delete empty files and directories as it provides searching files based on the size as well.

By the way, if you are new to Linux then I also suggest you go through a comprehensive Linux course to learn some basics commands and fundamentals like Linux file system, permissions, and other basic things. 

If you need an online course, I highly recommend Linux Mastery: Master the Linux Command Line in 11.5 Hours on Udemy. It's a very practical and hands-on course to learn Linux fundamentals in a quick time. It's also very affordable and you can buy in just $10 on Udemy flash sales which happen every now and then. 


Creating Empty files and directories in Unix

let's first create an empty file and directory to demonstrate an example of how to delete empty files in Unix. We can use the same set of commands which we have used in our example of How to find the size of files and directories in Unix.

//This will create an empty file in the current directory
test@localhost:~/unix touch empty.txt          

//This will create an empty directory inside the current directory
test@localhost:~/unix mkdir empty_dir    

//This the command will find all empty files and directories in Unix
test@localhost:~/unix find . -empty          
./empty.txt
./empty_dir



Searching Empty Files and Directory in Unix/Linux

find -empty option prints both empty files and directories. If you just want to print files then use -type f option and -type d for listing empty directories. its quite flexible. You can also use grep command along with ls –lrt to display empty files and directories as shown below :

//this command will print empty files
test@localhost:~/unix find . -type f -empty
./empty.txt


//this command will print empty directories
test@localhost:~/unix find  . -type d -empty
./empty_dir


//How to use grep command to print empty files and directories
test@localhost:~/unix ls -ltr | grep '\<0\>'
drwxr-xr-x+ 1 test Domain Users   0 Jun 15 11:43 empty_dir/
-rw-r--r--  1 test Domain Users   0 Jun 15 11:44 empty.txt

//find command to print empty files and directories
test@localhost:~/unix find . -maxdepth 1  -size 0 -ls
90353467524120775    0 drwxr-xr-x   1 test Domain Users        0 Jun 15 11:43 .
9007199255261921     0 -rw-r--r--   1 test Domain Users        0 Jun 15 11:44 ./empty.txt
19421773393554899    0 drwxr-xr-x   1 test Domain Users        0 Jun 15 11:43 ./empty_dir


How to remove empty files and directories in Unix and Linux




Deleting Empty Files and Directories in Unix Linux

Now once we certain that there are empty files and directory exists you can delete them by using find -delete option or executing rm command in combination with find command as shown below:

//removing Empty files and directories using the find command
test@localhost:~/unix find . -empty -delete

test@localhost:~/unix find . -empty -delete

test@localhost:~/unix ls -lrt
total 1.0K
-rw-r--r-- 1 test Domain Users 118 Aug  4  2011 contacts.txt

//using find and xargs command to remove empty files and directories
test@localhost:~/unix find . -empty | xargs rm -r

test@localhost:~/unix find . -empty -type d -exec rm -r {} \;
find: `./empty_dir': Not a directory


That’s all on How to find and remove empty files and directories in Unix and Linux host. As I mentioned there are many ways to find empty files and directory but the best way is by using find command, which not only lists empty files but empty directories as well.

Further Learning
Linux Command Line Basics
Linux Command Line Interface (CLI) Fundamentals
Learn Linux in 5 Days and Level Up Your Career

Other Unix and Linux command tutorials from Javarevisited Blog

3 comments :

deepish said...

Very nice article..very helpful for interviews..
As you mentioned there are lot of ways to delete files and directories, just wanted to add on one more way:

rm -rf `find . -empty`

Anonymous said...

I've been using find for ages but this is the first time to hear about -empty and -delete. (Later GNU additions?) Thanks!

But the premise in this fine article is incorrect. Why would "find . -empty -delete" or "find . -empty -exec rm -r {} \;" not be counted as a "single command"?

Second, "find . -empty -delete" or its -exec equivalent is better than xargs and backquote when you have many to delete. The problem with a second command is that each command can only take this many arguments in shell. So when find finds too many empty files, the second command will bail with error message "Too many arguments." Now the rm from within -exec actually receives only a single argument each time find finds a file or directory, so there is no fuss about too many of them.

LarrybwoY said...

What about "find . -size 0 -delete" ?

Post a Comment