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 'の回答を参照してください。 @Ignaciocp
が最新の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
はどちらも隠しディレクトリをコピーします
コメントを残す