‹ BackHN Continuity

Thread

cp: -r or -R?

125 points · 107 comments · zdw

  1. bariumbitmap · · focus · HN ↗
    This blog post is odd because it keeps on hinting about a difference between `-r' and `-R' and links to the source code but never actually says what it is. I'll quote the OpenBSD manual that the post mentions but does not link to for some reason:

    > Historic versions of the cp utility had an -r option. This implementation supports that option; however, its use is strongly discouraged, as it does not correctly copy special files, symbolic links or FIFOs.

    <a href="https:&#x2F;&#x2F;man.openbsd.org&#x2F;cp" rel="nofollow">https:&#x2F;&#x2F;man.openbsd.org&#x2F;cp

    1. rakel_rakel · · focus · HN ↗
      You can see it in the code, lstat vs stat and copy_special, copy_fifo vs copy_file. They&#x27;re all in utils.c.

      from the lstat man page:

           The lstat() function is identical to stat() except when the named file is
           a symbolic link, in which case lstat() returns information about the link
           itself, not the file the link references.
      
      symbolic link example:

          $ tree test1
          test1
          |-- file.txt
          `-- link.txt -&gt; ..&#x2F;link.txt
      
          0 directory, 2 files
      
          $ cp -r test1 test2
          $ tree test2
          test2
          |-- file.txt
          `-- link.txt
      
          0 directory, 2 files
      
          $ cp -R test1 test3
          $ tree test3
          test3
          |-- file.txt
          `-- link.txt -&gt; ..&#x2F;link.txt
      
          0 directory, 2 files
      
      For posterity, -R with added -L

          $ cp -RL test1 test4
          $ tree test4&#x2F;
          test4&#x2F;
          |-- file.txt
          `-- link.txt
      
          0 directory, 2 files
Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.