22

I want to copy a certain file to a location, irrespective of that file already exists in the destination or not. I'm trying to copy through shell script.But the file is not getting copied. I'm using the following command

/bin/cp -rf /source/file /destination

but that doesn't work.

3
  • maybe you should try --remove-destination Commented Dec 13, 2012 at 8:39
  • 1
    cp overrides the destination unless option -n is given, so your command should succeed. Also you didn't tell us why you think your command fails. Commented Dec 13, 2012 at 8:43
  • Are there any error messages? Commented Dec 13, 2012 at 8:43

5 Answers 5

28

Use

cp -fr /source/file /destination

this should probably solve the problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you explain why reversing the order of the flags makes a difference?
17

This question has been already discussed, however you can write a little script like this:

#!/bin/bash
if [ ! -d "$2" ]; then
  mkdir -p "$2"
fi
cp -R "$1" "$2"

Explaining this script a little bit

  1. #!/bin/bash: tells your computer to use the bash interpreter.

  2. if [ ! -d "$2" ]; then: If the second variable you supplied does not already exist...

  3. mkdir -p "$2": make that directory, including any parent directories supplied in the path.

    Running mkdir -p one/two/three will make:

    $ mkdir -p one/two/three
    $ tree one
    one/
    └── two
        └── three
    

    If you don't supply the -p tag then you'll get an error if directories one and two don't exist:

    $ mkdir one/two/three
    mkdir: cannot create directory ‘one/two/three’: No such file or directory
    
  4. fi: Closes the if statement.

  5. cp -R "$1" "$2": copies files from the first variable you supplied to the directory of the second variable you supplied.

    So if you ran script.sh mars pluto, mars would be the first variable ($1) and pluto would be the second variable ($2).

    The -R flag means it does this recursively, so the cp command will go through all the files and folders from your first variable, and copy them to the directory of your second variable.

Comments

15

Your problem might be caused by an alias for cp command created in your system by default (you can see al your aliases by typing "alias"). For example, my system has the following alis by default: alias cp='cp -i', where -i overrides -f option, i.e. cp will always prompt for overwriting confirmation.

What you need in such case (that'll actually work even if you don't have an alias) is to feed "yes" to that confirmation. To do that simply modify your cp command to look like this:

yes | cp /source/file /destination

Comments

0

/bin/cp -rf src dst or /usr/bin/env cp -rf

1 Comment

this is because OP could unknowingly have cp aliased to 'cp -i', and I think some (all?) systems will prioritize the -i option. For example, on my system: > which cp alias cp='cp -i' /bin/cp > cp abc.txt efg.txt cp: overwrite efg.txt'? n > cp -f abc.txt efg.txt cp: overwrite efg.txt'? n
-1

just 1 prefix backslash

\cp <source_file> <destiation addr>

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.