"cp --parents" equivalent on Mac

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?

1

3 Answers

--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.

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

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like