I tried to even use the cp -p
command as another answer had suggested here: Linux: copy and create destination dir if it does not exist, but for some reason on my bash shell on mac, it doesn't actually copy the parent folder over.
-
The `-p` options is for `mkdir`, not `cp`. – Barmar Apr 13 '15 at 04:27
-
GNU `cp` also has a `-p` option, but OS X doesn't use the GNU version. – Barmar Apr 13 '15 at 04:28
1 Answers
You also have functions available to you to create your own copydeep routine if you like. There are several ways to handle making it available. The first being your ~/.bashrc
file, the second being exporting the function within your shell. In your bashrc you can simply declare the function to make it available. To export the function from the command line, simply enter the function (or copy/paste from your favorite editor) and then export the function with export -f funcitonname
. (Bash)
Note I haven't tested on OSX, just Linux, but as long as you have similar export capability, it, or something like it, should work.
A quick hack at a copydeep function named cpd
. (You can also enter it under a longer name, like copydeep
and alias cpd='copydeep'
as well)
Enter/paste the function:
$ cpd () {
[ -z $1 ] || [ -z $2 ] && {
printf "usage: cpd file /path/path1/etc...\n";
return 1
}
mkdir -p "$2" || {
printf "error: unable to create directory '%s' (check write permission)\n" "$2";
return 1
}
cp "$1" "$2"
}
Export it:
$ export -f cpd
Testing/Use:
$ cpd tmp.c this/long/path
$ ls -l this/long/path/
-rw-r--r-- 1 david david 1161 Apr 13 00:01 tmp.c
$ cpd
usage: cpd file /path/path1/etc...
Note: this is just intended as an example of a basic copy with minimal input testing. The use and specific tests and options for mkdir
and cp
are up to you. Of course the third option is simply save the contents of the function as a script, make it executable and able to be found in your path, and call it as needed.

- 81,885
- 6
- 58
- 85