It's possible to fail if a file exists on Linux, via a slight hack with GNU
stdin so any interactive prompts are ignored, but treats the use of
stdin as a failure.
$ nohup cp -ia foo/* bar/
To clean up a little bit:
$ nohup cp -ia foo/* bar/ 2>nohup.out && rm nohup.out || cat nohup.out
- By default
stderr into the file as well.
&& rm || cat will clean up on success or output the error from
nohup.out on error. You can add whatever error handling you want instead of/including the
cat or remove all of that and deal with
$? as normal.
- You will need to be a bit smarter about the temp file location if you use this in earnest (
On BSD you can redirect
cp which will be treated as
n and return a non 0 status.
$ cp -ia foo/* bar/ </dev/null
cp behaves differently to BSD, surprisingly, and does return a non 0 status with
-n on a skipped file.
$ cp -n foo/* bar/