Search code examples
linuxbashsh

Using bash command to copy files from a subfolder to another


I have the following structure:

.
├── 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

I want to create a new folder getting only current spark jobs, my expected output folder is:

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

I've tried to use

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

Although my script is not writing the files and returns me no error. How can I solve this?


Solution

  • Use this, install command take the input file and copy it to another dir structure, creating the whole tree of dirs if necessary as mkdir -p transparently:

    (you need to add wildcard * in -wholename to effectively find files)

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

    Exemple of what is done:

    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
    

    The source path is an example, if longer, no issue.