ファイルまたはディレクトリをコピーするcpコマンドは、最も一般的に使用されるLinuxシステムのコマンドのいずれかです。ファイルをコピーする際に通常の状況下では、シェルは、ターゲット・ファイルがすでに存在する場合、それは関係なく、あなたが-iパラメータを使用するかどうかの、コマンドラインを上書きするかどうかを聞いてきます、エイリアスを設定します。CPは、シェルスクリプトで実行された場合でも、不在-iパラメータを上書きすることを求めていません。このショーはいくつかの違いは、コマンドラインやシェルスクリプトを実行します。
フォーマット:
使用法:
CP [オプション] ... [-T]送信元と送信先
か:CP [オプション]ソース...ディレクトリ...
か:CP [オプション] ... -tディレクトリソース...機能:
ターゲットディレクトリに複数のソースファイルをターゲットファイルにソースファイルをコピー、またはコピーします。コマンド・パラメーター:
-a, --archive 等于-dR --preserve=all
--backup[=CONTROL 为每个已存在的目标文件创建备份
-b 类似--backup 但不接受参数
--copy-contents 在递归处理是复制特殊文件内容
-d 等于--no-dereference --preserve=links
-f, --force 如果目标文件无法打开则将其移除并重试(当 -n 选项存在时则不需再选此项)
-i, --interactive 覆盖前询问(使前面的 -n 选项失效)
-H 跟随源文件中的命令行符号链接
-l, --link 链接文件而不复制
-L, --dereference 总是跟随符号链接
-n, --no-clobber 不要覆盖已存在的文件(使前面的 -i 选项失效)
-P, --no-dereference 不跟随源文件中的符号链接
-p 等于--preserve=模式,所有权,时间戳
--preserve[=属性列表 保持指定的属性(默认:模式,所有权,时间戳),如果可能保持附加属性:环境、链接、xattr 等。
-R, -r, --recursive 复制目录及目录内的所有项目
- コマンド例:
例:ターゲットディレクトリに単一のファイルをコピーし、ファイルがターゲットファイルに存在しない
コマンドを実行します。
cp log.log test5
出力:
[root@localhost test]# cp log.log test5
[root@localhost test]# ll
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxr-xr-x 2 root root 4096 10-28 14:53 test5
[root@localhost test]# cd test5
[root@localhost test5]# ll
-rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log
-rw-r--r-- 1 root root 0 10-28 14:53 log.log
説明:
-aパラメータが存在しない場合には、時間の2つのファイルが同じではありません。とき-aパラメータ、時間とともに二つの文書が一致しています。
例2:対象のファイルが存在する場合、上書きするかどうかを尋ねられます
コマンド:
cp log.log test5
出力:
[root@localhost test]# cp log.log test5
cp:是否覆盖“test5/log.log”? n
[root@localhost test]# cp -a log.log test5
cp:是否覆盖“test5/log.log”? y
[root@localhost test]# cd test5/
[root@localhost test5]# ll
-rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
説明:
ターゲットファイルが存在し、上書きするかどうかを尋ねられます。CPはCP -i別名であるためです。対象のファイルが存在する場合、でも-fフラグを追加する場合、また、上書きするかどうかを尋ねられます。
3つの例は:ディレクトリ全体をコピーします
コマンド:
出力:
ターゲット・ディレクトリが存在する場合:
[root@localhost test]# cp -a test3 test5
[root@localhost test]# ll
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxr-xr-x 3 root root 4096 10-28 15:11 test5
[root@localhost test]# cd test5/
[root@localhost test5]# ll
-rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
目标目录不存在是:
[root@localhost test]# cp -a test3 test4
[root@localhost test]# ll
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
drwxr-xr-x 3 root root 4096 10-28 15:11 test5
[root@localhost test]#
注:
ターゲット・ディレクトリが存在するかどうか、結果は同じではないことに注意してください。ターゲットディレクトリが存在する場合、全体のソースディレクトリは、宛先ディレクトリにコピーされます。
4つの例:コピーlog.logのは、リンクファイルlog_link.logを確立します
コマンド:
cp -s log.log log_link.log
出力:
[root@localhost test]# cp -s log.log log_link.log
[root@localhost test]# ll
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -> log.log
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
drwxr-xr-x 3 root root 4096 10-28 15:11 test5
説明:
log_link.log -sパラメータので、あなたは彼らが行く右端ファイルの、ファイルが表示されている「リンク」に表示されます、「ショートカット」の設立によって引き起こされていること!