Odd need: to track down NUL bytes

Nils Kassube kassube at gmx.net
Sun Feb 5 19:35:57 UTC 2017

Kevin O'Gorman wrote:
> I now discover that some files that were under construction at that
> time are corrupted with extensive NUL bytes.  These are usually text
> files.  I want to find all such files and "cleanse" them to recover
> what information I can.  With just about anything _except_ a NUL byte
> this would be easy, but due to the special treatment of NUL in
> strings, it's not that clear to me how best to even search for those
> bytes.

Well, I also don't know how to search for those files, but you could at 
least remove the NUL bytes with something like this:

cat infile | tr -d '\0' >outfile

Or perhaps you prefer to replace the NUL bytes with something else, like 
space - that would be like this:

cat infile | tr '\0' ' ' >outfile

Now, you could just do this operation for all files, regardles if they 
contain NUL bytes:

for f in *;do test -f "$f" && cat "$f" | tr '\0' ' ' >"outdir/$f";done

This would place a copy of each file in the existing folder "outdir" 
with the NUL bytes replaced by spaces (if there are any).


More information about the ubuntu-users mailing list