よく忘れるのでメモ。
cpコマンドでディレクトリをコピーするとき、ディレクトリの中身だけをコピーしたい。
コピー先にディレクトリがない場合:そのままコピーされる
$ tree /tmp
/tmp
└── src
└── fileA.txt
1 directory, 1 file
$ cp -a /tmp/src /tmp/dest
$ tree /tmp
/tmp
├── dest
│ └── fileA.txt
└── src
└── fileA.txt
2 directories, 2 files
コピー先にディレクトリがある場合:ネストされてしまう
$ tree /tmp
/tmp
├── dest
└── src
└── fileA.txt
2 directories, 1 file
$ cp -a /tmp/src /tmp/dest
$ tree /tmp
/tmp
├── dest
│ └── src
│ └── fileA.txt
└── src
└── fileA.txt
3 directories, 2 files
コピー先にディレクトリがあるが、コピー元の指定で末尾に「/.」をつけた場合:コピー元ディレクトリの中身だけがコピーされる
$ tree /tmp
/tmp
├── dest
└── src
└── fileA.txt
2 directories, 1 file
$ cp -a /tmp/src/. /tmp/dest
$ tree /tmp
/tmp
├── dest
│ └── fileA.txt
└── src
└── fileA.txt
2 directories, 2 files
コピー先の指定で末尾に「/.」をつけた場合、コピー先ディレクトリがないとエラーになる
$ tree /tmp
/tmp
└── src
└── fileA.txt
1 directory, 1 file
$ cp -a /tmp/src /tmp/dest/.
cp: cannot create directory '/tmp/dest/.': No such file or directory
コピー先の指定で末尾に「/.」をつけた場合、コピー先ディレクトリがあるとネストしてコピーされる
$ tree /tmp
/tmp
├── dest
└── src
└── fileA.txt
2 directories, 1 file
$ cp -a /tmp/src /tmp/dest/.
$ tree /tmp
/tmp
├── dest
│ └── src
│ └── fileA.txt
└── src
└── fileA.txt
3 directories, 2 files
0 件のコメント:
コメントを投稿