Info on cp –preserve=links
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?
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
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).