首頁·終端機命令

cp

檔案與目錄

用法

cp -r [source] [dest]

複製檔案或整個目錄樹;目標是否已存在,決定來源是變成那個名字還是被放進它裡面。

常用選項

選項含義
-rCopy directories and their contents
-aArchive: -r plus permissions, times and symlinks kept
-pKeep mode, owner and timestamps
-iAsk before overwriting
-nNever overwrite a file that already exists
-vName each file as it is copied
-uGNU: copy only when the source is newer

範例

cp -a site/ backup/

Tree copied with permissions and times intact.

cp config.yml config.yml.bak

Quick backup beside the original.

cp -r src/. dest/

Copies the contents of src into an existing dest.

搬移、刪除和尋找。有些命令不會再問一次,所以順手加 -i 是在救自己。

怎麼看

  • 方括號 [ ] 表示這部分可以不寫。
  • 省略號 … 表示可以寫多個。
  • 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。

常見問題

Q. cp 是做什麼的?

複製檔案或整個目錄樹;目標是否已存在,決定來源是變成那個名字還是被放進它裡面。

Q. 怎麼寫?

cp -r [source] [dest] —— 方括號表示可以省略的部分。

Q. 值得記的選項有幾個?

這裡列了 7 個,完整列表在 man cp。這條命令屬於檔案與目錄。

相關命令

man 手冊: man cp