How to filter files when using scp to copy dir recursively?

前端 未结 9 1410
醉酒成梦
醉酒成梦 2021-01-29 20:00

I need to copy all the .class files from server to local with all dir reserved. e.g. server:/usr/some/unknown/number/of/sub/folders/me.class will be /usr/proj

9条回答
  •  独厮守ぢ
    2021-01-29 20:35

    With ssh key based authentication enabled, the following script would work.

    for x in `ssh user@remotehost 'find /usr/some -type f -name *.class'`; do y=$(echo $x|sed 's/.[^/]*$//'|sed "s/^\/usr//"); mkdir -p /usr/project/backup$y; scp $(echo 'user@remotehost:'$x) /usr/project/backup$y/; done
    

提交回复
热议问题