Skip to content

How to Preserve Directory Path with CP Command

Advertisement

The cp command is used to copy files from one directory to another directory. Some times you are required to copy a file with its parent directory structure.

You can use -r option to copy files recursively. But if we need to copy a single file with the full directory structure. Use cp command with --parents option to use full source file name under the directory and create a similar directory structure on the destination.

cp --parents ./dir1/dir2/myfile.txt /opt/

Now list the file on the destination directory. You can see that the same directory structure is created in the destination directory.

ls -l /opt/dir1/dir2/myfile.txt

-rwxr-xr-x 1 root root 4864094 Jan 15 11:09 /opt/dir1/dir2/myfile.txt

Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top