clsbartek
clsbartek

Reputation: 206

Automate SCP copy files from multiple directories (in brackets) to appropraite directories

I have a bash script used for copy some files from different directories in remote host. All of them have the same parent. So i put them into list:

LIST=\{ADIR, BDIR, CDIR\}

and i use the scp command

sshpass -p $2 scp -o LogLevel=debug -r [email protected]$/PATH/$LIST/*.txt /home/test/test

that command makes me able to copy all of .txt files from ADIR, BDIR, CDIR to my test directory. Is there any option which can put all of .txt files in appropriate directory like /home/test/test/ADIR or /home/test/test/BDIR ... ?

Upvotes: 0

Views: 883

Answers (2)

clsbartek
clsbartek

Reputation: 206

Finally my working code:

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

Upvotes: 0

Matt Johnson
Matt Johnson

Reputation: 358

Have you considered using rsync?

You could try something along these lines:

# 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

Upvotes: 3

Related Questions