Harish P C
Harish P C

Reputation: 127

scp multiple files with different names from source and destination

I am trying to scp multiple files from source to destination.The scenario is the source file name is different from the destination file

Here is the SCP Command i am trying to do

scp /u07/retail/Bundle_de.properties rgbu_fc@<fc_host>:/u01/projects/MultiSolutionBundle_de.properties

Basically i do have more than 7 files which i am trying seperate scps to achieve it. So i want to club it to a single scp to transfer all the files

Few of the scp commands i am trying here -

$  scp /u07/retail/Bundle_de.properties rgbu_fc@<fc_host>:/u01/projects/MultiSolutionBundle_de.properties

$ scp /u07/retail/Bundle_as.properties rgbu_fc@<fc_host>:/u01/projects/MultiSolutionBundle_as.properties

$ scp /u07/retail/Bundle_pt.properties rgbu_fc@<fc_host>:/u01/projects/MultiSolutionBundle_pt.properties

$ scp /u07/retail/Bundle_op.properties rgbu_fc@<fc_host>:/u01/projects/MultiSolutionBundle_op.properties  

I am looking for a solution by which i can achieve the above 4 files in a single scp command.

Upvotes: 4

Views: 4722

Answers (2)

Cyrus
Cyrus

Reputation: 88889

With GNU tar, ssh and bash:

tar -C /u07/retail/ -c Bundle_{de,as,pt,op}.properties | ssh user@remote_host tar -C /u01/projects/ --transform 's/.*/MultiSolution\&/' --show-transformed-names -xv

If you want to use globbing (*) with filenames:

cd /u07/retail/ && tar -c Bundle_*.properties | ssh user@remote_host tar -C /u01/projects/ --transform 's/.*/MultiSolution\&/' --show-transformed-names -xv

-C: change to directory

-c: create a new archive

Bundle_{de,as,pt,op}.properties: bash is expanding this to Bundle_de.properties Bundle_as.properties Bundle_pt.properties Bundle_op.properties before executing tar command

--transform 's/.*/MultiSolution\&/': prepend MultiSolution to filenames

--show-transformed-names: show filenames after transformation

-xv: extract files and verbosely list files processed

Upvotes: 1

Toby Speight
Toby Speight

Reputation: 30932

Looks like a straightforward loop in any standard POSIX shell:

for i in de as pt op
do scp "/u07/retail/Bundle_$i.properties" "rgbu_fc@<fc_host>:/u01/projects/MultiSolutionBundle_$i.properties"
done

Alternatively, you could give the files new names locally (copy, link, or move), and then transfer them with a wildcard:

dir=$(mktemp -d)
for i in de as pt op
do cp "/u07/retail/Bundle_$i.properties" "$dir/MultiSolutionBundle_$i.properties"
done
scp "$dir"/* "rgbu_fc@<fc_host>:/u01/projects/"
rm -rf "$dir"

Upvotes: 3

Related Questions