恥は/dev/nullへ by 初心者

プログラミング素人がのろのろと学んだことをつづっています♪

シンボリックリンクをコピーするには?

シンボリックリンクをコピーするには cp -d を使う

ビルドしたVim関係のファイルを ~/.local/binにコピーしようとして

cp * ~/.local/bin

としたら、シンボリックリンクが通常のファイルになってしまいました。以下は ls -l が出力した内容です。

# 元ファイル
lrwxrwxrwx (中略) eview -> vim
lrwxrwxrwx (中略) evim -> vim
lrwxrwxrwx (中略) ex -> vim
lrwxrwxrwx (中略) gview -> vim
lrwxrwxrwx (中略) gvim -> vim
lrwxrwxrwx (中略) gvimdiff -> vim
-rwxr-xr-x (中略) gvimtutor
lrwxrwxrwx (中略) rgview -> vim
lrwxrwxrwx (中略) rgvim -> vim
lrwxrwxrwx (中略) rview -> vim
lrwxrwxrwx (中略) rvim -> vim
lrwxrwxrwx (中略) view -> vim
-rwxr-xr-x (中略) vim
lrwxrwxrwx (中略) vimdiff -> vim
-rwxr-xr-x (中略) vimtutor
-rwxr-xr-x (中略) xxd

# ~/.local/binにコピーされたファイル
-rwxr-xr-x (中略) eview
-rwxr-xr-x (中略) evim
-rwxr-xr-x (中略) ex
-rwxr-xr-x (中略) gview
-rwxr-xr-x (中略) gvim
-rwxr-xr-x (中略) gvimdiff
-rwxr-xr-x (中略) gvimtutor
-rwxr-xr-x (中略) rgview
-rwxr-xr-x (中略) rgvim
-rwxr-xr-x (中略) rview
-rwxr-xr-x (中略) rvim
-rwxr-xr-x (中略) view
-rwxr-xr-x (中略) vim
-rwxr-xr-x (中略) vimdiff
-rwxr-xr-x (中略) vimtutor
-rwxr-xr-x (中略) xxd


ネット情報とmanページによると、cpコマンドに -d オプションを付ければ良さそうです。

cp -d * ~/.local/bin/

としてみたら、以下のようにシンボリックリンクのままコピーされました。

# cp -d を使って ~/.local/binにコピーされたファイル
lrwxrwxrwx (中略) eview -> vim
lrwxrwxrwx (中略) evim -> vim
lrwxrwxrwx (中略) ex -> vim
lrwxrwxrwx (中略) gview -> vim
lrwxrwxrwx (中略) gvim -> vim
lrwxrwxrwx (中略) gvimdiff -> vim
-rwxr-xr-x (中略) gvimtutor
lrwxrwxrwx (中略) rgview -> vim
lrwxrwxrwx (中略) rgvim -> vim
lrwxrwxrwx (中略) rview -> vim
lrwxrwxrwx (中略) rvim -> vim
lrwxrwxrwx (中略) view -> vim
-rwxr-xr-x (中略) vim
lrwxrwxrwx (中略) vimdiff -> vim
-rwxr-xr-x (中略) vimtutor
-rwxr-xr-x (中略) xxd


mvコマンドの場合

mvコマンドではオプション無しでシンボリックリンクのままファイル移動ができました。


素朴な疑問

cp -P でも同じことができそうです。cp -P を使ってコピーしてみたところ、シンボリックリンクのままコピーできました。

ただ、manページには

SYNOPSIS
       cp [OPTION]... [-T] SOURCE DEST
       cp [OPTION]... SOURCE... DIRECTORY
       cp [OPTION]... -t DIRECTORY SOURCE...


-d    same as --no-dereference --preserve=links

-P, --no-dereference
      never follow symbolic links in SOURCE

と書かれています。これを見ると--preserve=linksの有無が違うようです。

info cp

を見てみると

'-d'
     Copy symbolic links as symbolic links rather than copying the files
     that they point to, and preserve hard links between source files in
     the copies.  Equivalent to '--no-dereference --preserve=links'.

'-P'
'--no-dereference'
     Copy symbolic links as symbolic links rather than copying the files
     that they point to.  This option affects only symbolic links in the
     source; symbolic links in the destination are always followed if
     possible.

'-p'
'--preserve[=ATTRIBUTE_LIST]'
     Preserve the specified attributes of the original files.  If
     specified, the ATTRIBUTE_LIST must be a comma-separated list of one
     or more of the following strings:

     'mode'
          (省略)
     'ownership'
          (省略)
     'timestamps'
          (省略)
     '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.  For example,
               $ mkdir c; : > a; ln -s a b; cp -aH a b c; ls -i1 c
               74161745 a
               74161745 b
          Note the inputs: 'b' is a symlink to regular file 'a', yet the
          files in destination directory, 'c/', are hard-linked.  Since
          '-a' implies '--no-dereference' it would copy the symlink, but
          the later '-H' tells 'cp' to dereference the command line
          arguments where it then sees two files with the same inode
          number.  Then the '--preserve=links' option also implied by
          '-a' will preserve the perceived hard link.

          Here is a similar example that exercises 'cp''s '-L' option:
               $ mkdir b c; (cd b; : > a; ln -s a b); cp -aL b c; ls -i1 c/b
               74163295 a
               74163295 b

とあります。他にシンボリックリンクに関係する記述として以下の文章もありました。

   When copying from a symbolic link, 'cp' normally follows the link
only when not copying recursively or when '--link' ('-l') is used.  This
default can be overridden with the '--archive' ('-a'), '-d',
'--dereference' ('-L'), '--no-dereference' ('-P'), and '-H' options.  If
more than one of these options is specified, the last one silently
overrides the others.

   When copying to a symbolic link, 'cp' follows the link only when it
refers to an existing regular file.  However, when copying to a dangling
symbolic link, 'cp' refuses by default, and fails with a diagnostic,
since the operation is inherently dangerous.  This behavior is contrary
to historical practice and to POSIX.  Set 'POSIXLY_CORRECT' to make 'cp'
attempt to create the target of a dangling destination symlink, in spite
of the possible risk.  Also, when an option like '--backup' or '--link'
acts to rename or remove the destination before copying, 'cp' renames or
removes the symbolic link rather than the file it points to.


参考にしたサイト

シンボリックリンクのコピー | コマンドの達人