How to delete many 0 byte files in linux?

63,839

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.

Share:
63,839
small_ticket
Author by

small_ticket

Updated on July 05, 2022

Comments

  • small_ticket
    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
    Philipp almost 14 years
    I guess you should use double quotes: -name "file*" Otherwise the pattern will be expanded by the shell.
  • Martin Wickman
    Martin Wickman almost 14 years
    +1 for xargs. Much better than -exec. Consider use -print0 and -0 for safety.
  • Nathan Fellman
    Nathan Fellman almost 14 years
    This doesn't limit the rm to files with 0 bytes. To be fair, though, neither does the code the OP posted.
  • small_ticket
    small_ticket almost 14 years
    Thanks, i'm giving a shot for this one. I'll be posting the result.
  • Philipp
    Philipp almost 14 years
    You can use + instead of ; to have find call rm with multiple arguments instead of invoking a process for each file.
  • Didier Trosset
    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. See man xargs for more info.
  • small_ticket
    small_ticket almost 14 years
    The reason is why they are there is here: stackoverflow.com/questions/3157144/tomcat-creates-0-byte-fi‌​les I'm also trying to solve that problem
  • GreenMatt
    GreenMatt almost 14 years
    Nice - I didn't realize find had a delete action.
  • jim mcnamara
    jim mcnamara almost 14 years
    Only in GNU find. POSIX does not specify actions like -delete and -ls
  • small_ticket
    small_ticket almost 14 years
    edit: Yes, it worked!...(my mistake) Great thanks, it is not that fast but it is handy
  • andereld
    andereld almost 14 years
    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.
  • tumtumtum
    tumtumtum over 11 years
    You want to use -size 0c. -size 0 will include files less than 512bytes.
  • Didier Trosset
    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
    Skippy le Grand Gourou almost 10 years
    Note that obviously you don't need the -name 'file*' part if you don't filter by name.
  • seanbreeden
    seanbreeden almost 8 years
    This works for Ubuntu: find . -type f -name '*' -size 0 -print0 | xargs -r0 rm
  • Colin D
    Colin D almost 7 years
    To just make the copy-and-pasteable solution of Skippys comment, just use find . -size 0 -delete
  • alper
    alper over 5 years
    How could I use is on a specific folder path, something like: /home/user/file* @coredump
  • 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
    alper over 5 years
    Why should I use -print before -delete? As I understand correctly, if find ./ -type f -size 0 -print returns valid output then I can do: find ./ -type f -size 0 -delete @coredump
  • 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
    Kevin Genus over 4 years
    Warning: If you are new to POSIX based system, you should know device files in POSIX systems are 0 bytes in length. i.e. your keyboard is a device.
  • ctpenrose
    ctpenrose almost 4 years
    Should include -type f as seanbreeden suggests. otherwise can folders will be included and will abort the rm call prematurely