find Verified current stable Not installed? Programming

Find / Print Fields With Nul Terminated Lines

Print Fields With Nul Terminated Lines

Extract fields from nul-terminated strings in command output.

$
Terminal
<find . -print0> | cut -z -d "</>" -f <2>

When To Use

When interfacing with tools that output null-terminated strings, particularly in file searching or processing scenarios.

Pro Tip

Use the `-print0` flag with `find` to ensure filenames with spaces are correctly handled without losing data during parsing.

Terminal Output

Expected runtime feedback

Simulated preview
>
Output
./file1.txt\0./file2.txt\0./subdir/file3.txt\0

Anatomy of Output

Understanding the result

/path/to/file1/path/to/file2/path/to/file3 Captured Lines

Output represents different file paths separated by null characters.

/path/to/file2 Output Field 2

The second field extracted from the nul-terminated output.

/path/to/file3 Output Field 3

The third field, to be captured if specified.

Troubleshooting

Common pitfalls

cut: 1: No such field

Solution: Verify the field number corresponds with the separated fields.

cut: Invalid delimiter

Solution: Make sure the input is read as null-terminated; check the command generating output.

find: invalid option

Solution: Ensure that the options used in `find` are correct and match the expected format.

Command Breakdown

What each part is doing

<find
Base Command
The executable that performs this operation. Here it runs Find before the shell applies any redirect operators.
<find . -print0>
find . print0
The value supplied for find . print0.
-z
z| zero terminated
The value supplied for z| zero terminated.
-d
d| delimiter
The value supplied for d| delimiter.
</>
Input Value
The value supplied for this command input.
-f
f| fields
The value supplied for f| fields.
<2>
2
The value supplied for 2.
-print0>
Command Option
Tool-specific option used by this command invocation.
-z
Command Option
Tool-specific option used by this command invocation.
-d
Command Option
Tool-specific option used by this command invocation.
-f
Command Option
Tool-specific option used by this command invocation.

How To Run

Execution path

  1. Step 1

    Run the command: find . -print0 | cut -d '/' -f 2

  2. Step 2

    Verify the output using: echo $OUTPUT | xargs -0 -n1

  3. Step 3

    Check for displayed fields correctly separated by nul characters.

Alternative Approaches

Comparable commands in other tools

Alternative programming tools for the same job.