linux - 使用 bash 命令将文件从子文件夹复制到另一个

标签 linux bash sh

我有以下结构:

.
├── dag_1
│   ├── dag
│   │   ├── current
│   │   └── deprecated
│   └── sparkjobs
│       ├── current
│       |    └── spark_3.py
│       └── deprecated
│           └── spark_1.py
│           └── spark_2.py
├── dag_2
│   ├── dag
│   │   ├── current
│   │   └── deprecated
│   └── sparkjobs
│       ├── current
│       |    └── spark_3.py
│       └── deprecated
│           └── spark_1.py
│           └── spark_2.py

我想创建一个仅包含当前 spark 作业的新文件夹,我预期的输出文件夹是:

.
├── dag_1
|    └── spark_3.py
├── dag_2
     └── spark_3.py

我试过

find /mnt/c/Users/User/Test/ -type f -wholename "sparkjob/current" | xargs -i cp {} /mnt/c/Users/User/Test/output/

尽管我的脚本没有写入文件并且没有返回任何错误。我该如何解决这个问题?

最佳答案

使用这个,install 命令获取输入文件并将其复制到另一个目录结构,必要时创建整个目录树,如 mkdir -p 透明:

(需要在-wholename中加入通配符*才能有效查找文件)

find . -type f -wholename "*/sparkjob/current/*" -exec bash -c '
    dir=${1#./} dir=${dir%%/*} file=${1##*/}
    install -D "$1" "./$dir/$file"
' bash {} \;

完成的示例:

install -D ./dag_2/sparkjob/current/spark_3.py ./dag_2/spark_3.py
install -D ./dag_1/sparkjob/current/spark_3.py ./dag_1/spark_3.py

源码路径是一个例子,再长一点也没问题。

关于linux - 使用 bash 命令将文件从子文件夹复制到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75032555/

相关文章:

linux - 要显示的 Shell 脚本 - 内存使用情况、磁盘使用情况和 CPU 负载?

linux - ubuntu bash脚本备份/home/$user但保留最新的

linux - cabal-install 安装失败

mysql - 预定mysql修复的弊端?

bash - 查找:缺少 -exec 的参数

linux - 如何从脚本运行另一个脚本并自动回答提示

bash - 有没有等同于 bash 的 '!$' 的 vim?

linux - bash 脚本中的正则表达式(for 循环)

linux - Bash 脚本 : expansion of argument not using $@ or $*

php - 无法在 CentOs 7 中将 PHP 从 5.4 更新到更高版本