Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert symlink to regular file?

Tags:

linux

symlink

What is the most direct way to convert a symlink into a regular file (i.e. a copy of the symlink target)?

Suppose filename is a symlink to target. The obvious procedure to turn it into a copy is:

cp filename filename-backup rm filename mv filename-backup filename 

Is there a more direct way (i.e. a single command)?

like image 565
nibot Avatar asked Dec 04 '11 17:12

nibot


People also ask

Does removing symlink remove file?

When you remove a symlink, the file it points to is not affected. Use the ls -l command to check whether a given file is a symbolic link, and to find the file or directory that symbolic link point to. The first character “l”, indicates that the file is a symlink. The “->” symbol shows the file the symlink points to.

How do I bypass symlinks?

Overwriting Symlinks If you try to create a symbolic link that already exists , the ln command will print an error message. To overwrite the destination path of the symlink, use the -f ( --force ) option.

Can symlinks 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.

Can you edit symlinks?

No. The symlink system call will return EEXIST if newpath already exists. You can only link from a new node in the filesystem.


2 Answers

There is no single command to convert a symlink to a regular file. The most direct way is to use readlink to find the file a symlink points to, and then copy that file over the symlink:

cp --remove-destination `readlink bar.pdf` bar.pdf 

Of course, if bar.pdf is, in fact, a regular file to begin with, then this will clobber the file. Some sanity checking would therefore be advisable.

like image 181
nibot Avatar answered Sep 21 '22 13:09

nibot


for f in $(find -type l);do cp --remove-destination $(readlink $f) $f;done; 
  • Check symlinks in the current directory and subdirectories find -type l
  • Get the linked file path readlink $f
  • Remove symlink and copy the file cp --remove-destination $(readlink $f) $f
like image 35
Yadhu Avatar answered Sep 19 '22 13:09

Yadhu