首页 文章

将SCP复制文件从多个目录(括号中)自动化到适当的目录

提问于
浏览
0

我有一个bash脚本用于从远程主机中的不同目录复制一些文件 . 他们都有同一个父母 . 所以我把它们列入清单:

LIST=\{ADIR, BDIR, CDIR\}

我使用scp命令

sshpass -p $2 scp -o LogLevel=debug -r $1@192.168.121.1$/PATH/$LIST/*.txt /home/test/test

该命令使我能够将所有.txt文件从ADIR,BDIR,CDIR复制到我的测试目录 . 是否有任何选项可以将所有.txt文件放在适当的目录中,如/ home / test / test / ADIR或/ home / test / test / BDIR ......?

2 回答

  • 0

    你考虑过使用rsync吗?

    你可以尝试这些方面的东西:

    # Rsync Options
    # -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
    # -D                          same as --devices --specials
    # -g, --group                 preserve group
    # -l, --links                 copy symlinks as symlinks
    # -o, --owner                 preserve owner (super-user only)
    # -O, --omit-dir-times        omit directories from --times
    # -p, --perms                 preserve permissions
    # -r, --recursive             recurse into directories
    # -t, --times                 preserve modification times
    # -u, --update                skip files that are newer on the receiver
    # -v, --verbose               increase verbosity
    # -z, --compress              compress file data during the transfer
    
    
    for DIR in 'ADIR' 'BDIR' 'CDIR'
    do
            rsync -zavu --rsh="ssh -l {username}" 192.168.121.1:/$PATH/$DIR /home/test/test/
    done
    
  • 3

    最后我的工作代码:

    SOURCE='/usr/.../'
    DEST='/home/test/test'
    DIRS_EXCLUDED='test/ADIR test/BDIR'
    EXTENSIONS_EXCLUDED='*.NTX *.EXE'
    
    EXCLUDED_STRING=''
    
    for DIR in $DIRS_EXCLUDED
    do
        EXCLUDED_STRING=$EXCLUDED_STRING'--exclude '"$DIR"' '
    done
    
    for EXTENSION in $EXTENSIONS_EXCLUDED
    do
        EXCLUDED_STRING=$EXCLUDED_STRING'--exclude '"$EXTENSION"' '
    done
    
    rsync -zavu $EXCLUDED_STRING --rsh="sshpass -p $2 ssh -l $1" 192.168.xxx.xxx:$SOURCE $DEST
    

相关问题