Search code examples
bashmacosshellcopyosx-elcapitan

Copy command with `cp -f -R` strangely does not work on all OS X machines


I am on OS X El Capitan. I have 2 directories SourceDir & DestDir with the following structures.

ParentDir/SourceDir |-s_dir1/ss_dir1
                    |-s_dir2

ParentDir/DestDir |-s_dir1/ss_dir2
                  |-ddir1
                  |-ddir2
                  |-ddir3

I want to do a copy command in such a way that s_dir1 in ParentDir/SourceDir gets merged with s_dir1 in ParentDir/DestDir and additionally, s_dir2 gets placed into ParentDir/DestDir. So, after the copy ParentDir/DestDir should look like this:

ParentDir/DestDir |-s_dir1/ss_dir1
                  |-s_dir1/ss_dir2
                  |-s_dir2
                  |-ddir1
                  |-ddir2
                  |-ddir3

I use the following command to copy:
cp -f -R ParentDir/SourceDir/ ParentDir/DestDir/

It works all fine on MacOS Sierra. But strangely it doesn't work on an OS X machine with MacOS El Capitan. Again, I tried it with a MacOS El Capitan on my colleague's machine. It worked fine!!!

What is wrong?

Do different versions of MacOS El Capitan behave differently to cp command?

Or, do I need to change the copy command syntax on El Capitan?

How can I do a copy command on my MacOS El Capitan machine to ensure the correct recursive copy at least on all OSX machines?

Please do not suggest to upgrade the El Capitan machine to Sierra. That is not an option for me. Hence posted this question to get some other syntax options of copy command.


Solution

  • It appears that the standard cp command is being overridden by something else. You can force it to use the standard cp command by explicitly specifying /bin/cp. You could also try to override the current override by using something like an alias, but it'd really be better to find out what the current override is, where it's coming from, and (maybe) getting rid of it.

    The first thing to do is run type cp its output tells you what cp is currently doing:

    • If it prints something like "cp is /usr/local/bin/cp", then you've got a custom-installed version of cp that's overriding the standard one... and is apparently causing problems. Your best option here is to find out what's installed the nonstandard cp command (maybe homebrew?) and remove it.
    • If it prints something like "cp is aliased to <some command string>", then you have an alias defined in one of your shell initialization files (or one of the files they run, etc). Check your ~/.profile, ~/.bash_profile, ~/.bash_login, and ~/.bashrc for the source of the alias definition.
    • If it prints "cp is a function", then you have a function defined in one of your shell init files. Check as you would for an alias definition.