Wednesday, April 21, 2010

LINUX: Copy Files With Structure

Copying select files from one folder to another while preserving the files in same structure/hierarchy can be done simply within the find command.

If you have files in directory trunk that need to be copied to another folder called branch that already has the same structure, first make sure trunk and branch are at the same level, then cd into trunk. Next run the below command to take all files of .mp* format (that is for example, you can do something else) from one to the other.

find -type f -iname '*.mp*' -exec cp -p {} ../branch/{} \;

If the structure is NOT the same, you will need to create the appropriate structure FIRST. Do the following from within the trunk directory:

find -type d -exec mkdir -p ../branch/{} \;

No comments:

Post a Comment