The cp command's infopage offers on the option --preserve= the following:links        Preserve in the destination files any links between        corresponding source files.  Note that with -L' or-H',        this option can convert symbolic links to hard links.followed by an example I don't get [now]; anyhow:Question: How to turn soft- into hardlinks with cp? And is there a way back too [converting hard- into softlinks]?Secondary Issue: Where does can in the quote above come into play? I understand the purpose of -L and -H, I'm able to copy fully functional softlinks etc., but so far I didn't manage to turn soft- into hardlinks.
convert soft- to hardlinks with cp
linux;command line;cp;options;coreutils
The example in the info page shows you how though the example is a bit hard to follow:$ mkdir c; : > a; ln -s a b; cp -aH a b c; ls -i1 c74161745 a74161745 bLet's break that down into its component commands:mkdir c; : creates the directory c/: > a; : just a quick way of creating an empty file. It is equivalent to echo  > a. : is a bash built in which does nothing, see help :.ln -s a b : create a softlink to a called b. At this point, these are the contents of the current directory:$ ls -l | cc2ter total 4-rw-r--r-- 1 terdon terdon    0 Oct  9 02:50 alrwxrwxrwx 1 terdon terdon    1 Oct  9 02:50 b -> adrwxr-xr-x 2 terdon terdon 4096 Oct  9 02:50 cNote that b is a symbolic link (soft link) it does not point to the same inode as a:$ ls -i1c a b16647344 a16647362 bcp -aH a b c; : copy files a and b into directory c. This is where the conversion is happening, the options passed to cp are:-a, --archive      same as -dR --preserve=all-d    same as --no-dereference --preserve=links-H    follow command-line symbolic links in SOURCEThe -H is necessary because (from info cp):When copying from a symbolic link, `cp' normally follows the link  only when not copying recursively.Since -a activates recursive copying (-R), -H is needed to follow symbolic links. -H means that links are followed despite recursion and will result in hard links being made in the target directory. These are the contents of c/ after the last step (the first column is the inode number):$ ls -li c total 017044704 -rw-r--r-- 2 terdon terdon 0 Oct  9 02:50 a17044704 -rw-r--r-- 2 terdon terdon 0 Oct  9 02:50 bNow as to how exactly it works, as far as I can figure out from playing around with it, cp --preserve=links combined with -L or -H will convert symbolic links to hard links if both the link and the target are being copied to the same directory.In fact, as the OP found out, at least on Debian systems, cp --preserve=links is sufficient to convert symlinks to hard links if the target directory is the same.