LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   General (http://www.linuxquestions.org/questions/general-10/)
-   -   Involuntary overwriting files when using cp. (http://www.linuxquestions.org/questions/general-10/involuntary-overwriting-files-when-using-cp-4175483428/)

stf92 11-05-2013 03:24 AM

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?

druuna 11-05-2013 03:44 AM

Quote:

Originally Posted by stf92 (Post 5058580)
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).

Firerat 11-05-2013 03:46 AM

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

...


stf92 11-05-2013 03:54 AM

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?

druuna 11-05-2013 04:07 AM

Quote:

Originally Posted by stf92 (Post 5058597)
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).

stf92 11-05-2013 04:28 AM

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?

druuna 11-05-2013 04:34 AM

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.


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