cpコマンド

UNIXサーバ構築 OnTheVMware

unixコマンドについて使用例を用いて解説
HOME > 基本操作コマンド >

cpコマンド

cpコマンド


スポンサードリンク


【説明】
ファイルやディレクトリをコピーする際に使用します。
ファイルのみを二つ指定した場合、1つめのファイルを2つめのファイルにコピーします。引数に3つ以上渡してしまうと文法エラーになってしまうので注意が必要です。またデフォルトではディレクトリのコピーは行いませんが、オプションに「-R」や「-r」を指定することでディレクトリを再帰的にコピーすることが可能になります。

【構文】
cp [ オプション] [ ファイル/ディレクトリ ] [ ディレクトリ/ファイル ]

例 test1ファイルをtest2ファイルへコピーします。
[root@localhost test]# ls -l
合計 0
-rw-r--r-- 1 test test 0 6月 12 07:38 test1
[root@localhost test]#
[root@localhost test]# cp test1 test2
[root@localhost test]#
[root@localhost test]# ls -l
合計 0
-rw-r--r-- 1 test test 0 6月 12 07:38 test1
-rw-r--r-- 1 root root 0 6月 12 07:53 test2
lsコマンドの詳細を見る

例 test1ファイルの”所有者・グループ・アクセス権・アクセス時刻”を引き継ぎ、test2ファイルへコピーします。
[root@localhost test]# ls -l
合計 0
-rw-r--r-- 1 test test 0 6月 12 07:38 test1
[root@localhost test]#
[root@localhost test]# cp -p test1 test2
[root@localhost test]#
[root@localhost test]# ls -l
合計 0
-rw-r--r-- 1 test test 0 6月 12 07:38 test1
-rw-r--r-- 1 test test 0 6月 12 07:38 test2

例 TEST1ディレクトリをTEST2ディレクトリへ再帰的にコピーします。その際、FIFOやスペシャルファイル、シンボリックリンクなどはそのままの形式でコピーします。
[root@localhost test]# ls -lR
.:
合計 4
drwxr-xr-x 2 root root 4096 6月 12 07:56 TEST1

./TEST1:
合計 0
-rw-r--r-- 1 root root 0 6月 12 07:56 test1
[root@localhost test]#
[root@localhost test]# cp -R TEST1 TEST2
[root@localhost test]#
[root@localhost test]# ls -lR
.:
合計 8
drwxr-xr-x 2 root root 4096 6月 12 07:56 TEST1
drwxr-xr-x 2 root root 4096 6月 12 07:57 TEST2

./TEST1:
合計 0
-rw-r--r-- 1 root root 0 6月 12 07:56 test1

./TEST2:
合計 0
-rw-r--r-- 1 root root 0 6月 12 07:57 test1

スポンサードリンク
 HOME / 免責事項 / サイトマップ /  問い合わせ
Copyright (C) 2008  UNIXサーバ構築 OnTheVMware  All rights reserved





スポンサードリンク