Remove files of 0 bytes in size via command line?
So, I got a directory filled with other directories, and I was wondering if it was possible to remove files that have no size. Typically these files are 0 bytes and since I want to merge all these subdirs I could replace a perfectly legit file with a weightless 0 byte file, and there goes my legit file. Any way to remove the zero byte files?
Use the Find command to find files by size and print file names to standard output.
find . -type f -size 0b -print
substitute -print
with -delete
to delete the files rather than print them on screen.
find . -type f -size 0b -delete
Find and remove all files with a size of 0 recursively:
find . -type f -size 0 -print0 | xargs -I{} -0 rm {}
Example:
% ls -og
total 4
-rw-rw-r-- 1 0 Jun 7 20:31 bar
-rw-rw-r-- 1 5 Jun 7 20:29 foo
% find . -size 0 -print0 | xargs -I{} -0 rm {}
% ls -og
total 4
-rw-rw-r-- 1 5 Jun 7 20:29 foo
You can also do it directly in the shell. This could be useful if you don't want to delete empty hidden files (those whose name begins with a .
). While you could do that with find
as well, an alternative would be to use the shell itself:
shopt -s globstar
for file in **/*; do [ ! -s "$file" ] && [ -f "$file" ] && rm "$file"; done
Explanation
-
shopt -s globstar
: turns on the globstar option for bash which makes**
match one or more subdirectories.**/*
will match all files and directories in the current directory and all its subdirectories. -
for file in **/*; do ...; done
: iterate over all files and directories found; -
[ ! -s "$file" ]
:[ -s "$file" ]
is true if the file exists and is not empty. Therefore,[ ! -s "$file" ]
(the!
inverses the test) is true if the file doesn't exist or if it is empty. -
[ -f "$file" ]
: true if the file is a regular file. Not a directory or a device file or a symlink etc. -
rm "$file"
: delete the file.
The &&
ensure that the next command is only run if the previous one was successful so this will only delete empty, regular files.
Although most answer above is correct, take a look for this command:
A 0 byte sized file means an empty file
though you can run this command:
find . -type f -empty -delete
this will delete all empty files.
You can take a look for those files before delete:
find . -type f -empty