[英]Automate SCP copy files from multiple directories (in brackets) to appropraite directories
我有一個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 ...?
您是否考慮過使用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
最后是我的工作代碼:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.