Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting empty (zero-byte) files

What's the easiest/best way to find and remove empty (zero-byte) files using only tools native to Mac OS X?

like image 945
Bank Avatar asked Feb 13 '09 01:02

Bank


People also ask

How do I delete 0 byte files from a folder?

via the Send To menu You can create a shortcut to the script in your SendTo folder and name it as Delete 0-byte Files. Prefix wscript.exe in the shortcut properties target field. Then, right-click on a folder where you want to delete empty files in the folder tree recursively → click Send To → click Delete 0-byte Files in the Send To menu.

How do I delete empty files in Linux terminal?

A command line window opens directly to the folder containing the Delete Empty executable. The format for the Delete Empty command is as follows: DelEmpty.exe OPTIONS [PATH] The following OPTIONS are available for use in the command: -f. delete files of size zero. -d. delete empty directories. -v.

How to delete empty files in Windows 10?

The tool will list empty files and folders in separate tabs. From the Empty Files tab, click Mark all Files and then click Delete Files. Similarly, to delete the 0-byte files in the selected folder tree, click on the Empty Files tab. Ashisoft.com has other awesome tools that you can check out! 2. Using Windows Search

How to delete a file if it is zero length?

You want to delete a specific file if it is zero length. So just substitute your file name for the * wild card. If you are going to use this in a batch file than you need to double all the percents ( %%F, %%~zF)


2 Answers

You can lower the potentially huge number of forks to run /bin/rm by:

find . -type f -size 0 -print0 | xargs -0 /bin/rm -f

The above command is very portable, running on most versions of Unix rather than just Linux boxes, and on versions of Unix going back for decades. For long file lists, several /bin/rm commands may be executed to keep the list from overrunning the command line length limit.

A similar effect can be achieved with less typing on more recent OSes, using a + in find to replace the most common use of xargs in a style still lends itself to other actions besides /bin/rm. In this case, find will handle splitting truly long file lists into separate /bin/rm commands. The {} is customarily quoted to keep the shell from doing anything to it; the quotes aren't always required but the intricacies of shell quoting are too involved to cover here, so when in doubt, include the apostrophes:

find . -type f -size 0 -exec /bin/rm -f '{}' +

In Linux, briefer approaches are usually available using -delete. Note that recent find's -delete primary is directly implemented with unlink(2) and doesn't spawn a zillion /bin/rm commands, or even the few that xargs and + do. Mac OS find also has the -delete and -empty primaries.

find . -type f -empty -delete

To stomp empty (and newly-emptied) files - directories as well - many modern Linux hosts can use this efficient approach:

find . -empty -delete
like image 102
Alex North-Keys Avatar answered Oct 15 '22 02:10

Alex North-Keys


Easy enough:

find . -type f -size 0 -exec rm -f '{}' +

To ignore any file having xattr content (assuming the MacOS find implementation):

find . -type f -size 0 '!' -xattr -exec rm -f '{}' +

That said, note that many xattrs are not particularly useful (for example, com.apple.quarantine exists on all downloaded files).

like image 37
Charles Duffy Avatar answered Oct 15 '22 01:10

Charles Duffy