You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:使用Bash命令复制指定子文件夹文件失败的解决方法

问题分析与解决

原命令的问题

  1. 拼写错误:目录名是sparkjobs(复数),但你写的是sparkjob,导致路径匹配失败
  2. 类型与路径不匹配:你用-type f查找文件,但-wholename "sparkjob/current"匹配的是目录,所以find找不到任何符合条件的文件,自然没有复制操作
  3. 未处理目录结构:原命令会把文件直接复制到output根目录,无法生成你需要的dag_1/、dag_2/子目录结构

正确解决方案

方案一:用find+循环实现精确目录结构

先创建目标输出目录,然后遍历找到的目标文件,提取对应的dag_x目录名,创建子目录后复制文件:

# 先创建输出根目录
mkdir -p /mnt/c/Users/User/Test/output/

# 遍历所有sparkjobs/current下的py文件
find /mnt/c/Users/User/Test/ -type f -path "*/sparkjobs/current/*.py" | while read target_file; do
    # 从文件路径中提取dag目录名(比如dag_1、dag_2)
    dag_folder=$(echo "$target_file" | awk -F '/' '{print $(NF-3)}')
    # 创建输出目录下的dag子目录
    mkdir -p "/mnt/c/Users/User/Test/output/$dag_folder"
    # 复制文件到目标位置
    cp "$target_file" "/mnt/c/Users/User/Test/output/$dag_folder/"
done

方案二:用rsync快速筛选再调整结构

先用rsync筛选出需要的文件,再调整目录结构:

# 同步所有包含sparkjobs/current下py文件的目录结构
rsync -av --include='*/' --include='sparkjobs/current/*.py' --exclude='*' /mnt/c/Users/User/Test/ /mnt/c/Users/User/Test/output/

# 将每个dag下的py文件移动到dag目录根下
find /mnt/c/Users/User/Test/output/ -type f -name "*.py" | while read file; do
    target_dir=$(dirname $(dirname $(dirname "$file")))
    mv "$file" "$target_dir/"
done

# 删除空的sparkjobs目录及子目录
find /mnt/c/Users/User/Test/output/ -type d -name "sparkjobs" -empty -delete

执行任意一种方案后,你就能得到预期的目录结构:

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

内容的提问来源于stack exchange,提问作者OdiumPura

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 02:25:43