Search code examples
linuxbashunixscp

Copy few files (of different extenstion e.g. xml, crt. jks) from one server to another server using scp


I have a requirement where I need to copy few files of different extension e.g. xml, crt, jks from one server to another server using scp.

As of now, its copies all file from the directory (/tmp) and put that in destination server under newly created subdirectory tmp. Once files are transferred to the destination server, I need to execute few commands using copied files and some result files will be generated.

Once commands are executed in destination server, I need to copy the result files in source server but again it copies all the files :-(

I tried these commands but no luck

$scp '/tmp/*.{xml,crt,jks}' user@<destination server>

This is causing unnecessary file transfer across network, so I am trying to reduce the same.


Solution

  • Trying to conditionally select which file extensions should get scp'd over without sending all of them? No problem, you are a step in the right direction anyways! Lets take a moment to see what your original command was doing however...

    $scp '/tmp/*.{xml,crt,jks}' user@<destination server>
    

    Ok so from a command perspective,

    "I'm going to run the scp command and then look for a terminating ' character, and treat the internal as a literal. I see we will be looking for files within /tmp and take the file named *.{xml,crt,jks}. I see the ending ' so the literal is done, now I'll migrate this over to the passed server."

    Its no surprise that the command failed because you probably don't have any files named *.{xml,crt,jks} in your /tmp folder. You were not wrong in using {} according to the documentation on wildcards, however the problem you ran into was a simple mistake of literals blocking shell expansion. Your use of '' caused the input to be treated as an actual literal, thereby removing the need for bash to expand out those wildcards and conditionally do your operation. Therefore, the true command is...

    scp /path/to/files/{*.fileType1,*.fileType2,...} user@<destination server>

    I suggest reading more into shell expansions and doing more research into getting variables or expandable terms within quotes or literals if you wish to keep the '' and still have variable terms.