mv,Directory not empty不能目录覆盖

系统 2206 0
    一。
    
mv /test1/* /test2/test1
rm -rf /test1

二。

You can however use rsync with the --remove-source-files option (and possibly others) to merge one directory into another.

rsync won't delete any directories, so you will have to do something like find -type d -empty -delete afterwards to get rid of the empty source directory tree.

  

 

    
      rsync -av /source/ /destination/

(after checking)

rm -rf /source/
      


--remove-source-files has the advantage of only removing files that were transferred successfully,
so you can use find to remove empty directories and will be left with everything that wasn't transferred without having to check rsync s output

    
      cd source; find -type f | xargs -n 1 -I {} mv {} dest/{}


    
  
    
      
        

三。

I'd recommend these four steps:

      
        cd ${SOURCE}; 

find . -type d -exec mkdir -p ${DEST}/\{} \; 

find . -type f -exec mv \{} ${DEST}/\{} \; 

find . -type d -empty -delete


      
    

or better yet, here's a script that implements semantics similar to mv :

      
        #!/bin/bash



DEST=${@:${#@}}; for SRC in ${@:1:$(({#@} -1))}; do   (

    cd $SRC;

    find . -type d -exec mkdir -p ${DEST}/\{} \; 

    find . -type f -exec mv \{} ${DEST}/\{} \; 

    find . -type d -empty -delete

) done


      
    
    
      
        

Here is a script that worked for me. I prefer mv over rsync, so I use Jewel and Jonathan Mayer's solutions.

              
                #!/bin/bash



# usage source1 .. sourceN dest



length=$(($#-1))

sources=${@:1:$length}

DEST=$(readlink -f ${!#})

for SRC in $sources; do

    pushd $SRC;

    find . -type d -exec mkdir -p ${DEST}/{} \;

    find . -type f -exec mv {} ${DEST}/{} \;

    find . -type d -empty -delete

    popd
              
            
    
      
        


if you use use mv --backup=numbered
(or one of the other options for the --backup switch),
then mv will complete the merge and preserve the files intended to be overwritten

mv,Directory not empty不能目录覆盖


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论