| この記事を読むと・・・ |
|---|
| cpコマンドの基本的な使い方が理解できる |
目次
【cpコマンド】どんな時に使う?
以下のようなことがしたい場合にcpコマンドが役立ちます。
- ファイルをコピーしたい
- ディレクトリを再帰的にコピーしたい
【cpコマンド】基本オプション
書式
cp [オプション] コピー元 コピー先
※[ ]は省略可能です
基本的なオプションは以下の表の通りです。
| 短縮オプション | オプション | 説明 |
|---|---|---|
| – | オプションなし | ファイルをコピーする |
| -a | –archive | 属性とディレクトリの構成を可能な限り全てを保持してコピーする ※-dR –preserve=allと同じ |
| -p | – | パーミッション、所有者、タイムスタンプを保持してコピーする ※–preserve=mode,ownership,timestamps |
| – | –preserve[=属性リスト] | 指定した属性を保持してコピーする 「mode(パーミッション)、ownership(所有者)、timestamps(タイムスタンプ)、context(コンテキスト情報)、links(ディレクトリ内のハードリンク)、xattr(ファイルシステムの拡張属性)、all(可能な限り全ての属性)」 ※デフォルト: mode,ownership,timestamps |
| – | –attributes-only | ファイルのデータはコピーせず、ファイルの属性のみコピーする |
| -b | –backup | 既にファイルが存在する場合、バックアップを作成する ※「~」が接尾辞として設定される |
| -S 接尾辞 | –suffix=接尾辞 | 既にファイルが存在する場合、指定した接尾辞でバックアップを作成する ※接尾辞未指定時は「~」、または環境変数「SIMPLE_BACKUP_SUFFIX」に従い接尾辞が設定される |
| -d | – | シンボリックリンク自体をコピーする |
| -s | –symbolic-link | シンボリックリンクを作成する |
| -l | –link | ハードリンクを作成する |
| -R -r | –recursive | 再帰的にコピーする |
| -v | –verbose | 詳細を表示する |
| -i | –interactive | 上書き前に確認する |
| -n | –no-clobber | 上書きしない |
| -f | –force | 強制的に上書きする |
| -u | –update | コピー元のファイルがコピー先ファイルより新しいか、新規ファイルの場合だけコピーする |
| – | –parents | フォルダ構造ごとコピーする |
【cpコマンド】オプション使用例
オプションなし
(ファイルをコピーする)
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 13:08 test01.txt
# cp test01.txt test02.txt
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 13:08 test01.txt
-rw-r--r-- 1 root root 5 5月 25 13:10 test02.txt「-a」「–archive」オプション
(属性とディレクトリの構成を可能な限り全てを保持してコピーする)
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 13:08 test01.txt
# cp -a test01.txt test02.txt
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 13:08 test01.txt
-rw-r--r-- 1 test test 5 5月 25 13:08 test02.txt「-p」オプション
(パーミッション、所有者、タイムスタンプを保持してコピーする)
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 14:34 test01.txt
# cp -p test01.txt test02.txt
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 14:34 test01.txt
-rw-r--r-- 1 test test 5 5月 25 14:34 test02.txt「–preserve」オプション
(指定した属性を保持してコピーする)
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 14:34 test01.txt
# cp --preserve test01.txt test02.txt
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 14:34 test01.txt
-rw-r--r-- 1 test test 5 5月 25 14:34 test02.txt「–attributes-only」オプション
(ファイルのデータはコピーせず、ファイルの属性のみコピーする)
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 13:08 test01.txt
# cp --attributes-only test01.txt test02.txt
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 13:08 test01.txt
-rw-r--r-- 1 root root 0 5月 25 13:14 test02.txt「–b」「–backup=方法」オプション
(既にファイルが存在する場合、バックアップを作成する)
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 13:08 test01.txt
-rw-r--r-- 1 root root 5 5月 25 13:17 test02.txt
# cp -b test01.txt test02.txt
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 13:08 test01.txt
-rw-r--r-- 1 root root 5 5月 25 13:18 test02.txt
-rw-r--r-- 1 root root 5 5月 25 13:17 test02.txt~「-S 接尾辞」「–suffix=接尾辞」オプション
(既にファイルが存在する場合、指定した接尾辞でバックアップを作成する)
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 13:08 test01.txt
-rw-r--r-- 1 root root 5 5月 25 13:27 test02.txt
# cp -S _BK test01.txt test02.txt
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 13:08 test01.txt
-rw-r--r-- 1 root root 5 5月 25 13:28 test02.txt
-rw-r--r-- 1 root root 5 5月 25 13:27 test02.txt_BK「-d」オプション
(シンボリックリンク自体をコピーする)
# ls -l test*
-rw-r--r-- 1 test test 0 5月 25 13:33 test01.txt
lrwxrwxrwx 1 root root 10 5月 25 13:34 test02.txt -> test01.txt
# cp -d test02.txt test03.txt
# ls -l test*
-rw-r--r-- 1 test test 0 5月 25 13:33 test01.txt
lrwxrwxrwx 1 root root 10 5月 25 13:34 test02.txt -> test01.txt
lrwxrwxrwx 1 root root 10 5月 25 13:37 test03.txt -> test01.txt「-s」「–symbolic-link」オプション
(シンボリックリンクを作成する)
# ls -l test*
-rw-r--r-- 1 test test 0 5月 25 13:33 test01.txt
# cp -s test01.txt test02.txt
# ls -l test*
-rw-r--r-- 1 test test 0 5月 25 13:33 test01.txt
lrwxrwxrwx 1 root root 10 5月 25 13:40 test02.txt -> test01.txt「-l」「–link」オプション
(ハードリンクを作成する)
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 13:44 test01.txt
# cp -l test01.txt test02.txt
# ls -l test*
-rw-r--r-- 2 test test 5 5月 25 13:44 test01.txt
-rw-r--r-- 2 test test 5 5月 25 13:44 test02.txt「-r」「-R」「–recursive」オプション
(再帰的にコピーする)
# tree testdir1/
testdir1/
|-- test01.txt
0 directories, 1 file
# cp -r testdir1 testdir2
# tree testdir2
testdir2
|-- test01.txt
0 directories, 1 file「-v」「–verbose」オプション
(詳細を表示する)
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 13:44 test01.txt
# cp -v test01.txt test02.txt
'test01.txt' -> 'test02.txt'
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 13:44 test01.txt
-rw-r--r-- 1 root root 5 5月 25 14:25 test02.txt「-i」「–interactive」オプション
(上書き前に確認する)
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 14:34 test01.txt
-rw-r--r-- 1 root root 5 5月 25 15:10 test02.txt
# cp -i test01.txt test02.txt
cp: 'test02.txt' を上書きしますか? y
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 14:34 test01.txt
-rw-r--r-- 1 root root 5 5月 25 15:10 test02.txt「-n」「–no-clobber」オプション
(上書きしない)
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 13:44 test01.txt
-rw-r--r-- 1 root root 5 5月 25 14:25 test02.txt
# cp -n test01.txt test02.txt
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 13:44 test01.txt
-rw-r--r-- 1 root root 5 5月 25 14:25 test02.txt「-f」「–force」オプション
(強制的に上書きする)
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 13:44 test01.txt
-rw-r--r-- 1 root root 5 5月 25 14:25 test02.txt
# cp -f test01.txt test02.txt
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 13:44 test01.txt
-rw-r--r-- 1 root root 5 5月 25 14:30 test02.txt「-u」「–update」オプション
(コピー元のファイルがコピー先ファイルより新しいか、新規ファイルの場合だけコピーする)
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 13:44 test01.txt
-rw-r--r-- 1 root root 5 5月 25 14:30 test02.txt
# cp -u test01.txt test02.txt
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 13:44 test01.txt
-rw-r--r-- 1 root root 5 5月 25 14:30 test02.txt
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 14:34 test01.txt
-rw-r--r-- 1 root root 5 5月 25 14:30 test02.txt
# cp -u test01.txt test02.txt
# ls -l test*
-rw-r--r-- 1 test test 5 5月 25 14:34 test01.txt
-rw-r--r-- 1 root root 5 5月 25 14:35 test02.txt「–parents」オプション
(フォルダ構造ごとコピーする)
# ls -l testdir1/test01.txt
-rw-r--r-- 1 test test 0 5月 25 15:11 testdir1/test01.txt
# ls -l testdir2
合計 0
# cp --parents testdir1/test01.txt testdir2/
# tree testdir2
testdir2
|-- testdir1
|-- test01.txt
1 directory, 1 file
