"cp --parents" equivalent on Mac

2

1

The topic explains it -- basically I have a bash script used on a Linux system and a Windows system with Cygwin where the following command works perfectly:

cp --parents

However, running this same command on Mac in terminal gives the following error:

cp: illegal option -- -
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file
       cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directory

I am using the command in this context copying a select list of files into an output directory and retaining their directory structure:

cp --parents foo/gen1.file foo/bar/gen2.file foo/gen3.bar bar/foo/bar.file ~/my-output/

What can I do to get the cp --parents behavior?

udjamaflip

Posted 2013-12-06T02:12:11.297

Reputation: 103

If you are copying the whole directory structure from foo/ on down, then rsync -av foo ~/my-output/ would work for you. You can choose specific files with filter rules; but, that is starting to veer away from your original question. – Kent – 2013-12-06T05:50:13.223

Answers

1

I managed to synthesize this command on MAC by using the Rsync parameter -R to copy the relative directory structure of the file I was copying.

Syntax:

rsync -R <list of files to copy> <target dir>

Usage:

rsync -R foo/gen1.file foo/bar/gen2.file foo/gen3.bar bar/foo/bar.file ~/my-output/

This gets the same result though I'm sure there's probably a bit of overhead added from using RSYNC rather than CP

udjamaflip

Posted 2013-12-06T02:12:11.297

Reputation: 103

0

--parents is probably a non-POSIX behavior specific to GNU cp, whereas the BSD-derived cp in OS X doesn't have it.

You can probably get the functionality you want by installing the GNU cp on your Mac and using that. It's probably part of a GNU package called fileutils, and you can probably install it with Homebrew, MacPorts, or Fink.

Spiff

Posted 2013-12-06T02:12:11.297

Reputation: 84 656

0

Do you have basename ? If so:

$ for i in `ls foo/gen1.file foo/bar/gen2.file foo/gen3.bar bar/foo/bar.file`; do cp $i ~/my-output/`basename $i`; done

malat

Posted 2013-12-06T02:12:11.297

Reputation: 981