Subtle Coolness · chrono index · alpha index

You, too, can -print0

by William Jackson on 2016-11-18

Do you use the command line a lot? Do you search for files with find(1)? Do you send lists of files to xargs(1)? Do the files sometimes have names with spaces, apostrophes, and other special characters in them? Do you think it is great that find -print0 takes care of that when you produce filenames, and xargs -0 takes care of that when you consume them?

Do you write your own command line tools?

Do your command line tools generate lists of files? Can I send the output of your tools to xargs, even if the files have names with special characters in them?

You, too, can separate filenames in the output of your command line tools with a null character instead of a newline.

This is how I usually do it in Python:


import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-0', '--print0', action='store_true')
args = parser.parse_args()

for filename in generate_filenames_somehow():
    if args.print0:
        print(filename, end='\x00')