3.3.2.2 Safe File Name Handling

Here is how to make find output file names so that they can be used by other programs without being mangled or misinterpreted. You can process file names generated this way by giving the ‘-0’ or ‘--null’ option to GNU xargs, GNU tar, GNU cpio, or perl.

Action: -print0

True; print the entire file name on the standard output, followed by a null character.

Action: -fprint0 file

True; like ‘-print0’ but write to file like ‘-fprint’ (see Print File Name). The output file is always created.

As of findutils version 4.2.4, the locate program also has a ‘--null’ option which does the same thing. For similarity with xargs, the short form of the option ‘-0’ can also be used.

If you want to be able to handle file names safely but need to run commands which want to be connected to a terminal on their input, you can use the ‘--open-tty’ option to xargs or the ‘--arg-file’ option to xargs like this:

find / -name xyzzy -print0 > list
xargs --null --arg-file=list munge

The example above runs the munge program on all the files named xyzzy that we can find, but munge’s input will still be the terminal (or whatever the shell was using as standard input). If your shell has the “process substitution” feature ‘<(...)’, you can do this in just one step:

xargs --null --arg-file=<(find / -name xyzzy -print0) munge