I have a bash script (Scientific Linux). The script has to operate on a file. Let's say "file.dat" (around 1 GB of size) After some time the scripts is restarted and executes the following:
if [ -f file.dat ]; then
cp file.dat file.previous.dat
fi
to have a backup of the file. Then a process starts and overwrites "file.dat"
In order to be on the safest side (electricity shut down or anything unexpected). What would be the best option: cp or mv ? Thanks.
With cp you still have your complete original data in its original directory structure. With mv you have some of your data in the original dir, some in the target dir, and re-merging them may be difficult (esp. if the target dir contains other data).
The cp command will copy your file(s) while the mv one will move them. So, the difference is that cp will keep the old file(s) while mv won't.
I would use a combination:
mv file.dat file.dat.previous
cp file.dat.previous file.dat
That way file.dat.previous
will always be complete as mv
is atomic.
If you want a quick, atomic move, then mv is the thing to do since man 2 rename
says:
If newpath already exists it will be atomically replaced (subject to a few conditions; see ERRORS below), so that there is no point at which another process attempting to access newpath will find it missing.
Perhaps more importantly, mv is largely a directory entry operation, so it's very quick compared to a file copy in any normal circumstance.
If you're worried about power outages or unexpected system shutdowns, then:
The mv command should be faster, but robustness in the face of catastrophic failures is a hardware or filesystem issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With