In a terminal I can run...

find . -type f -print0

And then...

find . -type f -printf "%p\0"

They both visually appear the same, but since this is about the null character, that doesn't say much. If I run via the ruby repl:

2.5.1 :001 > `find . -type f -print0`
 => "./testdir/testfile2.txt\u0000./testdir/testfile.txt\u0000" 

and then

2.5.1 :002 > `find . -type f -printf "%p\0"`
Traceback (most recent call last):
        3: from /usr/share/rvm/rubies/ruby-2.5.1/bin/irb:11:in `<main>'
        2: from (irb):2
        1: from (irb):2:in ``'
ArgumentError (string contains null byte)

What is the difference in what the -print0 option outputs vs printf?

Test system info:

uname: Linux XPS-15-9570 4.15.0-30-generic #32-Ubuntu SMP Thu Jul 26 17:42:43 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

ruby: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux].


There should be no difference. Pipe the output through cat -v which will escape non-printable characters.

Perhaps you have some special locale settings, which modifies what -print0 does. At least with my en_US.UTF-8 settings there is no difference. Perhaps add the output of locale to your question.

Possibly your test with ruby causes ruby to interpret the \0 itself, and find is not even executed.

  • 1
    Your theory about this invalid ruby test was correct. Thanks! Running the same test, but properly escaping the \0 in ruby did the trick. The following ruby then evaluates to true: find . -type f -printf "%p\\0" == find . -type f -print0 – user605331 Aug 13 '18 at 16:07

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.