Linux: how to delete only empty files or directories
Recently I had to deal with old “sins”, I had some scripts that did not behave very well and potentially leave empty directories on the…
Recently I had to deal with old “sins”, I had some scripts that did not behave very well and potentially leave empty directories on the filesystem, of course, old sins will bite you someday, and now I had to deal with this problem ASAP, thankfully the good people who made the find command have an excellent solution to this problem.
Example: How to find and delete only empty directories or files.
Find provides the -empty option to return only empty files and directories, to narrow this only to files or directories we can use the -type option.
This will return only empty directories
$ find . -empty -type dThis will return only empty files
$ find . -empty -type fTo delete the findings you need to use one of the above commands with the -delete parameter
$ find . -empty -type d -deleteBe careful with the -delete option and make sure that you are sure that these are the files or directories you found before using it
I hope you found this short article useful :)