I have several sparse files on a server running red-hat. I want to look at the files without having to see the zeroed bytes. When I less the files, I see a lot of '@^'s where the zeroed bytes exist. When I cat the files none of the zeroed bytes are displayed. However, when I cat and pipe that to less, the zeroed bytes are displayed again.

Why is this happening? How do I get around this to view the files using less, and not have the zeroed bits displayed?

migrated from stackoverflow.com Mar 12 '12 at 16:18

This question came from our site for professional and enthusiast programmers.


I'm sure the zero bytes are displayed also when you use cat. You just don't see them because they're... zero bytes, and your terminal displays nothing when fed zero bytes.

On the other hand, whether a file is in fact stored sparsely is in implementation detail of the filesystem. Tools like less and cat don't know (indeed can't know) and don't care if the file is sparse. If the file happens to contain a run of zero bytes, then so be it. It could have contained instead a run of bytes whole value is 1 instead, and it would be all the same to them.

If what you want to do is filter out zero bytes and display all the remaining bytes with less, just use a filter to delete those bytes:

tr -d \\000 < inputfile | less

The reason why cat is not showing the zeroed bytes is that it does not escape anything and your terminal does not print zero bytes at all.

You could remove the zero bytes for viewing in less like this:

cat $filename | tr -d "\\0" | less

Replace $filename with the file's name. This will not modify the file (and may take a while for large binary files).

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.