复制文件并保留完整路径

Luk*_*uke 5 directory cp aix filenames

我需要将文件及其完整路径复制到目标文件夹。我可以在 Linux(Red Hat/Centos)上轻松做到这一点,如下所示:

cp --parents /some/path/to/file /newdir
Run Code Online (Sandbox Code Playgroud)

然后我在目标目录中得到如下内容:

/newdir/some/path/to/file
Run Code Online (Sandbox Code Playgroud)

我在 AIX 6.1 上需要完全相同的功能。我尝试了一些事情但还没有成功。有什么方便的命令来完成这项工作的想法吗?

Jef*_*ler 6

正如您所发现的, AIX 的本机cp实用程序不包含该--parent选项。

一种选择是安装并使用AIX Toolbox for Linux Applications软件集合中的 rsync。您还需要安装 popt RPM(作为 rsync 的依赖项)。

然后你可以运行:

rsync -R /some/path/to/file /newdir/
Run Code Online (Sandbox Code Playgroud)

最终以/newdir/some/path/to/file.


作为一个自行开发的选项,您可以使用 ksh93(用于数组支持)编写一个包装函数来模拟该行为。下面是一个简单的函数作为示例;它假设您要复制具有相对路径的文件,并且不支持任何选项:

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
}
Run Code Online (Sandbox Code Playgroud)