copy the file and keep full path

6,540

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
}
Share:
6,540

Related videos on Youtube

Luke
Author by

Luke

Updated on September 18, 2022

Comments

  • Luke
    Luke almost 2 years

    I need to copy the file to destination folder with it's full path. I can do it on Linux (Red Hat/Centos) easily like this:

    cp --parents /some/path/to/file /newdir
    

    Then I get, in destination directory, something like this:

    /newdir/some/path/to/file
    

    I'd need exactly the same feature on AIX 6.1. I tried few things but didn't succeed yet. Any ideas for handy command to do the job?

    • Jeff Schaller
      Jeff Schaller over 5 years
      If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!