Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy a directory with symbolic links and resolve them?

I would like to recursively copy the contents of a directory which contains symbolic links (symlinks) as well as normal files with a Bash / Shell Script. I don’t know how to copy the symlink-contents. The pseudocode would look something like this:

for file in directory do
  if is symlink
    resolve symlink and copy its contents
  else 
    copy the file / folder

My directory-structure looks like this:

base/
  dir1/
  symlinkdir1*/ (--> ../somewhere/else/dirA)
    /file1
    /file2
  symlinkdir2*/ (--> ../somewhere/else/dirB)
    /file3
    /file4
  …

After the copy-procedure, I would like to have a directory-structure like this:

base/
  dir1/
  symlinkdir1/ (no symlink, actual directory)
    /file1
    /file2
  symlinkdir2/ (no symlink, actual directory)
    /file3
    /file4
  …
like image 767
Pwdr Avatar asked May 18 '14 21:05

Pwdr


People also ask

How do I copy a directory with symbolic links?

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. rsync copied file1 to destination_directory.

Can you symbolic link a directory?

A symbolic link, also known as a soft link or symlink, is a special file pointing to another file or directory using an absolute or relative path. Symbolic links are similar to shortcuts in Windows and are useful when you need quick access to files or folders with long paths.

How do you dereference a symbolic link?

When you specify a symbolic link as an argument to ls, the –H option causes ls to dereference the symbolic link; it displays information about the file the link points to (the target file; memoA in the example).


1 Answers

cp -rL /source /destination

r = recursive L = follow and expand symlinks

like image 143
Daniele Testa Avatar answered Sep 19 '22 00:09

Daniele Testa