0

I want to use terminal to copy a folder structure with specific folders inside and its content only.

Structure:

Parentsource folder 
 Child one (child can be any names)
    Folder one (this folder will always have the same name)
    Folder two
    Folder three
 Child two 
    Folder one
    Folder two
    Folder three
 Child three 
    Folder one
    Folder two
    Folder three

Desired outcome:

targetfolder  
Child one 
    Folder one
      All content
 Child two 
    Folder one
      All content
 Child three 
    Folder one
      All content

today I use this command to copy the full structure with empty subfolder and then manuallly remove/add contents to the right folder:

Cd sourcefolder
find . -type d -exec mkdir -p ~/path-target/{} \;

But would like to streamline it to select one of the child folders and its content.

Whats the best approach?

4
  • Something like cd parent; for d in child*; do mkdir target/$d; cp -r $d/folder1 target/$d/; done, with some more thoughts on error handling and taking care of proper quoting?
    – nohillside
    Feb 1, 2022 at 10:49
  • Forgot - the child folders will have different name. Updated the question
    – Jerry
    Feb 1, 2022 at 10:59
  • 1
    Please use the real names then in your example, to avoid confusion. And replace the pattern in the for loop :-)
    – nohillside
    Feb 1, 2022 at 11:00
  • The names is different so dont have a live example at the moment. Updated my structure in the question though.
    – Jerry
    Feb 1, 2022 at 11:02

1 Answer 1

1

The question is a bit short on details so the following needs to be taken with a grain of salt and thoroughly tested before actually using it.

cd .../parent-dir
for d in *; do
    if [ -d "$d" -a -d "$d/Folder one" ]; then
        mkdir ".../target/$d"
        cp -r "$d/Folder one" ".../target/$d/"
    fi
done

You must log in to answer this question.

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