- Kali Linux Tutorial
- Kali Linux - Home
- Installation & Configuration
- Information Gathering Tools
- Vulnerability Analyses Tools
- Kali Linux - Wireless Attacks
- Website Penetration Testing
- Kali Linux - Exploitation Tools
- Kali Linux - Forensics Tools
- Kali Linux - Social Engineering
- Kali Linux - Stressing Tools
- Kali Linux - Sniffing & Spoofing
- Kali Linux - Password Cracking Tools
- Kali Linux - Maintaining Access
- Kali Linux - Reverse Engineering
- Kali Linux - Reporting Tools
- Kali Linux Useful Resources
- Kali Linux - Quick Guide
- Kali Linux - Useful Resources
- Kali Linux - Discussion
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
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.
- Related Articles
- How to Protect Files and Directories from Deleting in Linux
- How to move a file, group of files, and directories in Linux?
- Find the Largest Top 10 Files and Directories On a Linux
- How to delete empty files and folders using PowerShell?
- How to Find a Specific String or Word in Files and Directories in Linux
- How to remove files and directories in the Linux operating system using the terminal?\n
- Listing out directories and files in Python?
- Soft-Delete Files from the Terminal on Linux
- Create, Delete and Change Directories in Perl
- How to compare the files available in two directories using diff command in Linux?
- Compare two directories in Linux?
- Generate temporary files and directories using Python
- Listing out directories and files using C#
- How to Empty or Delete a Large File Content in Linux?
- How to add, delete, change ownership (of files) of a group in Linux?