Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cp --parents option on mac

On Linux, I have a --parents option available for the cp command so I can do

cp --parents test/withintest/go.rb test2

http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html

On Mac, I do not have this option available. Is there a way to do this on Mac? Why is this option not available?

PS. The purpose of --parents is the following:

‘--parents’ Form the name of each destination file by appending to the target directory a slash and the specified name of the source file.

The last argument given to cp must be the name of an existing directory.

For example, the command:

      cp --parents a/b/c existing_dir 

copies the file a/b/c to existing_dir/a/b/c, creating any missing intermediate directories.

like image 992
dmonopoly Avatar asked Jun 28 '12 13:06

dmonopoly


People also ask

What does the -- parents option to cp do?

You can use cp –parents option to copy the full source file name under DIRECTORY.

What does cp mean in Mac?

Copy a file or folder locally In the Terminal app on your Mac, use the cp command to make a copy of a file. For example, to copy a folder named Expenses in your Documents folder to another volume named Data: % cp -R ~/Documents/Expenses /Volumes/Data/Expenses. The -R flag causes cp to copy the folder and its contents.


7 Answers

This bothered me quite a lot as well. A workaround for this could be to use rsync.

rsync -R test/withintest/go.rb test2

has the same effect as cp --parents and OS X comes standard with rsync.

like image 101
Stephan Avatar answered Oct 06 '22 00:10

Stephan


You can use the ditto command on Mac OS X:

The basic form

ditto <src-path> <dst-path>

does what you want. There's a lot more options too - check out the man page.

like image 21
Echelon Avatar answered Oct 05 '22 23:10

Echelon


You can install the GNU version of cp using MacPorts.

After MacPorts is installed you can install the coreutils packages:

sudo port install coreutils

Then you will be able to use the GNU version cp and other core utilitites (ls, date, cat, etc.) by prefixing the command with a g:

gcp --parents test/withintest/go.rb test2

If you want these GNU versions to be used by default you can add the GNU bin update your path. Add the following to your ~/.bash_profile:

export PATH="/opt/local/libexec/gnubin:$PATH"
like image 44
Kara Avatar answered Oct 06 '22 00:10

Kara


The Homebrew way:

Install coreutils

brew install coreutils

Use the GNU g- prefixed command

gcp --parents test/withintest/go.rb test2
like image 30
Martin Peter Avatar answered Oct 05 '22 23:10

Martin Peter


I used rsync and what I did was:

rsync -R dir/**/file.json destination

like image 40
lucasdeassis Avatar answered Oct 06 '22 00:10

lucasdeassis


Try

mkdir -p `dirname "$file_path"` && cp "$old_dir/$file_path" "$file_path"

This first creates the directory with all itermediates in the relative file path. Then it copies the file to the newly created directory.

like image 35
Edward Ji Avatar answered Oct 06 '22 00:10

Edward Ji


I would not replace mac cp with GNU cp. I would also not used ditto because it is not cross-platform. Instead use cross-platform tools, such as rsync:

rsync <srcDir/srcFile> <dst>

Result: dst/srcDir/srcFile

like image 31
vikrantt Avatar answered Oct 05 '22 22:10

vikrantt