Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symlink-copying a directory hierarchy

What's the simplest way on Linux to "copy" a directory hierarchy so that a new hierarchy of directories are created while all "files" are just symlinks pointing back to the actual files on the source hierarchy?

cp -s does not work recursively.

like image 724
Max Spring Avatar asked Aug 06 '09 18:08

Max Spring


People also ask

How do I copy a directory with symbolic links?

Use cp -P (capital P) to never traverse any symbolic link and copy the symbolic link instead. This can be combined with other options such as -R to copy a directory hierarchy — cp -RL traverses all symbolic links to directories, cp -RP copies all symbolic links as such.

How do I copy a folder hierarchy?

Type "xcopy", "source", "destination" /t /e in the Command Prompt window. Instead of “ source ,” type the path of the folder hierarchy you want to copy. Instead of “ destination ,” enter the path where you want to store the copied folder structure. Press “Enter” on your keyboard.

How do I copy a directory hierarchy in Linux?

In order to copy a directory on Linux, you have to execute the “cp” command with the “-R” option for recursive and specify the source and destination directories to be copied. As an example, let's say that you want to copy the “/etc” directory into a backup folder named “/etc_backup”.

Can symbolic links be copied?

We can use the -l option of rsync for copying symlinks. rsync copies the symlinks in the source directory as symlinks to the destination directory using this option. Copying the symlinks is successful in this case.


2 Answers

I just did a quick test on a linux box and cp -sR /orig /dest does exactly what you described: creates a directory hierarchy with symlinks for non-directories back to the original.

like image 166
freiheit Avatar answered Oct 04 '22 01:10

freiheit


cp -as /root/absolute/path/name dest_dir 

will do what you want. Note that the source name must be an absolute path, it cannot be relative. Else, you'll get this error: "xyz-file: can make relative symbolic links only in current directory."

Also, be careful as to what you're copying: if dest_dir already exists, you'll have to do something like:

cp -as /root/absolute/path/name/* dest_dir/ cp -as /root/absolute/path/name/.* dest_dir/ 
like image 43
PonyEars Avatar answered Oct 04 '22 00:10

PonyEars