I'm trying to match a filename to a string, which is basically a timestamp, during an SFTP connection.

How do I match part of the filename? I can't seem to declare a variable within an SFTP connection. And neither can I call a variable created in Bash beforehand.

Edit: things I've tried

yr=$(date + "%Y")
sftp -i key.pem un@server
sftp> echo $yr
Invalid command.
sftp> $yr
Invalid command.

new shell

sftp -i key.pem un@server
sftp> test=$(date +"%Y")
Invalid command.

Tried those 2

  • 1
    Hi! It would be helpful if you edited your question to show the exact command you are using, so we can tell what you tried and what, precisely, doesn't work. – dhag Sep 10 '15 at 16:43
  • SFTP is a protocol. Can be used in various ways – Ed Heal Sep 10 '15 at 16:46
  • added in code of what I tried – simplycoding Sep 10 '15 at 17:22
  • Use expect - that should do the business for you – Ed Heal Sep 10 '15 at 17:24
  • If I use expect, I need to probably do a bunch of expect and send lines, right? Or can I just use expect once for the timestamp? – simplycoding Sep 10 '15 at 18:03

The sftp program is its own thing; it's completely independent from and unrelated to bash, which knows the value of its variables and would expand $yr. In general, you can't pass bash variables into external programs like that without application-specific communication methods.

If you just want to upload or download a file with $yr in the filename, you could use scp instead, something like:

yr=$(date +%Y)
scp -i key.pem un@server:/some/long/path/that/includes/$yr .

for download, or

scp -i key.pem some_local_file un@server:/some/long/$yr/path

for upload.

If for some reason you really require using sftp specifically, you could create a batch file dynamically, e.g.

sftp -i key.pem -b - un@server <<< "get /some/path/with/$yr"
  • Yeah I looked into scp but it's not allowed on the server. I'm looking into expect at the moment – simplycoding Sep 10 '15 at 17:53
  • expect can do a lot, but it's fundamentally a hack used to allow scripting of programs which aren't designed to be scriptable. If you can use a different method, e.g. the -b option to sftp, it's usually easier. (Parenthetically, how is your server set up, that sftp is allowed but scp isn't?) – Tom Hunt Sep 10 '15 at 17:57
  • It's not my server, but all I know at the moment is that scp isn't allowed. Could you explain aht -b would do? Looking at documenation, its effect is that it notifies when jobs running in background terminate – simplycoding Sep 10 '15 at 18:02
  • Should I be able to set a variable in a here-doc? Or would that throw the same error as before? – simplycoding Sep 10 '15 at 18:25
  • You can expand variables in either heredocs or herestrings (herestring is the syntax I used above). – Tom Hunt Sep 10 '15 at 20:19

sftp is a standalone program, not a part of the shell. It doesn't understand shell syntax. It doesn't have features such as variable expansion and conditional statements.

The easiest way to do complex things over SFTP is to use SSHFS. SSHFS is a filesystem that uses SFTP to make a remote filesystem appear as a local filessytem. On the client, SSHFS requires FUSE, which is available on most modern unices. On the server, SSHFS requires SFTP; if the server allows SFTP then you can use SSHFS with it.

mkdir server
sshfs -o IdentityFile=key.pem un@server: server
cp "server/dir/myfile-$(date +%Y).txt" /local/path
fusermount -u server

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.