How to delete many 0 byte files in linux?

LinuxFileFindXargsDelete File

Linux Problem Overview


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?

Linux Solutions


Solution 1 - Linux

Use find combined with xargs.

find . -name 'file*' -size 0 -print0 | xargs -0 rm

You avoid to start rm for every file.

Solution 2 - Linux

With GNU's find (see comments), there is no need to use xargs :

find -name 'file*' -size 0 -delete

Solution 3 - Linux

If you want to find and remove all 0-byte files in a folder:

find /path/to/folder -size 0 -delete

Solution 4 - Linux

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.

Solution 5 - Linux

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 6 - Linux

Delete all files named file... in the current directory:

find . -name file* -maxdepth 1 -exec rm {} \;

This will still take a long time, as it starts rm for every file.

Solution 7 - Linux

you can even use the option -delete which will delete the file.

from man find, -delete Delete files; true if removal succeeded.

Solution 8 - Linux

Here is an example, trying it yourself will help this to make sense:

bash-2.05b$ touch empty1 empty2 empty3
bash-2.05b$ cat > fileWithData1
Data Here
bash-2.05b$ ls -l
total 0
-rw-rw-r--    1 user group           0 Jul  1 12:51 empty1
-rw-rw-r--    1 user group           0 Jul  1 12:51 empty2
-rw-rw-r--    1 user group           0 Jul  1 12:51 empty3
-rw-rw-r--    1 user group          10 Jul  1 12:51 fileWithData1
bash-2.05b$ find . -size 0 -exec rm {} \;
bash-2.05b$ ls -l
total 0
-rw-rw-r--    1 user group          10 Jul  1 12:51 fileWithData1

If you have a look at the man page for find (type man find), you will see an array of powerful options for this command.

Solution 9 - Linux

"...sometimes that does not even delete these files" makes me think this might be something you do regularly. If so, this Perl script will remove any zero-byte regular files in your current directory. It avoids rm altogether by using a system call (unlink), and is quite fast.

#!/usr/bin/env perl
use warnings;
use strict;

my @files = glob "* .*";
for (@files) {
    next unless -e and -f;
    unlink if -z;
}

Solution 10 - Linux

Going up a level it's worth while to figure out why the files are there. You're just treating a symptom by deleting them. What if some program is using them to lock resources? If so your deleting them could be leading to corruption.

lsof is one way you might figure out which processes have a handle on the empty files.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsmall_ticketView Question on Stackoverflow
Solution 1 - LinuxDidier TrossetView Answer on Stackoverflow
Solution 2 - LinuxcoredumpView Answer on Stackoverflow
Solution 3 - LinuxGiangimgsView Answer on Stackoverflow
Solution 4 - LinuxjitendraView Answer on Stackoverflow
Solution 5 - Linuxuser7194913View Answer on Stackoverflow
Solution 6 - LinuxSjoerdView Answer on Stackoverflow
Solution 7 - LinuxthegeekView Answer on Stackoverflow
Solution 8 - LinuxNoel MView Answer on Stackoverflow
Solution 9 - LinuxandereldView Answer on Stackoverflow
Solution 10 - LinuxPaul RubelView Answer on Stackoverflow