Copy the files in directory and sub folders as it is to another directory.

How to copy files from one directory to another directory with the subfolders copied.

If i have folder1/sub1/sub2/* it needs to copy files to folder2/sub1/sub2/*.
I do not want to create sub folders in folder2.
Can copy command create them automatically?

I tried cp -a and cp -R but did not work.

Thanks,

Like this? This one-liner will find all files recursively under folder1/ and copy them into folder2/ without creating sub-folders.

find ./folder1/ -type f -exec cp {} ./folder2/ \;
1 Like

It has to create sub folders.

i m using the command
find /directory1/sub1/ -name $file -exec cp -p {} /directory2/lib/ \;

but in the "sub1" folder i have subfolders and files, which are copied as all files in the single folder "lib" but not creating the subfolders present in the "sub1". I need those subfolders also to be created and copied.

Contradicting requirements.

And surprising that cp -R didn't work.

cp -R directory1/sub1 directory2/lib
1 Like

Sorry that i did not put that in a clear way. I do not want to create the folders by myself. I want the copy command to create itself the subfolders and copy the files.