tags:

views:

136

answers:

8

Hi all,

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?

+2  A: 

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.

Sjoerd
I guess you should use double quotes: -name "file*" Otherwise the pattern will be expanded by the shell.
Philipp
This doesn't limit the `rm` to files with 0 bytes. To be fair, though, neither does the code the OP posted.
Nathan Fellman
You can use `+` instead of `;` to have `find` call `rm` with multiple arguments instead of invoking a process for each file.
Philipp
+8  A: 

Use find combined with xargs.

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

You avoid to start rm for every file.

Didier Trosset
+1 for `xargs`. Much better than `-exec`. Consider use `-print0` and `-0` for safety.
Martin Wickman
Thanks, Martin. I added the `-print0`, `-0`.
Didier Trosset
Thanks, i'm giving a shot for this one. I'll be posting the result.
small_ticket
@Martin: What's wrong with `-exec … +`?
Philipp
`-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. See `man xargs` for more info.
Didier Trosset
This doesn't work unfortunately
small_ticket
edit: Yes, it worked!...(my mistake) Great thanks, it is not that fast but it is handy
small_ticket
A: 

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.

Noel M
+1  A: 

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.

curious
+5  A: 

NB. there is no need to use xargs :

find -name 'file*' -size 0 -delete
coredump
Nice - I didn't realize find had a delete action.
GreenMatt
Only in GNU find. POSIX does not specify actions like -delete and -ls
jim mcnamara
A: 

"...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;
}
anders_we
This doesn't work unfortunately
small_ticket
Hm, 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.
anders_we
A: 

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.

Paul Rubel
The reason is why they are there is here: http://stackoverflow.com/questions/3157144/tomcat-creates-0-byte-filesI'm also trying to solve that problem
small_ticket
A: 

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

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

thegeek