Ubuntu: Copy all folders with name “ABC” to another location



Question:

I have 100 folders and every folder has a subfolder called "ABC". I want to copy folder structure and contents of "ABC" to another location.

How can I do it through Ubuntu terminal?


Solution:1

I'd use a find command something like this to target the deeper ABC directory, then mkdir -p to handle the directory structure, and finally rsync to mirror the contents (especially useful if you want to sync this up in the future).

find . -mindepth 2 -maxdepth 2 -type d -name ABC \  -exec mkdir -p ../target/{} \; \  -exec rsync -a {}/ ../target/{}/ \;  

Here's a test:

$ mkdir -p input/dir{1,2,3}/{ABC,DEF} target  $ touch input/dir{1,2,3}/{ABC,DEF}/file  $ tree  .  â"œâ"€â"€ input  â"‚   â"œâ"€â"€ dir1  â"‚   â"‚   â"œâ"€â"€ ABC  â"‚   â"‚   â"‚   â""â"€â"€ file  â"‚   â"‚   â""â"€â"€ DEF  â"‚   â"‚       â""â"€â"€ file  â"‚   â"œâ"€â"€ dir2  â"‚   â"‚   â"œâ"€â"€ ABC  â"‚   â"‚   â"‚   â""â"€â"€ file  â"‚   â"‚   â""â"€â"€ DEF  â"‚   â"‚       â""â"€â"€ file  â"‚   â""â"€â"€ dir3  â"‚       â"œâ"€â"€ ABC  â"‚       â"‚   â""â"€â"€ file  â"‚       â""â"€â"€ DEF  â"‚           â""â"€â"€ file  â""â"€â"€ target    $ cd input # we need to be in the parent dir for this to work  $ find . -mindepth 2 -maxdepth 2 -type d -name ABC -exec mkdir -p ../target/{} \; -exec rsync -a {}/ ../target/{}/ \;    $ tree ../target/  ../target/  â"œâ"€â"€ dir1  â"‚   â""â"€â"€ ABC  â"‚       â""â"€â"€ file  â"œâ"€â"€ dir2  â"‚   â""â"€â"€ ABC  â"‚       â""â"€â"€ file  â""â"€â"€ dir3      â""â"€â"€ ABC          â""â"€â"€ file  

You can also do this with a single rsync command:

$ rsync -rv --include '*/ABC/' --include '*/ABC/*' --exclude '*/*' . ../target  sending incremental file list  dir1/  dir1/ABC/  dir1/ABC/file  dir2/  dir2/ABC/  dir2/ABC/file  dir3/  dir3/ABC/  dir3/ABC/file  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »