behavior of cp and mv in .sh files
Hi everybody. I'm looking into some cron'd .sh scripts on one of my servers, but had a real basic question to help me try to understand this mess i have.
If there is a cp or mv in a script, but the destination file already exists with the same name, by default does it overwrite, or will that fail to copy the new/updated file with the same name because 1 exists already? |
Why don't you try it and see? Create a couple of dummy files, write a short script to overwrite one with the other, and see what happens. That way, you teach yourself, more satisfactory than being taught.
|
The default behavior for mv and cp is to overwrite the existing files. You can use the options -b or --backup with those commands, in that case the commands will make a backup file (usually with the ~ suffix, but you can specify that when using the --backup option) of the already existing file before overwriting it.
For more info have a look at Code:
man cp |
Yeah, I will give it a try, just wondered what it should do or if there's any good things to do or not do, or what to expect
|
Thanks tobi. Good to know what stuff I can try out for this.
|
So I wouldn't need to throw a -f in there to force a .sh to overwrite the destination files with the same names that i'm trying to copy and move over? it would do it by default?
|
The -f option only applies when the already existent file can't be opened for whatever reason. That normally shouldn't happen, so -f shouldn't be needed.
|
Thanks, I was just trying it out as you replied and that's exactly what happened.
|
All times are GMT -5. The time now is 01:18 AM. |