I am attempting to make a script that will be able to load up some settings and files to a board. Basically I will plus a flash drive into the board click on the script. The script will then copy/move some files to replace old ones that are out of date. An example of this would be me replacing the /etc/apt/sources.list with a new one for different repositories. Is it better to copy or move a file to this location to replace it? I will also be installing apache2 in this script then moving over directories to the www and cgi-bin. Is it better to copy or move directories? I will also be changing permissions.
Basically my question is, is it better to move mv or copy cp files and directories when dealing with bash scripting?
2 Answers
Move if you only need it in the target path, copy if you need it in both locations. There's no inherent "insecurity" to mv or cp, and you'll be needlessly adding complexity (and extremely likely bugs) to your scripts if you start trying to implement your own mv.
For special cases there may be more suitable tools than either. For example, if you want to change owner or mode when copying there's the install tool which does that in one line rather than three.
It depends. In certain (usually rare) situations you should cp the file to a temporary name on the target filesystem, then mv it to it's final name, as the mv operation is atomic on a journaled file system. If there is any chance the application will try to read this file while you are replacing it, do this.
You specified that you are transferring the file from a different filesystem (flash drive). In this case mv is doing an implicit cp && rm. If you go back 20+ years mv didn't even support this, but there is no reason to be stuck in history.
3