bash - 将多个文件移动到新目录保留路径

标签 bash awk

我想将一些文件从当前目录移动到一个新目录,同时保留它们的原始路径(例如 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/

相关文章:

bash - "unable to get local issuer certificate"在 vagrant up 期间,即使在 vagrant box add --insecure 之后

linux - 无法在 bash 脚本中正确设置环境变量 LD_PRELOAD

bash - Ubuntu使用脚本将JAVA_HOME路径添加到bashrc不起作用

linux - 重命名文件夹中的部分多个文件

Bash - 根据列在行中添加值

Bash Awk,记录的逆序

sed 和 awk : how to replace a section of file to another content?

python - 避免在 gitlab CI 脚本管道中提前退出命令,同时仍捕获退出状态

regex - AWK 正则表达式中的行尾符号

linux - 将 awk 结果存储为 bash ${} 数据格式