One thing you need to take into consideration before looking at the following answer is that you will effectively be recreating the whole path all the way from /
This will be a multi-step process, because you are looking to recreate the parent folder structure, and not precisely copy it and its contents. The structure is as follows
home/
├── fol_a
│ ├── fol_ABC
│ │ └── My Folder
│ │ └── file 2
│ └── fol_XYZ
│ └── My Folder
│ ├── file1
│ └── fol_extra
│ └── file 3
└── my test
11 directories, 3 files
in other words the full path of file1
is /home/fol_a/fol_XYZ/My Folder/file1
. Our goal is to move folders My Folder
and their contents, to the my test
folder such that their paths become /home/my test/home/fol_a/fol_XYZ/My Folder/*
and /home/my test/home/fol_a/fol_ABC/My Folder/*
1. Rebuild directory structure and copy appropriate folders and their contents
Running from the parent folder of the tree branch where folders of the same name are located (in our example it would be fol_a
, but in your example it would be /Media
) , you can do the following:
NOTE: export these variables , as DEST and SOURCE will be necessary for feeding variables into bash -c
of find
DEST=target path
SOURCE=name of folder to copy, with its full path recreated
BRANCH=the common branch of the same-named folders you wish to copy
export DEST="/home/my test"
export SOURCE="My Folder"
export BRANCH="/home/fol_a"
find "$BRANCH" -type d -name "$SOURCE" -exec bash -c 'mkdir -p "$DEST""$(realpath "{}")" ; cp -r "{}"/* "$DEST""$(realpath "{}")"' \;
NOTE: If you get a message like cp: cannot stat './[FOLDERNAME]/*': No such file or directory
**it is not an error, it just means that ** cp -r [FOLDERNAME]/*
command happened to execute on a folder that was empty
Check the recreated folder tree and contents in your destination folder
To quickly check the folder structure and the files to see if the copying went correctly you can run
tree "$DEST"/..
At this stage(before removing) your overall folder tree will look like so:
home/
├── fol_a
│ ├── fol_ABC
│ │ └── My Folder
│ │ └── file 2
│ └── fol_XYZ
│ └── My Folder
│ ├── file1
│ └── fol_extra
│ └── file 3
└── my test
└── home
└── fol_a
├── fol_ABC
│ └── My Folder
│ └── file 2
└── fol_XYZ
└── My Folder
├── file1
└── fol_extra
└── file 3
14 directories, 6 files
2. Remove the folder tree from the old common branch
Finally (and after double checking all the files are actually within appropriate folders etc.) if everything looks correct , and you are looking to truly move fol_d
and its contents (and not just copy) you can finish it off with the following
From the same previously declared common branch (in the above example it was /home/fol_A
)
find "$BRANCH" -type d -name "$SOURCE" -exec rm -r "{}" \;
NOTE: You might get messages like find [$SOURCE] :No such file or directory
but this will be more or less normal when deleting contents and directory using find
and -exec
directive.
One last check is to run tree
and the final output becomes:
home/
├── fol_a
│ ├── fol_ABC
│ └── fol_XYZ
└── my test
└── home
└── fol_a
├── fol_ABC
│ └── My Folder
│ └── file 2
└── fol_XYZ
└── My Folder
├── file1
└── fol_extra
└── file 3
11 directories, 3 files