LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 04:29 PM   #1
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Rep: Reputation: 49
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, 05:46 PM   #2
Peverel
Member
 
Registered: May 2009
Location: Chelmsford, England
Distribution: OpenSuse 12.2 and 13.2, Leap 4.2
Posts: 128

Rep: Reputation: 24
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, 05:52 PM   #3
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
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, 05:54 PM   #4
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
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, 05:56 PM   #5
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
Thanks tobi. Good to know what stuff I can try out for this.
 
Old 04-03-2012, 09:51 AM   #6
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
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, 10:37 AM   #7
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
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, 10:41 AM   #8
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

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


Reply



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 09:45 AM
FC9: sysconfig network files behavior hawk_turner Fedora 5 05-21-2008 08:21 AM
How do you change default emacs behavior on .h files? BrianK Programming 4 02-26-2008 07:11 PM
ls -l behavior vjl323 Mandriva 3 11-05-2003 07:30 AM
strange behavior abhijit Linux - General 3 07-09-2003 11:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:41 AM.

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
Open Source Consulting | Domain Registration