6

I have a directory containing many similar subdirectories Each of these subdirectories have some files I am interested in and some that I am not.

I want to copy:

FROM: two particular files: file1.txt file2.json from every subdirectory

TO: a different location

But making the subdirectories in that location

For example If I have

base
|_folder_00000001
|         |_-file1.txt
|         |_ file2.json
|         |_some_other_things_I_dont_need
|         |_some_other_folder_I_dont_need
|
|_folder_00000002
|         |_-file1.txt
|         |_ file2.json
|         |_some_other_things_I_dont_need
|         |_some_other_folder_I_dont_need
|
|_folder_00000003
|         |_-file1.txt
|         |_ file2.json
|         |_some_other_things_I_dont_need
|         |_some_other_folder_I_dont_need
|
|_folder_00000004
|         |_-file1.txt
|         |_ file2.json
|         |_some_other_things_I_dont_need
|         |_some_other_folder_I_dont_need
|

I want to copy this to another location but excluding the "some other things and folders I dont need"

target
|_folder_00000001
|         |_-file1.txt
|         |_ file2.json
|         
|
|_folder_00000002
|         |_-file1.txt
|         |_ file2.json
|             |
|_folder_00000003
|         |_-file1.txt
|         |_ file2.json
|             |
|_folder_00000004
|         |_-file1.txt
|         |_ file2.json
|         

How can I do this from the bash terminal?

7
  • 1
    Can you say what "copy a couple of files from all directories making those directories" means? Oct 24, 2022 at 21:29
  • 1
    @RobbieGoodwin: They did provide a clear example of desired result. I too found that description in words to be very unclear, so it's a good thing the example is clear. Not sure how I'd describe it concisely; perhaps "copy directory structure, keeping only some of the files"? Oct 24, 2022 at 23:42
  • 1
    @muru This isn't exactly the same as that other post. That other post was just asking how to combine creating a directory to copy the source file into while copying the source file. This post is about copying multiple files from mulitple directories into a different location, preserving the source directory structure in the new location.
    – B.Kaatz
    Oct 27, 2022 at 23:01
  • 1
    @RobbieGoodwin: Maybe I was biased by seeing answers exist to confirm my interpretation of the question, maybe even an accepted answer. But there isn't any "code" here, just a directory structure. And I think the only interpretation of the words that's compatible with both the words and the example is the one I and answerers took. It's not a bad thing to ask for clarification of the wording, that the example is correct, though. Nov 19, 2022 at 22:37
  • 1
    @Peter Cordes Thanks and I see there's nothing there but a directory structure with no room any lack of clarity… and here, don't you think the very wording "|_some_other_things_I_dont_need" itself works like code? Still, I'm sorry I failed to notice how closely patterned this particular structure is, which pretty-much nullifies everything I said. Duh! Nov 20, 2022 at 11:45

2 Answers 2

8

You can use cp with --parents:

cd base
cp -v --parents */file1.txt */file2.json ../target/
3
  • I tried it. This is almost what I wanted. the only thing is that this creates base inside target. Is there a way to have all the subdirectories under target? (no base in the middle) Oct 24, 2022 at 12:24
  • sure, fixed this
    – pLumo
    Oct 24, 2022 at 12:28
  • 1
    +1 for simplicity :-)
    – Raffa
    Oct 24, 2022 at 12:31
0

Is there some pattern to the files you want to copy?

From the example, if we take the "-file1.txt" and the "file2.json" as the files you want copied to the new directory structure, it can be done this way, I should think:

rsync -av --include="*/" --include="*-file1.txt" --include="*file2.json" --exclude="*" /src/dir/  /dest/dir/

I hope that helps.

1
  • Thanks! I like rsync, I have been extremely busy so could not try the solution, but I will. Oct 28, 2022 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.