Remove empty files in home directory

how to remove empty files tried below command its remove only zero bytes not empty file which is greater then zero byte.

for x in *
 do
    if [ -s $x ]
    then
       rm $x
    fi
 done

Please explain the difference between an empty and a zero byte file. Show an "empty file which is greater then zero byte".

if will create emty file using touch command it will create empty file with zero size and if we will create emty file in vi editor then size will be greater then 0 with no data

If you create an empty file in vi , then it will also be zero bytes in size..

Any file with one or more bytes (even spaces!) in it is NOT an empty file. It may not contain meaningful or readable or interpretable data, nor even a (complete) line, but systemwise it will be treated as a non-empty file.

We can find zero size file using below comand if we want search 0 size and along with empty file
(File not contain any data may be space which is greater then 0 size ) want to remove
those file in my home directory

find . -type f -size 0 -exec rm {} \;

I'm not really sure of the point you're trying to make. An empty file is a zero length file. A file that isn't zero-length, whether it contains only spaces or not, is not an empty file. If you want to remove files that you consider to be "empty", then you'll need to check those those files.

I appreciate that English may not be your first language, but some punctuation would make it simpler to parse your statements.

Hi,

There's no easy way to do what you want to do with find , or at least not based on a simple -type test. As you've previously been advised, an empty file is defined as a file containing 0 lines and whose contents are 0 bytes in length - in other words, a file that is empty. A file that contains anything else is not empty, and no UNIX utility will ever treat it as such.

The problem you have here is that you're not really working with the correct definition of "empty", as far as UNIX itself is concerned. An empty file is one which contains literally and solely nothing whatsoever, and whose size on disk is always zero bytes. If it contains anything else it's not empty, and you'll have to test for whatever else you want to find on a case-by-case basis, being very careful indeed not to match any legitimate files you wouldn't want to remove.

For example, whilst it's easy to search for all files with a line consisting of a single space (e.g. grep -l ^\ $ * ), the grep utility operates on a line-by-line basis. So whilst this would indeed return the names of files which had a line consisting of a single space, this wouldn't take into consideration any lines before or after such a matching line which might contain legitimate text. There's no conceptually-easy way to model what you want to find, since by definition the files you're looking at are not actually empty.

The only thing that comes close to what you want would be if you know that the files you regard as "empty" are always no more than one line in length. You could at least return all files that consist of only one single line by doing something like:

find . -type f -exec /usr/bin/wc -l \{\} \; | awk '$1 <= 1 {print $2}'

This would find all files in or beneath the current working directory, and only print their filename if they consisted of 0 or 1 lines. But again note that this will not be paying any attention to the actual content of those lines - the files in question could consist of one single line of legitimate text, so you'd have to add further tests to check that they only contained whitespace. But this should be enough to get you started, with any luck.

2 Likes

What about non-space non-printable characters? Would you consider a file with 2 or more <linefeed> chars a removal candidate although it has two+ empty lines? Or, a <BEL> (0x07, ^G, \a) char?

1 Like