开发者

How to delete many 0 byte files in linux?

开发者 https://www.devze.com 2023-01-05 14:06 出处:网络
I\'ve a directory w开发者_JAVA技巧ith 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

I've a directory w开发者_JAVA技巧ith 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?


Use find combined with xargs.

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

You avoid to start rm for every file.


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

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


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

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


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


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.


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.


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

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


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.


"...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;
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消