LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   behavior of cp and mv in .sh files (https://www.linuxquestions.org/questions/linux-newbie-8/behavior-of-cp-and-mv-in-sh-files-937751/)

anon091 04-02-2012 04:29 PM

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?

Peverel 04-02-2012 05:46 PM

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.

TobiSGD 04-02-2012 05:52 PM

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
man mv


anon091 04-02-2012 05:54 PM

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

anon091 04-02-2012 05:56 PM

Thanks tobi. Good to know what stuff I can try out for this.

anon091 04-03-2012 09:51 AM

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?

TobiSGD 04-03-2012 10:37 AM

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.

anon091 04-03-2012 10:41 AM

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.