Score:7

How can I copy files from many subdirectories to another directory while preserving directory structure?

cn flag

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?

David avatar
cn flag
Please post the script you have already written. Then from there someone may be able to see what errors it may contain.
Robbie Goodwin avatar
ug flag
Can you say what "copy a couple of files from all directories making those directories" means?
Peter Cordes avatar
fr flag
@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"?
B.Kaatz avatar
jp flag
@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.
Robbie Goodwin avatar
ug flag
@PeterCordes Thanks and I, too, see "copy directory structure, keeping only some of the files" as a reasonable, but not a necessary conclusion. Don't you think people whose descriptions in words seem unclear, also generally produce unclear code and that the two should normally be taken together?
Peter Cordes avatar
fr flag
@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.
Robbie Goodwin avatar
ug flag
@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!
Score:9
in flag

You can use cp with --parents:

cd base
cp -v --parents */file1.txt */file2.json ../target/
cn flag
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)
pLumo avatar
in flag
sure, fixed this
Raffa avatar
jp flag
+1 for simplicity :-)
Score:0
jp flag

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.

cn flag
Thanks! I like `rsync`, I have been extremely busy so could not try the solution, but I will.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.