我的脚本应该使用cp还是mv更健壮?

esp*_*ahg 6 linux error-handling bash robustness fault

我有一个bash脚本(Scientific Linux).该脚本必须对文件进行操作.假设"file.dat"(大小约1 GB)一段时间后脚本重新启动并执行以下操作:

if [ -f file.dat ];  then
    cp file.dat file.previous.dat
fi
Run Code Online (Sandbox Code Playgroud)

备份文件.然后一个进程启动并覆盖"file.dat"

为了最安全的一面(电力关闭或任何意外).什么是最好的选择:cp还是mv?谢谢.

Tho*_*hor 5

我会用一个组合:

mv file.dat file.dat.previous
cp file.dat.previous file.dat
Run Code Online (Sandbox Code Playgroud)

那种方式file.dat.previous总是完整的,就像mv原子一样.