Skip to content
Tiantan
Articles

cp-rとcp-Rの違い(コピーコマンド)

On 2月 18, 2021 by admin

cp -rは、ファイルを再帰的にコピーすることを目的としています。およびcp -Rは、ディレクトリを再帰的にコピーします。しかし、確認したところ、どちらもファイルとディレクトリの両方を同じようにコピーしているように見えます。実際の違いは何ですか?

コメント

  • Linuxマニュアルでこれらのオプションについて言及している場合はすべて、'は同義語であると述べています。 ('他のunixバリアントにはありません。)

回答

-Rはposixで明確に定義されていますが、-rは移植性がありません!

Linuxでは、 cp、-r、および-RのGNUとBusyBoxの実装は同等です。

反対側では、cpの POSIX マニュアルページで読むことができます。 、-rの動作は実装定義です。

 * If neither the -R nor -r options were specified, cp shall take actions based on the type and contents of the file referenced by the symbolic link, and not by the symbolic link itself. * If the -R option was specified: * If none of the options -H, -L, nor -P were specified, it is unspecified which of -H, -L, or -P will be used as a default. * If the -H option was specified, cp shall take actions based on the type and contents of the file referenced by any symbolic link specified as a source_file operand. * If the -L option was specified, cp shall take actions based on the type and contents of the file referenced by any symbolic link specified as a source_file operand or any symbolic links encoun- tered during traversal of a file hierarchy. * If the -P option was specified, cp shall copy any symbolic link specified as a source_file operand and any symbolic links encoun- tered during traversal of a file hierarchy, and shall not follow any symbolic links. * If the -r option was specified, the behavior is implementation- defined. 

コメント

  • 違いの1つは、たとえばOSXでは、-rがシンボリックリンクで示される実際のファイルをコピーするのに対し、-Rはコピーすることです。ほとんどの場合意図されているシンボリックリンクをコピーします。

回答

小文字-rは、4.1BSDで導入された古いオプションで、すべての非ディレクトリをファイルとしてコピーするだけでした。つまり、デバイスまたはFIFOが検出された場合、デバイスまたはFIFOを開き、内容を読み取り、その内容を含むファイルを宛先に作成します。

大文字-Rは標準化されたオプション(4.4BSDでBSDに導入されましたが、以前のバージョンでは-rの同義語として使用されていました)であり、デバイス、FIFO、またはその他の特別なファイルに遭遇すると、宛先で同等の特殊ファイルを作成します。

多くの実装は引き続きこの区別を維持しますが、一部(Linuxで一般的なGNUバージョンを含む)は-Rのみを提供します。セマンティクス、-rを同義語とします。

回答

違いは1つは小文字の「R」を使用し、もう1つは大文字の「R」を使用します。それを超えて、違いはありません。 --recursiveロングオプションを使用する場合も同じです。

コメント

  • マニュアルページから:- R、-r、-recursive-ディレクトリを再帰的にコピーする
  • @DaveJennings:1つのプラットフォームで同等であるからといって、'すべてにあるとは限りません。 cpの一部の実装では、実際には違いがあります。 ランダム832 'の回答を参照してください。 @Ignacio cpが最新のGNU実装である場合は、"という修飾子をこの回答に含める必要があります"またはそのようなもの。
  • @Caleb:質問には" linux "のタグが付けられています。他に何がありますか?
  • -Rと-rは、私が使用しているいくつかのシステム(OS X、FreeBSD、BusyBox coreutils、現時点ではどちらかわからない)で異なる動作をします。私はずっと前に、常に-Rのみを使用することが賢明であると判断しました。実際、上記のシステムのマニュアルページの一部でも推奨されています。

回答

OSXおよび古いバージョンのFreeBSDでは-rは-R -L --copy-contentsはcoreutilsで、またはシンボリックリンクをたどって特殊ファイルとFIFOの内容を読み取ります。

mkdir a;touch b;ln -s $PWD/b a;cp -r a cは、シンボリックリンクをのターゲットファイルに置き換えます。 OS X、mkdir a;mkfifo a/b;cp -r a cはFIFOの読み取りをブロックされ、mkdir a;ln -s /dev/zero a;cp -r a bはb/zeroをゼロで埋め始めます。

OSXおよび古いバージョンのFreeBSDのcpのマニュアルページから:

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

FreeBSDの新しいバージョンでは、-rは-RLと同等です:

Historic versions of the cp utility had a -r option. This implementation supports that option, however, its behavior is different from historical FreeBSD behavior. Use of this option is strongly discouraged as the behavior is implementation-dependent. In FreeBSD, -r is a synonym for -RL and works the same unless modified by other flags. Historical imple- mentations of -r differ as they copy special files as normal files while recreating a hierarchy. 

http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html :

--copy-contents

再帰的にコピーする場合は、特別なファイル(FIFOやデバイスファイルなど)の内容を通常のファイルであるかのようにコピーします。これは、各ソースファイルのデータを読み取って宛先に書き込もうとすることを意味します。通常、FIFOや/devディレクトリにある特殊ファイルに望ましくない影響を与えるため、このオプションを使用するのは通常間違いです。ほとんどの場合、cp -R --copy-contentsはFIFOや/dev/consoleなどの特殊ファイルから読み取ろうとすると無期限にハングし、次の場合は宛先ディスクがいっぱいになります。 /dev/zeroをコピーするために使用します。 このオプションは、再帰的にコピーしない限り効果がなく、シンボリックリンクのコピーには影響しません。

回答

-rではないことがわかった違いの1つ -Rが隠しディレクトリをコピーする間、隠しディレクトリをコピーします。

ターゲットディレクトリの.gitディレクトリをテストし、上記の結論に達しました。 現在centOSを使用しています。

間違っている可能性がありますが、議論の余地があります。

コメント

  • ありません 'これはCentOS5では表示されません。-rと-Rはどちらも隠しディレクトリをコピーします

Written by admin

コメントを残す コメントをキャンセル

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

最近の投稿

  • 共振ダイポールを備えたバランの使用
  • “できない”と“できない? [重複]
  • JFKでは国内線のエアサイドターミナル間を移動できますか?
  • “深く感謝します”または“深く感謝します”
  • '抽象的なアイデア'とはどういう意味ですか? [クローズ]

アーカイブ

  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • Deutsch
  • Nederlands
  • Svenska
  • Norsk
  • Dansk
  • Español
  • Français
  • Português
  • Italiano
  • Română
  • Polski
  • Čeština
  • Magyar
  • Suomi
  • 日本語
  • 한국어

Copyright Tiantan 2021 | Theme by Theme in Progress | Proudly powered by WordPress

Back to top