How to delete many 0 byte files in linux?
Solution 1
Use find
combined with xargs
.
find . -name 'file*' -size 0 -print0 | xargs -0 rm
You avoid to start rm
for every file.
Solution 2
With GNU's find
(see comments), there is no need to use xargs :
find -name 'file*' -size 0 -delete
Solution 3
If you want to find and remove all 0-byte files in a folder:
find /path/to/folder -size 0 -delete
Solution 4
find . -maxdepth 1 -type f -size 0 -delete
This finds the files with size 0 in the current directory, without going into sub-directories, and deletes them.
To list the files without removing them:
find . -maxdepth 1 -type f -size 0
Solution 5
You can use the following command:
find . -maxdepth 1 -size 0c -exec rm {} \;
And if are looking to delete the 0 byte files in subdirectories as well, omit -maxdepth 1
in previous command and execute.
small_ticket
Updated on July 05, 2022Comments
-
small_ticket almost 2 years
I've a directory with many number of 0 byte files in it. I can't even see the files when I use the ls command. I'm using a small script to delete these files but sometimes that does not even delete these files. Here is the script:
i=100 while [ $i -le 999 ];do rm -f file${i}*; let i++; done
Is there any other way to do this more quickly?
-
Philipp almost 14 yearsI guess you should use double quotes: -name "file*" Otherwise the pattern will be expanded by the shell.
-
Martin Wickman almost 14 years+1 for
xargs
. Much better than-exec
. Consider use-print0
and-0
for safety. -
Nathan Fellman almost 14 yearsThis doesn't limit the
rm
to files with 0 bytes. To be fair, though, neither does the code the OP posted. -
small_ticket almost 14 yearsThanks, i'm giving a shot for this one. I'll be posting the result.
-
Philipp almost 14 yearsYou can use
+
instead of;
to havefind
callrm
with multiple arguments instead of invoking a process for each file. -
Didier Trosset almost 14 years
-exec
will start a new process with each argument.xargs
won't. This is a great improvement in the number of process to start, and a great improvement in execution time. Seeman xargs
for more info. -
small_ticket almost 14 yearsThe reason is why they are there is here: stackoverflow.com/questions/3157144/tomcat-creates-0-byte-files I'm also trying to solve that problem
-
GreenMatt almost 14 yearsNice - I didn't realize find had a delete action.
-
jim mcnamara almost 14 yearsOnly in GNU find. POSIX does not specify actions like -delete and -ls
-
small_ticket almost 14 yearsedit: Yes, it worked!...(my mistake) Great thanks, it is not that fast but it is handy
-
andereld almost 14 yearsHm, it works for me. It must have something to do with your other (Java/Selenium-related) problem. Either that, or the files you're trying to remove aren't regular files. I don't think the code is faulty.
-
tumtumtum over 11 yearsYou want to use -size 0c. -size 0 will include files less than 512bytes.
-
Didier Trosset over 11 years@tumtumtum It is true that
-size 0c
would be more correct (no unit specified default to number of blocks), but you're wrong in stating that-size 0
will include files less than 512 bytes. Indeed, as soon as a file is 1 byte in size, it occupies 1 block. -
Skippy le Grand Gourou almost 10 yearsNote that obviously you don't need the
-name 'file*'
part if you don't filter by name. -
seanbreeden almost 8 yearsThis works for Ubuntu:
find . -type f -name '*' -size 0 -print0 | xargs -r0 rm
-
Colin D almost 7 yearsTo just make the copy-and-pasteable solution of Skippys comment, just use
find . -size 0 -delete
-
alper over 5 yearsHow could I use is on a specific folder path, something like:
/home/user/file*
@coredump -
coredump over 5 years@alper
find
accepts a directory as its first argument, i.e.find /home/user/ -name "file* ..."
. Highly recommended: first use with-print
instead of-delete
, and then only when the result is satisfying, delete files. -
alper over 5 yearsWhy should I use
-print
before-delete
? As I understand correctly, iffind ./ -type f -size 0 -print
returns valid output then I can do:find ./ -type f -size 0 -delete
@coredump -
coredump over 5 years@alper Yes, first try with print (instead of delete), and if that works, you can delete safely (two separate commands)
-
Kevin Genus over 4 yearsWarning: If you are new to
POSIX
based system, you should know device files inPOSIX
systems are 0 bytes in length. i.e. your keyboard is a device. -
ctpenrose almost 4 yearsShould include
-type f
as seanbreeden suggests. otherwise can folders will be included and will abort the rm call prematurely