1

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?

  • If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you! – Jeff Schaller Jan 20 at 13:06
3

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
}
0

You could install AixTools, a gnu toolkit for AIX. http://www.aixtools.net/index.php/coreutils

Which includes cp among all the other tools you know and love.

0

As a two-step process, creating the target directory first (if it doesn't already exist), and then copying the file (if the mkdir succeeded).

dir=/some/path/to
mkdir -p "/newdir/$dir" && cp "$dir/file" "/newdir/$dir"

As a shell function (only handles copying a single file):

cp_parents () {
    source_pathname=$1
    target_topdir=$2

    mkdir -p "$target_topdir/${source_pathname%/*}" && cp "$source_pathname" "$target_topdir/$source_pathname"
}

Then,

$ cp_parents /some/path/to/file /newdir

Not the answer you're looking for? Browse other questions tagged or ask your own question.