0

I have a directory structure like this: m/n/o/p/q/r/s/t.

I want to find directories that match by name "r" and copy contents recursively from the parent-dir of the find-result (ie. "q/r/s/t" in this case - what would be achieved with 'cp -r q/r <dir_location>') onto another location.

Something like:

find m/n/o/p -type d -name "r" -exec cp -r "../{}" /<folder_to_copy_into> \;

I've tried experimenting with -printf %h and -maxdepth, but I'm not sure I have the correct syntax. I'm not sure if I should be piping into another find to get my intended result. (Goal is to only copy from one-parent-dir-above-from-find-results recursively into the other dir location).

As long as I can find dir named "r", I want to recursively copy everything from r's parent-dir onto a new dir-location. (eg: if I have m/n/o/p/q/r/s/t, I want "q/r/s/t" to be recursively copied onto the new dir-location).

1
  • 1
    "Everything from r's parent-dir" is r and other files in q, but not q itself. Then you say "I want q/r/s/t to be recursively copied. So you do want to copy q. Please clarify. It's "copy contents of q (i.e. everything from q)" vs "copy q". Commented Apr 7, 2021 at 19:59

2 Answers 2

0

You could replace your -exec with a bash command string:

 .... -exec bash -c 'cp -r $(dirname $0) /target/' '{}' \;

Please take @KamilMaciorowski's comment into consideration: it is a bit unclear what you are trying to extract. So searching for d1 in a directory structure like this

└ a
  └ b
    ├ c1
    │ └ d1
    │ └ d2
    │ └ d3
    ├ c2
    └ c3

the above will copy c1 with d2 and d3 included, not only d1.

0

This worked for me:

find m/n/o/p -mindepth 1 -maxdepth 1 -type d -exec [ -d {}/r ] ';' -exec cp -r {} <folder_to_copy_into> \;)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .