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)?
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.
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.
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.
No. The symlink system call will return EEXIST if newpath already exists. You can only link from a new node in the filesystem.
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.
for f in $(find -type l);do cp --remove-destination $(readlink $f) $f;done;
find -type l
readlink $f
cp --remove-destination $(readlink $f) $f
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With