LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > General
User Name
Password
General This forum is for non-technical general discussion which can include both Linux and non-Linux topics. Have fun!

Notices

Reply
 
Search this Thread
Old 11-05-2013, 02:24 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,125

Rep: Reputation: 46
Involuntary overwriting files when using cp.


How do you do in order not to overwrite some file when using cp? Say we have a source file we cp to an object directory which happens to have a file with the same name as the source file. According to the manual, cp will always overwrite it. Any easy oneliner which includes the cp command in it and warns the user before proceeding?
 
Old 11-05-2013, 02:44 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Quote:
Originally Posted by stf92 View Post
How do you do in order not to overwrite some file when using cp? Say we have a source file we cp to an object directory which happens to have a file with the same name as the source file. According to the manual, cp will always overwrite it. Any easy oneliner which includes the cp command in it and warns the user before proceeding?
The cp manual page mentions the -i and -n options.

I'm guessing the -i is what you are after (prompt before overwrite).
 
Old 11-05-2013, 02:46 AM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
from cp --help
Code:
...
  -i, --interactive            prompt before overwrite (overrides a previous -n
                                  option)
  -H                           follow command-line symbolic links in SOURCE
  -l, --link                   hard link files instead of copying
  -L, --dereference            always follow symbolic links in SOURCE
  -n, --no-clobber             do not overwrite an existing file (overrides
                                 a previous -i option)
  -P, --no-dereference         never follow symbolic links in SOURCE
  -p                           same as --preserve=mode,ownership,timestamps

...
 
Old 11-05-2013, 02:54 AM   #4
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,125

Original Poster
Rep: Reputation: 46
I was so certain cp provided no way to do it! Thanks and, by the way, how does the typical linux user use cp in this regard. Always with the -i option?
 
Old 11-05-2013, 03:07 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Quote:
Originally Posted by stf92 View Post
How does the typical linux user use cp in this regard. Always with the -i option?
I only use the -i option when absolutely necessary (for cp, mv, rm).

Some distro's make a default alias, for the root user, to include the -i option. One of the first things I do is remove that specific alias.

If you do want/need such an alias:
Code:
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'
Remember: Unix/Linux assumes that you know what you are doing (unlike Windows, which questions just about everything you do).

Last edited by druuna; 11-05-2013 at 03:41 AM. Reason: Added the rm command.
 
Old 11-05-2013, 03:28 AM   #6
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,125

Original Poster
Rep: Reputation: 46
OK. Now I have cp, which is just that alias, and suppose some other time I want to transitorily go back to normal, just to copy some file. I do not think there is an alternative other than unset the alias, given that options do not generally have a corresponding negative option (in this case, "option" 'do not prompt ...'). Am I right?
 
Old 11-05-2013, 03:34 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
You can use \cp to temporarily get rid of all aliases that are in place. This, the use of a leading backslash, isn't limited to the cp command.

EDIT: Some options override others. cp's -n overrides -i and if I'm not mistaken -f also overrides -i.

Last edited by druuna; 11-05-2013 at 03:37 AM.
 
  


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
[SOLVED] Overwriting free space or overwriting single files restored by photorec fcrok Linux - Security 22 09-15-2012 12:53 PM
[SOLVED] overwriting files that are being operated on captainentropy Programming 11 09-19-2011 10:37 AM
[SOLVED] rsync keeps overwriting some unmodified files figure002 Linux - Software 4 01-08-2010 01:16 PM
How to copy files without overwriting? Stephan_Craft Linux - Newbie 7 02-17-2009 05:28 AM
Please Help! Ubuntu keeps overwriting system files! ckr Ubuntu 2 07-13-2006 05:51 PM


All times are GMT -5. The time now is 03:16 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration