Loose-Info.com
Last Update 2014/03/11
TOP - Unix系OS - FreeBSD - cp(1)

ファイルやディレクトリをコピーします。

cp オプション1 ファイルパス1 ファイルパス2 cp オプション1 ファイルパス1 ... ディレクトリ
オプション1(任意) -f ファイルパス2のファイルが既に存在し、開く事ができない場合、削除してから再実行する -i ファイルパス2のファイルが既に存在する場合に、対話形式で確認する -L ファイルパス1のファイルのシンボリックリンクを辿ってコピーする -n ファイルパス2のファイルが既に存在する場合に、上書きしない -P -Rオプション指定の場合、ファイルパス1のファイルのシンボリックリンクを辿らずコピーする -p ファイルパス1のファイルの属性を可能な限り保持したままコピーする -R ファイルパス1のディレクトリを再帰的にコピーする -v コピー実行の表示を行う ファイルパス1(必須) コピー元ファイル(ディレクトリ) ファイルパス2(必須) コピー先ファイル(ディレクトリ) ディレクトリ(必須) コピー先ディレクトリ

※ オプションなどは個人的に重要と考えられるものを記述しており、記載の無いものは、manページや関連書籍などを参照願います。
※ 実行例の記述は、不要と考えられる部分の削除などの修正を行ったものを掲載しています。
※ 実行例の実行環境はFreeBSD 9.1です。

(例) オプション無し コピー先ファイル名指定無し
$ cp test1/test.txt test2/

コピー元ファイル
$ ls -l test1 -rw-r--r-- 1 ****** ****** 9 MMM d hh:mm test.txt

実行後
$ ls -l test2 -rw-r--r-- 1 ****** ****** 9 MMM d hh:mm test.txt

(例) オプション無し コピー先ファイル名指定有り
$ cp test1/test.txt test2/test_copy.txt

コピー元ファイル
$ ls -l test1 -rw-r--r-- 1 ****** ****** 9 MMM d hh:mm test.txt

実行後
$ ls -l test2 -rw-r--r-- 1 ****** ****** 9 MMM d hh:mm test_copy.txt

(例) -f オプション
$ cp -f test1/test.txt test2/test.txt

実行前
$ ls -l test1 -rw-r--r-- 1 ****** ****** 9 MMM d hh:mm test.txt $ ls -l test2 -r--r--r-- 1 ****** ****** 18 MMM d hh:mm test.txt ← 書き込み不可

実行後
$ ls -l test2 -rw-r--r-- 1 ****** ****** 9 MMM d hh:mm test.txt ← コピー元ファイルを上書き

(例) -i オプション
$ cp -i test1/test.txt test2/test.txt
実行前
$ ls -l test1 -rw-r--r-- 1 ****** ****** 9 MMM d hh:mm test.txt $ ls -l test2 -rw-r--r-- 1 ****** ****** 18 Aug 27 16:27 test.txt

確認メッセージ
overwrite test2/test.txt? (y/n [n])

実行後(確認メッセージで「y」を入力)
overwrite test2/test.txt? (y/n [n]) y ← 確認メッセージに対し「y」を入力 $ ls -l test2 -rw-r--r-- 1 ****** ****** 9 MMM d hh:mm test.txt ← コピー元ファイルを上書き

実行後(確認メッセージで[enter]のみ)
overwrite test2/test.txt? (y/n [n]) ← 確認メッセージで[enter]のみ入力 not overwritten $ ls -l test2 -rw-r--r-- 1 ****** ****** 18 MMM d hh:mm test.txt ← 元ファイルのまま

(例) -L オプション
$ cp -L test1/test_symlink test1/test_symlink_copy

実行前
$ ls -l test1 -rw-r--r-- 1 ****** ****** 5 MMM d hh:mm test.txt lrwxr-xr-x 1 ****** ****** 8 MMM d hh:mm test_symlink -> test.txt

実行後
$ ls -l test1 -rw-r--r-- 1 ****** ****** 5 MMM d hh:mm test.txt lrwxr-xr-x 1 ****** ****** 8 MMM d hh:mm test_symlink -> test.txt -rw-r--r-- 1 ****** ****** 5 MMM d hh:mm test_symlink_copy

(例) -n オプション
$ cp -n test1/test.txt test2/test.txt

実行前
$ ls -l test1 -rw-r--r-- 1 ****** ****** 5 Aug 24 12:53 test.txt $ ls -l test2 -rw-r--r-- 1 ****** ****** 15 Aug 24 12:56 test.txt

実行後
$ ls -l test2 -rw-r--r-- 1 ****** ****** 15 Aug 24 12:56 test.txt ← 上書きされない

(例) -P オプション
$ cp -PR test1/test_symlink test1/test_symlink_copy

実行前
$ ls -l test1 -rw-r--r-- 1 ****** ****** 5 Aug 24 12:53 test.txt lrwxr-xr-x 1 ****** ****** 8 Aug 24 15:13 test_symlink -> test.txt

実行後
$ ls -l test1 -rw-r--r-- 1 ****** ****** 5 Aug 24 12:53 test.txt lrwxr-xr-x 1 ****** ****** 8 Aug 24 15:13 test_symlink -> test.txt lrwxr-xr-x 1 ****** ****** 8 Aug 24 15:30 test_symlink_copy -> test.txt

(例) -p オプション
$ cp -p testusr1.txt testusr1_copy.txt

実行前
$ ls -l -rwxr-xr-x 1 testusr1 testusr1 17 Aug 30 21:34 testusr1.txt

実行後(root権限で実行した場合)
# ls -l -rwxr-xr-x 1 testusr1 testusr1 17 Aug 30 21:34 testusr1.txt -rwxr-xr-x 1 testusr1 testusr1 17 Aug 30 21:34 testusr1_copy.txt

(例) -R オプション
$ cp -R test1/testdir2 test2/

実行前
$ ls -lR test1 testdir/: drwxr-xr-x 2 ****** ****** 512 Aug 30 22:19 testdir2 test1/testdir2: -rw-r--r-- 1 ****** ****** 5 Aug 30 22:19 test.txt

実行後
$ ls -lR test2 drwxr-xr-x 2 ****** ****** 512 Aug 30 22:22 testdir2 test2/testdir2: -rw-r--r-- 1 ****** ****** 5 Aug 30 22:22 test.txt

(例) -v オプション
$ cp -uv testdir/test.txt testdir/test2.txt testdir/test3.txt /mnt/usbmem/

実行前
$ ls -l test1/ -rw-r--r-- 1 ****** ****** 5 Aug 30 22:51 test.txt -rw-r--r-- 1 ****** ****** 5 Aug 30 22:58 test2.txt -rw-r--r-- 1 ****** ****** 16 Aug 30 22:59 test3.txt $ ls -l test2/ -rw-r--r-- 1 ****** ****** 5 Aug 30 23:02 test.txt

実行時表示
test1/test3.txt -> test2/test3.txt test1/test2.txt -> test2/test2.txt test2/test.txt not overwritten test1/test.txt -> test2/test.txt

実行後
$ ls -l test2 -rw-r--r-- 1 ****** ****** 5 Aug 30 23:02 test.txt -rw-r--r-- 1 ****** ****** 5 Aug 30 23:06 test2.txt -rw-r--r-- 1 ****** ****** 16 Aug 30 23:06 test3.txt