Removing Null Characters

by shuba

I frequently need to remove null characters from a legacy encoded csv file for work. I usually run this command to do that find . -name "*.csv" -print0 | sudo xargs -n 1 -P 6 perl -pi -e 's/\0//g' The problem is that this command fails when a subdirectory has a name with whitespace characters e.g. 'This is a Directory'. I never name directories like this, but files downloaded from the internet often have this.

The fix, ironically, is to actually use null characters to delimit the find results with print0 to and specify the 0 flag with xargs so it knows to split on the null characters.

So this command fails:

find "ABCD New Directory" -name "*.csv" | xargs -n 1 -P 6 perl -pi -e 's/\0//g' Can't open ABCD: No such file or directory. Can't open New: No such file or directory. Can't open one_name.csv: No such file or directory. Can't open Directory/two: No such file or directory. Can't open ABCD: No such file or directory. Can't open New: No such file or directory. Can't open Directory/one_name.csv: No such file or directory.

But this one succeeds:

find "ABCD New Directory" -name "*.csv" -print0 | xargs -0 -n 1 -P 6 perl -pi -e 's/\0//g'