Home > OS >  How break symlinks but preserve directory contents on Linux
How break symlinks but preserve directory contents on Linux

Time:10-27

I have a directory structure like what is shown below

foo/
   bar/
     a.txt
     b.py
     c.py

and if you do ls -l you would see something like

test@test:/home/foo$ ls -l
lrwxrwxrwx    1        48 Oct 21 12:14 bar -> /lib/python2.7/site-packages/linked_dir/

so the contents of bar/ and linked_dir/ are the same because of the link.

I want to break the link but retain the bar/ directory and its contents. What I have seen online to break a link is rm <link> but that entirely removes the bar/ directory. Is there any way to accomplish what I want?

CodePudding user response:

First copy whatever the symlink points to. By using -L, cp will dereference the symlink:

cp -rL bar new-bar

Delete the symlink:

rm bar

Rename your new copy to the original name:

mv new-bar bar

  • Related