Find files below uploads, including names containing spaces.
Pass each path safely to sha256sum.
Which command is correct?
find -print0 and xargs -0 use NUL delimiters, preserving spaces and other characters in filenames.
Detailed explanation
find uploads -type f -print | xargs sha256sumIncorrect. Ordinary xargs splits whitespace and can break filenames.
Incorrect. Ordinary xargs splits whitespace and can break filenames.
find uploads -type f -print | sha256sumIncorrect. sha256sum would hash the path text from standard input, not open each file.
Incorrect. sha256sum would hash the path text from standard input, not open each file.
find uploads -type f -print0 | xargs -0 sha256sumCorrect. Both commands use NUL boundaries, so each path remains one argument.
Correct. Both commands use NUL boundaries, so each path remains one argument.
find uploads -type f -print | xargs sha256sum '{}'Incorrect. {} is not a replacement token in ordinary xargs and splitting remains unsafe.
Incorrect. {} is not a replacement token in ordinary xargs and splitting remains unsafe.
Try it yourself
An example you can run in a temporary verification environment.
mkdir -p uploads
printf alpha > 'uploads/report one.txt'
printf beta > 'uploads/report two.txt'
find uploads -type f -print0 | xargs -0 sha256sumExpected result
2行のハッシュ値と、次の2つのパスがそれぞれ1引数として表示される
uploads/report one.txt
uploads/report two.txtKey points
- NUL-delimited paths
- The -0 option to xargs
- Why ordinary whitespace splitting is unsafe
Notes
- Environment: GNU findutils 4.9以降 / GNU coreutils
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.