-1

I have a lot of empty files in various sub directories of my Linux file system. How can I delete only empty files using the rm command?

I'm tired of deleting by going to all the directories and finding the empty files to manually delete, so I've found a combination of commands like find -size 0 -type f | rm -f. But I need to delete all the empty files in all the directories, is that possible using only the one rm command?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Jaiveer
  • 35
  • 2
  • 11
  • https://www.cyberciti.biz/faq/howto-find-delete-empty-directories-files-in-unix-linux/ – Ankur Jyoti Phukan Jul 24 '17 at 08:28
  • possible duplicate of https://stackoverflow.com/questions/3157343/how-to-delete-many-0-byte-files-in-linux – Ayushya Jul 24 '17 at 08:31
  • 2
    Possible duplicate of [How to delete many 0 byte files in linux?](https://stackoverflow.com/questions/3157343/how-to-delete-many-0-byte-files-in-linux) – dlmeetei Jul 24 '17 at 08:33
  • 1
    You can not do with `rm` alone. I will use `find /path/to/dir -empty -type f -delete` if I am in your shoes – dlmeetei Jul 24 '17 at 08:34
  • 1
    Do not forget that `-delete` goes **last** (otherwise it will start deleting at the point in the `find` expression where `-delete` appears) – David C. Rankin Jul 24 '17 at 08:38

2 Answers2

1

I don't think rm allows selecting file on the basis of their size. However, if you want to use just one command, you can use find

find /path/to/dir -type f -empty -delete

-type f is necessary because also directories are marked to be of size zero. And -delete should go at last.

However you may be wanting to delete all files irrespective of their directory, it is not advisable to do so, because there are many system files and some symlinks also which might be deleted in the process.

Ayushya
  • 9,599
  • 6
  • 41
  • 57
  • tested. This should be what you are looking for. I was looking for similar commands a while back and I had this but mine uses xargs. – dat789 Jul 24 '17 at 08:41
  • Running `find / -type f -empty -delete` might have bad impact as it will remove all empty which which services also depends. Hence, you need to select the path, instead of `/` path – dlmeetei Jul 24 '17 at 08:57
  • @dlmeetei is correct! And although you may be wanting to delete all empty files irrespective of their directory, there are many important files and possibly some symlinks also which might get deleted in the process. It would be better if you select the directory yourself. – Ayushya Jul 24 '17 at 09:05
  • May be you should update those info in your answer :) – dlmeetei Jul 24 '17 at 09:08
0

Well, rm(1) command only deletes the files whose names you pass to it on the command line. There's no code in rm(1) to allow you to filter those files, based on some condition or criteria. The old UNIX filosophy mandates here, write simple tools and couple them on pipes as neccessary to construct complex commands. In this case, find(1) is the answer... this is a tool to select files based on quite arbitrary criteria (like the one you ask for) and generate the actual file names or simply call commands based on that. On this respect

find dir1 dir2 ... -type f -size 0 -print | xargs rm

would be the solution (batching the filenames with xargs(1) command to do less fork(2) and exec(2) calls with less process fork overhead) to your problem, allowing to specify several dirs, selecting only files of size 0 and passing them to the batch command xargs(1) to erase them in groups. you can even filter the filenames based on some regular expression with

find dir1 dir2 ... -type f -size 0 -print | grep 'someRegularExpression' | xargs rm

and you'll get erased only the files that match the regular expression (and the other two predicates you expressed in find(1)) You can even get a list of the erased files with

find dir1 dir2 ... -type f -size 0 -print | grep 'someRegularExpression' | tee erased.txt | xargs rm

See find(1), grep(1), tee(1), xargs(1) and rm(1) for reference.

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31