xargs错误地将我的src目录复制到一个tgt目录 [英] xargs is incorrectly copying my src dir to one tgt directory
问题描述
This question builds off of a previous question. I do not want to edit the previous question because then I would be introducing a new complexity to a question that already has SO many comments. It would skew the context of the current answers.
以下是名为mapping.txt
src/a/ tgt/aye/
src/b tgt/b/
src/c/ tgt/c
源文件结构如下
/tmp/testzone/src/a/afile.txt
/tmp/testzone/src/b/bfile.txt
/tmp/testzone/src/c/cfile.txt
我们要做的第一件事是基于mapping.txt
文件创建目标目录文件夹结构
First thing we do is create the target directory folder structure based on the mapping.txt
file
cut -f 2 mapping.txt | tr '\n' '\0' | xargs -0 mkdir -p
哪个将创建目标目录文件夹结构
Which creates the target directory folder structure
/tmp/testzone/tgt/aye/
/tmp/testzone/tgt/b/
/tmp/testzone/tgt/c/
接下来我们运行
xargs --arg-file mapping2.txt cp -a
似乎将源目录的内容放入目标目录的c
目录中.这是错误的.它应该使用mapping.txt
文件逐行从源复制到目标.
Which seems to put the source directory contents into the target directory's c
directory. This is wrong. It should be using the mapping.txt
file to copy from source to target line by line.
/tmp/testzone/tgt/aye/
/tmp/testzone/tgt/b/
/tmp/testzone/tgt/c/a/afile.txt
/tmp/testzone/tgt/c/aye/
/tmp/testzone/tgt/c/b/bfile.txt
/tmp/testzone/tgt/c/c/cfile.txt
什么可能导致此问题?
推荐答案
在没有其他指示的情况下,xargs
会将输入文件中尽可能多的行作为参数放在单个cp
调用中.这就是
In the absence of options to instruct it otherwise, xargs
will put as many lines of possible from the input file as arguments to a single cp
invocation. This is what xargs
was designed to do.
您可能会找到一种向正确方向轻推的方法,但这似乎只是您应该做的
You can probably find a way to nudge it in the right direction, but this looks like you should simply be doing
while read -r src dst; do
mkdir -p "${dst%/*}"
cp -a "$src" "$dst"
done<mapping2.txt
也许已经在注释中建议了install
来进一步简化此操作.
Maybe look at install
to further simplify this, as already suggested in a comment.
这篇关于xargs错误地将我的src目录复制到一个tgt目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!