有没有办法将文件从一组目录移动到另一组相应的目录

时间:2014-01-24 19:40:12

标签: bash

我从多个地方接收文件,作为发布聚合服务的一部分。我需要一种方法将已经传送给我的文件从一个位置移动到另一个位置,而不会丢失目录列表以进行排序。

示例:

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 \;

但是使用它我每天只能运行一次脚本,有时我可能需要多次运行它。

理想情况下,我应该能够在我的日常处理文件夹中创建一个copycat目录,然后将文件从一个文件移动到另一个目录。

2 个答案:

答案 0 :(得分:3)

您可以将rsync命令与--remove-source-files选项一起使用。你可以根据需要多次运行它。

#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/

答案 1 :(得分: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).