2014-01-24 41 views
1

我從多個位置提取文件作爲發佈聚合服務的一部分。我需要一種方法將已經交付給我的文件從一個位置移動到另一個位置,而不會丟失用於排序目錄的目錄列表。有沒有辦法將文件從一組目錄移動到另一組相應的目錄

例子:

Filepath of delivery: Server/Vendor/To_Company/Customer_Name/** 
Filepath of processing: ~/Desktop/MM-DD-YYYY/Returned_Files/Customer_Name/** 

我知道我可以做一些事情,如把所有的目錄:

find Server/Vendor/To_Company/* -exec mv -n ~/Desktop/MM-DD-YYYY/Returned_Files \; 

,但使用的是我只能每天運行腳本一次,並有是我可能需要多次運行它的時候。

看來理想情況下,我應該能夠在我的日常處理文件夾中創建一個副本目錄,然後將文件從一個文件移動到另一個文件夾。

回答

3

您可以使用帶--remove-source-files選項的rsync命令。您可以根據需要多次運行它。

#for trial run, without making any actual transfer. 
rsync --dry-run -rv --remove-source-files Server/Vendor/To_Company/ ~/Desktop/MM-DD-YYYY/Returned_Files/ 

#command 
rsync -rv --remove-source-files Server/Vendor/To_Company/ ~/Desktop/MM-DD-YYYY/Returned_Files/ 

參考:

http://www.cyberciti.biz/faq/linux-unix-bsd-appleosx-rsync-delete-file-after-transfer/

+0

謝謝,@neon!還有一個問題:我可以使用它排除一個目錄嗎?我正在閱讀大量的rsync手冊頁,並沒有看到我可以? –

+0

用於提供'--dry-run'的+1,總是先測試一下吧! – grebneke

+0

你可以使用 - 排除「PATTERN」選項。 http://stackoverflow.com/questions/8720234/rsync-exclude-not-excluding-specific-files – neon

0

你可以使用rsync爲你這樣做:

rsync -a --remove-source-files /Server/Vendor/To_Company/Customer_Name ~/Desktop/$(date +"%y-%m-%d")/Returned_files/ 

添加-n做一個預演,以確保它你想要做什麼。

從手冊頁:

--remove-source-files 
      This tells rsync to remove from the sending side the files (meaning non-directories) that are a part of the 
      transfer and have been successfully duplicated on the receiving side. 

      Note that you should only use this option on source files that are quiescent. If you are using this to move 
      files that show up in a particular directory over to another host, make sure that the finished files get renamed 
      into the source directory, not directly written into it, so that rsync can’t possibly transfer a file that is 
      not yet fully written. If you can’t first write the files into a different directory, you should use a naming 
      idiom that lets rsync avoid transferring files that are not yet finished (e.g. name the file "foo.new" when it 
      is written, rename it to "foo" when it is done, and then use the option --exclude='*.new' for the rsync trans‐ 
      fer). 
相關問題