How to convert symlink to regular file?

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

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.


Just a rehash of other's answers, but adding the "sanity check" to ensure the link passed in is actually a symbolic link:

removelink() {
  [ -L "$1" ] && cp --remove-destination "$(readlink "$1")" "$1"
}

This is saying that if the file is a symbolic link, then run the copy command.

Tags:

Linux

Symlink