I have hundreds of directories, each with one or more .js or .ts files in them but also other files. Many of them are nested multiple levels deep. What I am trying to do is copy each sub directory to a new directory with the directory contents, but only with the .js or .ts files in them.
Here is what I have so far:
find "." -path "*node_modules*" -prune -o -type f \( -iname \*.ts -o -iname \*.js \) -printf "%h\0" | sort -uz | xargs -0 -I _ cp -a _ "/c/test/"
What this is currently doing is copying all directories (excluding the node_modules directories) of file type .ts or .js, and copying them to the /c/test/ directory. However, the problem I am having is that for each directory, it is copying ALL the files AND all the subdirectories when I only want it to copy the .ts or .js files located within that directory. And here is the most important part, for every directory it copies, no matter how deep or shallow that directory is, I only want it to copy that directory. I don't want to copy a directory with all of the subdirectories located in it. However, I want to copy every sub directory directory no matter how deeply nested it is. Hopefully this makes sense.
Imagine my directory structure is your dresser. Inside each drawer are several other drawers. Some of the drawers have items inside but also other drawers. One could simply empty all the items on the floor, but that's not what I want to do. I want to empty all the DRAWERS on the floor, keeping the items inside, but no drawer should contain another drawer. But the end result is every single drawer that has items is on the floor.
In other words, the end result should be a /test directory with a bunch of copied directories inside, but each of those directories has NO sub directories inside of them. They should only have js or ts files inside. I hope this makes sense.