I’m trying to understand what cp --preserve=links does when used by itself, from my tests it seems that it copies a normal file normally and dereferences symlinks, but it seems like it just has the same effect as cp -L when used on a single file.
Is that true or is there something I’m missing?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The --preserve=links option does not refer to symbolic links, but to hard links. It asks cp to preserve any existing hard link between two or more files that are being copied.
$ date > file1 $ ln file1 file2 $ ls -1i file1 file2 6034008 file1 6034008 file2
You can see that the two original files are hard-linked and their inode number is 6034008.
$ mkdir dir1 $ cp file1 file2 dir1 $ ls -1i dir1 total 8 6035093 file1 6038175 file2
You can see now that without --preserve=links their copies have two different inode numbers: there is no longer a hard link between the two.
$ mkdir dir2 $ cp --preserve=links file1 file2 dir2 $ ls -1i dir2 total 8 6089617 file1 6089617 file2
You can see now that with --preserve=links, the two copies are still hard-linked, but their inode number is 6089617, which is not the same as the inode number of the original files (contrary to what cp --link would have done).
Method 2
I’m getting some conflicting information after testing and reading the man page. I just ran some tests and found the following.
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="65170a0a11250009524854">[email protected]</a> dest]# ls -l total 0 lrwxrwxrwx. 1 root root 16 Aug 18 16:51 test1.txt -> ../src/test1.txt
The following commands all deference the link
cp test1.txt test2.txt cp -L test1.txt test2.txt cp --preserve=link test1.txt test2.txt
The following command copies the symlink itself
cp -P test1.txt test2.txt
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0