Delete empty files and directories in Linux


Overview

We'll learn how to remove empty directories and empty file on Linux.

Empty directories don't take up any disk space, but they're good to keep them tidy. We should clear out old files and folders periodically.

All the instructions in this tutorial are for Linux systems. They won't work on Windows.

Delete Empty Files in a Directory

You can use the `find` command to remove all the empty files in an existing folder.

$ find . -type f -empty -print -delete

To delete empty directories, we first need to search for all the empty folders in the specified folder and then, delete them.

To find all the empty directories in the current directory, use the following command: find. −type d −empty −print | xargs rm −rf. Add the −delete option to remove them.

Let’s take an example to better explain this concept.

A directory containing both empty and non-empties may be considered. Here, the file prefixed with data-filename is a non-empty file and the one prefixed with empty is an empty file.

|--  data-file1
|--  data-file2
|--  empty-file1
|--  empty-file2
|--  empty-file3
|--  empty file 4
|--  mydir1
|    |-- data-file3
|    `-- empty-file5
|--  mydir2
|    |-- data-file4
|    `-- empty-file6
`-- mydir3
`-- mydir4
   `-- mydir5

We'll now run the above command inside these directories. It will delete all empty files recursively - meaning that the empty-file4 and empty-file5 inside the directories mydir1 and mydird2, respectively, will be removed too.

$ find . -type f -empty -print -delete
./empty-file1
./empty-file2
./empty-file3
./mydir1/empty-file5
./mydir2/empty-file6
./empty file 4

Let’s look at the output carefully. We’ll see that this operation has removed the files whose names include spaces from the directory.

It has deleted only empty files and not the directories like mydir1, mydir2, mydir4, and mydir6.

Non-Recursive Deletion of Empty Files

We've talked about deleting empty files from directories recursively so far. But what if we want to remove empty files from the current directory itself?

The find utility has an option −maxDepth that defines the maximum depth at which the find utility searches for files.

Using −maxdepth1, the 'f' (file) command will search for files in the current directory only.

$ find . -maxdepth 1 -type f -empty -print -delete
./empty-file1
./empty-file2
./empty-file3
./empty file 4

Delete All Empty Directories

We can search for files by using -type f with the find command −

$ find . -type d -empty -print -delete

It will remove all the empty directories from the current directory.

Let's run this command inside the directory where we saved our script.

$ find . -type d -empty -print -delete
./mydir3
./mydir4/mydir5
./mydir4

After deleting the mydir5 folder, mydir3 is no longer an empty directory; instead, it contains the files from mydir2.

Non-Recursive Deletion of Empty Directories

By using −maxdepth 1, the find utility searches only for empty directories in the current working directory.

$ find . -maxdepth 1 -type d -empty -print -delete
./mydir3

Delete Empty Files and Directories Together

It’s now finally the right moment to combine everything that we've learned so far. Delete all the empty files and folders from the current directory by running one command.

We'll use the logical OR (OR) operation (-o), with the find utility, to search for empty files or directories.

$ find . -type d -empty -print -delete -o -type f -empty -print -delete
./empty-file1
./empty-file2
./empty-file3
./mydir1/empty-file5
./mydir2/empty-file6
./mydir3
./mydir4/mydir5
./mydir4

The −o option splits the command from the filename into two parts. The fist part, −type d −empty − print −delete, deletes all the empty directories, whereas the second part, − type f −empty − print − delete, deletes all the files which are empty.

We can use −maxdepth 0 to delete the empty files/directoires recursively.

$ find . -maxdepth 1 -type d -empty -print -delete -o -type f -empty -print -delete
./empty-file1
./empty-file2
./empty-file3
./mydir3

Conclusion

Here, we've learnt about empty files, empty folders, and how to remove them in Linux. We've looked at two different ways of deleting files − Recursive and Non−Recursive.

It is important to review all the directories and delete any unnecessary ones before removing them. In all the examples discussed above, we can use the −recurse option to review all the directories that would be removed.

As a best practice, we can set up a cron task to remove empty folders and subfolders from our system. This way, we won't ever accumulate empty folders and subfoldes on our computer.

Updated on: 23-Dec-2022

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements