6

如何在 Linux 搬動資料夾與其所有檔案到另一個同名資料夾

 2 years ago
source link: https://blog.miniasp.com/post/2021/06/25/Move-folder-and-files-to-same-folder-name-on-another-disk
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
如何在 Linux 搬動資料夾與其所有檔案到另一個同名資料夾

我今天遇到一個緊急事件,我有台 Linux 主機意外的被一個 Bug 吃光所有磁碟空間,導致服務中斷。我原本想將檔案搬移到另一個地方,但是這個資料夾檔案特別多,不但檔案多,資料夾也多,我先用 mv 搬到另一個地方,空出足夠的空間後,先終止搬移作業,打算先修復服務,然後就無法搬了!💥

今天的事件頗意外的 (哪次事件不意外?XD),我先詳細說一下我的狀況:

  1. 我只能有一個 SSH Session 連入

  2. 空間不足先用 mv 搬動檔案到另一個磁碟

  3. 搬動檔案到一半就先停下來,先處理重要服務

  4. 服務處理到一半,等待的時間我就想繼續用 mv 再搬一次,結果出現以下錯誤:

    unable to remove target: Directory not empty
    
  5. 後來想先 cp 過去,然後再用 rm 刪除檔案,不過由於檔案很多,服務運作中還會持續使用磁碟,所以我比較傾向用搬動的方式移動檔案

  6. 然後想用 find + mv 搬動檔案,但時覺得實在太慢,所以繼續找其他方法,但卻意外的花了好些時間才找到答案!

沒有之一,只有唯一!就是用 rsync 搭配 --remove-source-files 來解決這個問題!

rsync -a --remove-source-files SOURCEDIR /mnt/TARGETDIR/

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK