- How can I recursively delete all empty files and directories in Linux?
- 3 Answers 3
- Delete Empty Files and Directories in Linux
- 1. Overview
- 2. Delete Empty Files in a Directory
- 3. Non-Recursive Deletion of Empty Files
- 4. Delete All Empty Directories
- 5. Non-Recursive Deletion of Empty Directories
- 6. Delete Empty Files and Directories Together
- 7. Conclusion
- Delete empty files and directories in Linux
- Delete Empty Files in a Directory
- Non-Recursive Deletion of Empty Files
- Delete All Empty Directories
- Non-Recursive Deletion of Empty Directories
- Delete Empty Files and Directories Together
- Conclusion
How can I recursively delete all empty files and directories in Linux?
How can I recursively cleanup all empty files and directories in a parent directory? Let’s say I have this directory structure:
Parent/ |____Child1/ |______ file11.txt (empty) |______ Dir1/ (empty) |____Child2/ |_______ file21.txt |_______ file22.txt (empty) |____ file1.txt
Parent/ |____Child2/ |_______ file21.txt |____ file1.txt
I wasn’t confused, per se, but, even before I saw the comments, I thought it was confusing that the illustration had multiple files with the same name. You might want to consider using a naming convention like Child1/file11.txt , Child2/file21.txt and Child2/file22.txt .
3 Answers 3
This is a really simple one liner:
It’s fairly self explanatory. Although when I checked I was surprised that it successfully deletes Parent/Child1. Usually you would expect it to process the parent before the child unless you specify -depth .
This works because -delete implies -depth . See the GNU find manual:
-delete Delete files; true if removal succeeded. If the removal failed, an error message is issued. If -delete fails, find’s exit status will be nonzero (when it eventually exits). Use of -delete automatically turns on the -depth option.
Note these features are not part of the Posix Standard, but most likely will be there under many Linux Distribution. You may have a specific problem with smaller ones such as Alpine Linux as they are based on Busybox which doesn’t support -empty .
Other systems that do include non-standard -empty and -delete include BSD and OSX but apparently not AIX.
Delete Empty Files and Directories in Linux
The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.
To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.
Connect your cluster and start monitoring your K8s costs right away:
1. Overview
In this tutorial, we’ll learn how to delete empty files and directories on Linux.
An empty file is a file of zero bytes size. An empty directory is a directory that doesn’t contain any file or directory. It’s fair to say that empty files don’t consume space, but we should clean our file system from time to time as a best practice.
All the commands discussed in this tutorial are Linux-specific and won’t work on Windows.
2. Delete Empty Files in a Directory
We can use the find command to delete all the empty files in a given directory:
$ find . -type f -empty -print -delete
In order to delete empty files, we need to perform two steps. First, search all the empty files in the given directory and then, delete all those files.
This particular part of the command, find . -type f -empty -print, will find all the empty files in the given directory recursively. Then, we add the -delete option to delete all those files.
Let’s try to understand this with an example.
Consider a directory that contains both empty and non-empty files and sub-directories. Here, the files prefixed with data-file are non-empty files and the ones prefixed with empty are empty files:
|-- 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
Now, we’ll run the above command inside this directory. It will delete all the empty files recursively. This means that the empty-file5 and empty-file6 inside the directory mydir1 and mydir2, respectively, will also be deleted:
$ find . -type f -empty -print -delete ./empty-file1 ./empty-file2 ./empty-file3 ./mydir1/empty-file5 ./mydir2/empty-file6 ./empty file 4
Let’s observe the output closely. We’ll notice that this command has deleted the files whose name includes space in it (“empty file 4” in our example).
Also, this command has only deleted empty files and not the empty directories like mydir3 and mydir5.
3. Non-Recursive Deletion of Empty Files
Until now, we’ve discussed the scenario where we delete empty files recursively inside a directory. What if we need to delete the empty files present in the current directory and not the ones that are present inside the sub-directories?
The find command has an option -maxdepth that defines the maximum number of directory levels deep the find command should search for a file.
Using -maxdepth 1, the find command will search for a file in the current directory only:
$ find . -maxdepth 1 -type f -empty -print -delete ./empty-file1 ./empty-file2 ./empty-file3 ./empty file 4
4. Delete All Empty Directories
We can search for the directories by using -type d with the find command:
$ find . -type d -empty -print -delete
This will delete all the empty directories present inside the current directory recursively.
Let’s run this command inside the same file system as mentioned above:
$ find . -type d -empty -print -delete ./mydir3 ./mydir4/mydir5 ./mydir4
Again, notice the output carefully. mydir4 directory is also deleted because after deleting the mydir5 directory, mydir4 becomes an empty directory (closely observe the directory structure once again).
5. Non-Recursive Deletion of Empty Directories
Using -maxdepth 1, we can restrict the find command to search for the empty directories in the current directory only:
$ find . -maxdepth 1 -type d -empty -print -delete ./mydir3
6. Delete Empty Files and Directories Together
Now it’s time to combine everything that we’ve learned so far. Let’s delete all the empty files and directories present inside the current directory using a single command.
We’ll use the logical OR operator, -o, with the find command to search for empty files and 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 breaks the command from the file path into two parts. The first part,-type d -empty -print -delete, will delete all the empty directories, and the second part, -type f -empty -print -delete, will delete all the empty files.
Again, we can use -maxdepth 1 to delete the empty files and directories non-recursively:
$ find . -maxdepth 1 -type d -empty -print -delete -o -type f -empty -print -delete ./empty-file1 ./empty-file2 ./empty-file3 ./mydir3
7. Conclusion
In this tutorial, we’ve learned about empty files, empty directories, and how to delete them in Linux. We’ve looked into two types of deletion approaches, recursive and non-recursive.
Once a file or directory is deleted, it cannot be restored. So, it’s highly recommended to review all the files/directories before deleting them. In all the commands discussed above, we can remove the -delete option to review all the files that would be deleted.
Also, as a good practice, we can create a cron job to delete empty files and directories. This way, we’ll never accumulate empty files/directories on our machine.
Delete empty files and directories in Linux
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.