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

前端 未结 9 1264
醉酒成梦
醉酒成梦 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:14
    1. Copy your source folder to somedir:

      cp -r srcdir somedir

    2. Remove all unneeded files:

      find somedir -name '.svn' -exec rm -rf {} \+

    3. launch scp from somedir

    0 讨论(0)
  • 2021-01-29 20:22

    To exclude dotfiles in base directory:

    scp -r [!.]* server:/path/to/something
    

    [!.]* is a shell glob that expands to all files in working directory not starting with a dot.

    0 讨论(0)
  • 2021-01-29 20:24

    I'd probably recommend using something like rsync for this due to its include and exclude flags, e.g:-

    rsync -rav -e ssh --include '*/' --include='*.class' --exclude='*' \
    server:/usr/some/unknown/number/of/sub/folders/ \ 
    /usr/project/backup/some/unknown/number/of/sub/folders/
    

    Some other useful flags:

    • -r for recursive
    • -a for archive (mostly all files)
    • -v for verbose output
    • -e to specify ssh instead of the default (which should be ssh, actually)
    0 讨论(0)
  • 2021-01-29 20:24
    scp -i /home/<user>/.ssh/id_rsa -o "StrictHostKeyChecking=no" -rp /source/directory/path/[!.]* <target_user>@<target_system:/destination/directory/path
    
    0 讨论(0)
  • 2021-01-29 20:27

    Below command for files.

    scp `find . -maxdepth 1 -name "*.log" \! -name "hs_err_pid2801.log" -type f` root@IP:/tmp/test/

    1. IP will be destination server IP address.
    2. -name "*.log" for include files.
    3. \! -name "hs_err_pid2801.log" for exclude files.
    4. . is current working dir.
    5. -type f for file type.

    Below command for directory.

    scp -r `find . -maxdepth 1 -name "lo*" \! -name "localhost" -type d` root@IP:/tmp/test/

    you can customize above command as per your requirement.

    0 讨论(0)
  • 2021-01-29 20:30

    There is no feature in scp to filter files. For "advanced" stuff like this, I recommend using rsync:

    rsync -av --exclude '*.svn' user@server:/my/dir .
    

    (this line copy rsync from distant folder to current one)

    Recent versions of rsync tunnel over an ssh connection automatically by default.

    0 讨论(0)
提交回复
热议问题