LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-02-2012, 05:29 PM   #1
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,671

Rep: Reputation: 46
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?
 
Old 04-02-2012, 06:46 PM   #2
Peverel
Member
 
Registered: May 2009
Location: Chelmsford, England
Distribution: OpenSuse 12.2 and 13.2
Posts: 105

Rep: Reputation: 21
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.
 
Old 04-02-2012, 06:52 PM   #3
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831
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
 
1 members found this post helpful.
Old 04-02-2012, 06:54 PM   #4
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,671

Original Poster
Rep: Reputation: 46
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
 
Old 04-02-2012, 06:56 PM   #5
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,671

Original Poster
Rep: Reputation: 46
Thanks tobi. Good to know what stuff I can try out for this.
 
Old 04-03-2012, 10:51 AM   #6
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,671

Original Poster
Rep: Reputation: 46
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?
 
Old 04-03-2012, 11:37 AM   #7
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831
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.
 
1 members found this post helpful.
Old 04-03-2012, 11:41 AM   #8
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,671

Original Poster
Rep: Reputation: 46
Thanks, I was just trying it out as you replied and that's exactly what happened.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
3 files that control the behavior of a web site? evil_empire Linux - Newbie 5 08-27-2009 10:45 AM
FC9: sysconfig network files behavior hawk_turner Fedora 5 05-21-2008 09:21 AM
How do you change default emacs behavior on .h files? BrianK Programming 4 02-26-2008 08:11 PM
ls -l behavior vjl323 Mandriva 3 11-05-2003 08:30 AM
strange behavior abhijit Linux - General 3 07-10-2003 12:25 AM


All times are GMT -5. The time now is 04:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration