My application X organizes projects in folders with a specific tree. The first level of the tree is defined (hard constraint), while all the contents are freely organized.
My goal is to iterate over all folders of the project tree, parse folder names to get targets based on conditions, then copy the selected targets to a new project folder, keeping the tree structure, so that application X is not going to complain.
So far, I've come up with this:
cp -pr `find . -type d | grep -i targetstring` ../newProjectFolder/
This has issues: 1) it doesn't preserve folder tree; 2) throws a lot of errors cp: cannot stat
(see also this). It isn't just working as intended.
Moreover, parsing the output of find
is generally a bad idea.
I was able to overcome first problem with --parents
as suggested here. However, I'm copying way less than what I need due to point 2, so not a solution after all.
I am looking for a one-liner solution to the problem. Script based solution is available here. EDIT: as this was marked as duplicated of the link provided, I'll rephrase a little: I want a solution where I don't need to explicitly while and if-else my way to it. Piping find
and grep
provides all the locations to be copied, but cp
is complaining. How do we feed them to cp
, without it complaining?
I was looking for something using -exec
as suggersted here, and came up with the following, which is not working (nothing gets copied) (only partial tree structure is copied, and files inside that structure are only copied if they match the regex as well):
find . -regex ".*targetstring.*" -exec cp -rp --parents \{\} ../newProjectFolder \;