iT邦幫忙

0

linux語法應用的問題

mv

請教大家
要如何把在路徑:/home/anderson/confluence/attachments路徑下的所有檔案及資料夾(包含子資料夾)搬移到/var/attlasian/application-data/confluence/attachments/ ,如果遇到重覆的檔案或資料夾則直接覆蓋之
謝謝

fillano iT邦超人 1 級 ‧ 2017-01-27 09:11:27 檢舉
mv -f source target
ektrontek iT邦研究生 1 級 ‧ 2017-01-30 21:38:45 檢舉
謝謝
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
bizpro
iT邦大師 1 級 ‧ 2017-01-30 20:32:19
最佳解答

我習慣用rsync, (而mv不能做到.)
如果要移動整個目錄, 我通常用rsync後再用rm移除來源目錄
$ rsync -avH --delete {source}/ {target}
$ rm {source} -rf
請特別注意--delete將刪除對象目錄中多餘檔案及子目錄, 建議加上-dry-run先測試, 看看rsync做了什麼, 以免誤刪.
$ rsync -avH --delete --dry-run {source} {target}
請注意{source}或{target}的結尾路徑符號{/}, 會導致不同的結果.
以目錄source為例:
$ mkdir -p source/dir01/dir02
$ touch source/dir01/dir02/{file01,file02,file03}
$ touch source/dir01/{file04,file05,file06}
$ tree source
source
└── dir01
├── dir02
│   ├── file01
│   ├── file02
│   └── file03
├── file04
├── file05
└── file06

$ rsync -avH --delete source target
表示在同步source目錄及其下的檔案與子目錄到target目錄下:
$ tree target
target
└── source
└── dir01
├── dir02
│   ├── file01
│   ├── file02
│   └── file03
├── file04
├── file05
└── file06
因此, 指令應為:
$ rsync -avH --delete source/ target
表示在同步source目錄下的檔案與子目錄到target目錄下, 不含目錄source:
$ tree target
target
└── dir01
├── dir02
│   ├── file01
│   ├── file02
│   └── file03
├── file04
├── file05
└── file06

之後再
$ rm -rf source

ektrontek iT邦研究生 1 級 ‧ 2017-01-30 21:38:37 檢舉

感謝資訊分享

我要發表回答

立即登入回答