1

I have a folder that contains hundreds of thousands of small .txt files of size 4kb. Some of them are totally empty while others are not. However, they all show the same size 4kb since this is the minimum block size configured. Is there a way to massively delete the empty ones but keep the non empty? Doing an

rsync --remove-sent-files -rv --min-size=4kb

removes everything.

Thank you

1

I think I found a partial answer. If we suppose that a txt file with an empty content has at least one hidden character, then someone can find all files with size under a specific number of bytes and delete these. If for example we would like to delete all files of size 1 byte we can issue:

find . -type f -size -2c -delete
0

The truly empty files will show 0 bytes, when looked at with ls -l.

You can remove them with find (read man find https://manpages.ubuntu.com/manpages/cosmic/en/man1/find.1.html):

find folder -maxdepth 1 -type f -name '*.txt' -size 0 -delete
  • Thank you for your comment, however the files that I have do not show size 0 but size 4kb. When I open them there is no content but probably some hidden characters. – pebox11 Jun 25 at 20:13
  • Try running od -bc on one of the files. Or, use /bin/ls -l. Even a zero-byte file uses a 4K disk block, so that a write to it won't fail due to disk full. You're confused about the difference between "data in file (0 bytes)" and "filesystem space allocated". – waltinator Jul 4 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.