When working with Linux, it's common to end up with empty files or directories scattered across your system. These might be leftover from software installations, temporary files, or simply folders that were created but never used. While empty files and directories don’t take up much space, they can clutter your system, making it harder to stay organized. Fortunately, Linux provides powerful command line tools to help you find and delete these empty files and directories.
In this guide, we’ll walk through how to easily identify and clean up unused files and directories using simple terminal commands. This can help you maintain a clean, and efficient system.
Table of Contents
Find and Delete Empty Files and Directories using find Command in Linux
If you want to clean up empty files or directories in a particular directory, you can use the find
command like this:
find /path/to/directory -type f -empty -delete
/path/to/directory
: Replace with the path where you want to search.-type f
: Searches for files.-empty
: Finds empty files.-delete
: Deletes the found files.
Example:
This command will find and delete all empty files in the Documents
directory.
find ~/Documents -type f -empty -delete
Similarly, to find and delete empty directories:
find /path/to/directory -type d -empty -delete
-type d
: Searches for directories.
Example:
The following command will find and delete all empty directories in the Downloads
directory.
find ~/Downloads -type d -empty -delete
This is useful for keeping your filesystem clean and free of unnecessary empty files and folders.
Cheatsheet
Download and keep this cheatsheet near your desk for quick reference.
Is It Safe to Delete Empty Directories or Files?
In general, it is usually safe to delete empty directories and files, but there are some considerations to keep in mind:
When It’s Safe:
1. Temporary or Log Files
If you’re cleaning up temporary files or logs that are no longer needed, deleting empty files or directories is typically safe and can help reduce clutter.
2. Old Project Files
In a project directory where you know the purpose of each file and folder, deleting empty ones can help maintain organization.
When to Be Cautious:
1. System or Application Directories
Some applications or systems may create and expect certain directories to exist, even if they are empty. Deleting these could cause errors or unexpected behavior. For example, configuration directories may be empty until the application is run.
2. Symbolic Links
An empty symbolic link (symlink) might look like an empty file, but it could point to something important. Deleting it could break functionality.
3. Backup or Version-Controlled Directories
In directories managed by backup software or version control systems, empty files or folders might be placeholders or have other purposes.
Best Practices:
1. Check Before Deleting
Before running a command with -delete
, it’s a good idea to first list what will be deleted. For example:
find /path/to/directory -type d -empty
This way, you can review what will be affected.
2. Backup Important Data
Always ensure that important data is backed up before performing any bulk deletion.
By being aware of the context, you can safely clean up empty files and directories when appropriate.
There are multiple backup applications exist. Please check our Backup category and pick one that suits your needs. Here's the list of backup tools that I personally tested.
Linux backup and synchronization guides:
- How To Mount Google Drive Locally Using Rclone In Linux
- How To Mount Dropbox Locally Using Rclone In Linux
- How To Transfer Files Between Dropbox And Google Drive With Rclone In Linux
- Vorta – A Graphical Frontend For BorgBackup
- How To Backup And Restore Files Using BorgBackup In Linux
- How To Backup Your Entire Linux System Using Rsync
- How To Backup Files And Directories Using Rsync In Linux
- How To Backup And Restore Linux System With Timeshift
- How To Backup And Restore Files Using Deja Dup In Linux
- How To Setup Backup Server Using Rsnapshot
- How To Synchronize Files With Unison On Linux
- How To Synchronize Local And Remote Directories In Linux
- How To Synchronize Files And Directories Using Zaloha.sh
- CYA – System Snapshot And Restore Utility For Linux
- Restic – A Fast, Secure And Efficient Backup Application
Conclusion
In this guide, we discussed how to find and delete empty files and directories using find
command in Linux and Unix-like operating systems. We also discussed when to delete such unused files and directories and when you shouldn't. I recommend you to follow my advice under the best practices section before deleting any files.
There are also other tools to achieve this task, but I think deleting unused files and folders using find
command is easier.