6

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?

8

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).

1

I'm getting some conflicting information after testing and reading the man page. I just ran some tests and found the following.

[root@el7-1 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 
  • 1
    Accorsing to other answers --preserve=links and --no-dereference used together should be used to copy symlinks, but after trying only --no-dereference and --preserve=links and --no-dereference together and running stat on the two links it seems the results are the same. – Matt Aug 18 '17 at 15:10
  • I also noticed this. It seems that the man page and other sources (random google results) offer no coherent information. – Michael Anckaert Aug 18 '17 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.