简体   繁体   中英

Linux cp command to preserve file strucutre

So I have a linux file structure like this:

/base/dir_a/a/this_d=1_another/testfile
/base/dir_a/a/this_d=2_another/testfile
/base/dir_a/a/aa/this_d=1_another/testfile
/base/dir_a/a/ab/this_d=1_another/testfile
/base/dir_a/a/ab/this_d=2_another/testfile

I would like to copy over all folders containg d=1 into dir_b

/base/dir_a/*d=1* -> /base/dir_b

So the final dir_b should be:

/base/dir_a/a/this_d=1_another/testfile
/base/dir_a/a/aa/this_d=1_another/testfile
/base/dir_a/a/ab/this_d=1_another/testfile

I tried a copy like the following (2 commands..)

cp /base/dir_a/a/*/*d=1* -> /base/dir_b;
cp /base/dir_a/a/*d=1* -> /base/dir_b;

But this will attempt to just copy the this_d=1_another folder into /base/dir_b which will cause issues because all of my nested folders are named the same.

How can I approach this issue?

It's easiest to use rsync if that's available to you:

rsync -avP --include=*/ --include=**/*d=1* --exclude='*' /base/dir_a /base/dir_b

However, this will create a bunch of empty directories as a side-effect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM