LoginSignup
2
3

More than 3 years have passed since last update.

cp(copy)コマンドで複数ファイルを一括でコピーする方法

Posted at

cpコマンドで引数を複数とっているパターンがあったので処理内容の確認。

macはcp、windowsはcopy

$cp A B C D E ./Fが何をしているかを理解する。

引数の数による処理の違い

(1) 引数一つの場合
ファイルをファイル名を指定してコピー

cp [元のファイル名] [コピー後のファイル名]

cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file target_file

(2)引数が複数の場合
複数のファイルをディレクトリに移動する。
一番末尾がディレクトリのパスになる。

cp [コピーするファイル名1] [コピーするファイル名2]... [ディレクトリ名]

cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file ... target_directory



▼エラー事例

ディレクトリが存在しない場合はコピーエラーとなる。

エラー
cp: ./cat.jpg and cat.jpg are identical (not copied).


実例

以下の処理は、ファイルA,B,C,D,EをディレクトリFにコピーしている操作となる。

$cp A B C D E ./F

▼実際の処理例

//現在のディレクトリ
$ ls
cat.jpg     cat2.png    cat3.png    test

//testディレクトリに3つの画像をコピー
$ cp cat.png cat2.png cat3.png ./test

//testの中身を確認
$ ls test
cat.jpg     cat2.png    cat3.png
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3