I would like to move or copy some files with a particular extension that are missing in a new data update provided into another new directory preserving the subdirectories structure. I would like to do it with bash commands. I am using a MacBook Pro.
I got a structure like this:
/dir1/a/b/c/file1.json
/dir1/a/b/k/file2.json
/dir1/a/x/z/file3.json
/dir1/a/x/z/file4.zip
I would like to move ONLY those files ending with .json into another directory preserving the subdirectories. Regarding the other directory, it can contain other files in the subfolders.
In the new directory there can be also different files:
/dir2/a/b/c/file9.HTML
/dir2/a/b/k/file1.RAR
/dir2/a/x/z/file3.xls
/dir2/a/x/z/file4.XML
The movements will be like:
/dir1/a/b/c/file1.json -> /dir2/a/b/c/file1.json
/dir1/a/b/k/file2.json -> /dir2/a/b/k/file2.json
/dir1/a/x/z/file3.json -> /dir2/a/x/z/file3.json
/dir1/a/x/z/file4.zip -> no action as it is not .json
So the final status of the structure will be this:
/dir2/a/b/c/file9.HTML
/dir2/a/b/c/file1.json
/dir2/a/b/k/file1.RAR
/dir2/a/b/k/file2.json
/dir2/a/x/z/file3.xls
/dir2/a/x/z/file3.json
/dir2/a/x/z/file4.XML
You can use this find + gnu-cp
command with --parents
option:
find /dir1 -name '*.json' -exec cp --parents -t /dir2/ {} +
As per man cp
:
--parents use full source file name under DIRECTORY
Which will basically create same sub-directory structure in /dir2
for each *.json
file as in /dir1
.