The shuf command has an --output flag that you can use to specify where to write its output (instead of writing to stdout).

I want to shuffle a file in-place. Is it safe to use shuf file --output file, using the same filename for the output filename and the file to read from?

Or could this cause file corruption? Is it platform-specific? It seems to work OK when I tested on a small file, but the man page doesn't say anything about this, and I'm not sure whether to worry about it.


Yes - per the documentation - it should be safe to shuffle a file in-place:

-o output-file
Write output to output-file instead of standard output. shuf reads all input before opening output-file, so you can safely shuffle a file in place by using commands like shuf -o F <F and cat F | shuf -o F.


As you said, spot-testing on a small file seems to do what you want, but if you want to be safe, the best option would be to write to a temporary file and then swap the new one in:

trap 'rm -f "$scratch"' EXIT
if [[ -r "$1" ]]; then
    shuf "$1" --output "$scratch" && mv "$scratch" "$1"

The above script can be saved as e. g. shuf_in_place in your PATH.

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.