Search code examples
bashscp

How do I scp from a server with a variable as the file path?


I am trying to write a bash script that (1) finds the newest file matching a string pattern on a server and (2) copies that file from the server to my local. I have completed the first part but am having trouble with the second.

I have tried all combinations of using quotes, curly braces, escaping the colon, hard coding the root folder on the server, but none of it works.

file_name=$(ssh -t id@server.com "ls -t /path/to/file/on/server/file_name_* | head -1; bash -l") echo $file_name #this works scp id@server.com:$file_name path/to/local #this does not work

Line 2 works, so I assume line 1 works. I can run line 3 with no problems when I type the command into terminal with file_name hard coded. When I run this script, I get an error saying No such file or directoryhalf/of/the/path/to/the/file.csv Basically the error message, no space, then half of the path from the echo in line 2. Any help is appreciated.


Solution

  • Don't understand why you used "ssh -t" and "bash -l". They are not needed. They cause issues in my test environment.

    That said, this kind of issues are very likely caused by invisible characters. To check if it is the case, you may want to echo "[$file_name]" so that you can see what is in it.

    In this particular case, below please see what I've found:

       $ file_name=$(ssh -t testbox "ls -t /home/ec2-user/hello* | head -1")
       Connection to xxx closed. 
    
       $ echo "[$file_name]"
       ]/home/ec2-user/hello  # <--- the expected output is [/home/ec2-user/hello]
    
       $ scp testbox:$file_name ./
       : No such file or directory
    

    In comparison, ssh without -t gives us the expected outcome:

    $ file_name=$(ssh testbox  "ls -t /home/ec2-user/hello* | head -1")
    
    $ echo "[$file_name]"
    [/home/ec2-user/hello]
    
    $ scp testbox:$file_name ./
    hello                                    100%    0     0.0KB/s   00:00