AIX's native cp
utility does not include the --parent
option, as you've discovered.
One option would be to install and use rsync from the AIX Toolbox for Linux Applications software collection. You'd also need to install the popt RPM (as a dependency of rsync).
Then you could run:
rsync -R /some/path/to/file /newdir/
To end up with /newdir/some/path/to/file
.
As a home-grown option, you could write a wrapper function using ksh93 (for the array support) to emulate the behavior. Below is a bare-bones function as an example; it assumes you want to copy files with relative paths, and does not support any options:
relcp() {
typeset -a sources=()
[ "$#" -lt 2 ] && return 1
while [ "$#" -gt 1 ]
do
sources+=("$1")
shift
done
destination=$1
for s in "${sources[@]}"
do
if [ -d "$s" ]
then
printf "relcp: omitting directory '%s'\n" "$s"
continue
fi
sdir=$(dirname "$s")
if [ "$sdir" != "." ] && [ ! -d "$destination/$sdir" ]
then
mkdir -p "$destination/$sdir"
fi
cp "$s" "$destination/$sdir"
done
unset sources s sdir
}