我想将一些文件从当前目录移动到一个新目录,同时保留它们的原始路径(例如 example/app1/main.c
应该移动到 /newdir/example/app1/main.c
)。
我通过创建了所有必要的目录。以下内容:
git ls-files --others | awk -F '/' '{print "/home/user/newdir/"$1"/"$2}' | xargs mkdir -p
但我不确定如何强制 mv
使用 git ls-files --others
目录路径来确定新位置。
最佳答案
我通常这样做的一种方法是使用 tar,因为它非常适合保存文件。
tar -c -f - $(git ls-files --other) | tar -x -f - -C /newdir && git clean -df
或者类似的东西应该是你想要的。第一个 tar 在 stdout 上创建一个 tar,由第二个读取;第二个将其提取到目的地。之后,我们需要删除文件,因此需要 git clean。
请注意,如果您有很多文件,您可以将第一个 tar 分成两部分:
git ls-files > /tmp/files && tar -c -f - -T /tmp/files ...
关于bash - 将多个文件移动到新目录保留路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30654698/