9

Using a shell script or Terminal, how do I overwrite files that may or may not be present. I looked at man page for cp, it said use the -f argument to force overwrite. But, it does nothing.

$ cp -f /path/to/source/file.txt /path/to/target

Or is there another option to overwrite files using the common in-built command line tools in OS X?

  • 3
    It works for me. What is it doing instead of overwriting when you try it? – Gordon Davisson Dec 6 '13 at 1:32
  • 2
    If you don't have permission to overwrite the target file, this will fail. If you really want to overwrite a target file, you can use sudo at the beginning of the command. But use with caution! – Daniel Dec 6 '13 at 1:36
  • @Gordon Davisson: What's it doing? nothing. It will only copy the files to the target directory if I mv them or rm them. – rogerFernand Dec 10 '13 at 18:50
  • @Daniel Lawson: Without using sudo I can use mv or rm to move or remove files before using cp. Using sudo cp makes no difference. – rogerFernand Dec 10 '13 at 18:54
  • Curiouser and curiouser. – Daniel Dec 10 '13 at 20:04
13

I'm stumped as to why cp is behaving this way if there's no alias involved. However, there is a quick-dirty-and-dangerous utility provided for this exact situation: yes. You can use it to pipe a continuous stream of affirmatives into any command that gives you confirmation prompts (you can use it to send any text, but the default is "y").

yes | cp foo bar will execute the cp command and bypass all confirmation prompts.

8

It might be caused by a command alias.

To call cp directly you can use /bin/cp src dest

  • 1
    Not sure why this was downvoted. I was under the impression that explicitly specifying -f would override -i in the alias, but I checked and it does not. The poster should find out whether they have cp aliased (type alias cp). They can change the alias in their profile or use /bin/cp as suggested. – octern Dec 6 '13 at 18:50
  • I am using default cp as Apple provided it. – rogerFernand Dec 10 '13 at 18:47
  • @octern: roger$ alias cp -bash: alias: cp: not found – rogerFernand Dec 10 '13 at 19:25
  • I had to use /bin/cp I don't know why. – beingalex Aug 20 '14 at 20:59
  • 1
    I might be way too late but for people coming from search engine I was able to see if the command had any alias to it by doing type cp. And it indeed did. – shriek Oct 26 '17 at 23:23
2
rm foo/bar/destination && cp source foo/bar/destination

First you remove (rm) the destination, then if successful (&&) you do your copying. When removing a folder, please remember to add -r to rm (rm -r).

1

I also had an alias messing up with -f, call cp directly from /bin and all is fine, having an alias that prevents accidental overwrites/removals by default is pretty good, so best not remove it.

.# alias

alias cp='cp -i'

like already stated, the -i flag superceedes -f, which wouldn't allow below command to perform as intended.

.# /bin/cp -fauv src dst

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .