1

I have written a shell script to get the files from another UNIX server. But files are not being copied. Could someone help me what I am doing wrong.

sftp username@server:$path
get ubpbilp* ./
mget cust.cmp* bunc.cmp* ./
echo "Your files are copied."
2
  • Use get instead of mget. And use just . (dot) for current directory Commented May 15, 2015 at 15:54
  • You can look at Gilles' answer here - unix.stackexchange.com/questions/105667/… Commented May 15, 2015 at 15:56

2 Answers 2

6

Perhaps like this:

sftp username@server <<EOT
cd $path
get ubpbilp*
get cust.cmp* 
get bunc.cmp*
quit
EOT

as sftp doesn't support mget.

1
  • 1
    is this in #!/bin/bash? Commented Sep 25, 2018 at 5:16
2

As others have remarked, you need to pass the get commands as input to sftp. You can do it with a here document. Also, note that sftp doesn't have a mget command.

sftp "username@server:$path" <<'EOF'
get ubpbilp* ./
get cust.cmp* ./
get bunc.cmp* ./
EOF

SFTP isn't very convenient to script. If the server allows scp, use it. If you just want to copy some files, it's easier. You can pass patterns on the command line of scp; make sure to quote them, so that they're expanded on the remote side and not by the local shell. Don't forget the final . indicating that the destination is the current directory.

scp -p \
  "username@server:$path/ubpbilp*" \
  "username@server:$path/cust.cmp*" \
  "username@server:$path/bunc.cmp*" \
  .

If you need to do more than copy files, install SSHFS, if possible. SSHFS provides access to remote files via the normal filesystem mechanism. It uses SFTP under the hood, so if the server allows SFTP, you can use SSHFS, provided that your computer allows client allows FUSE.

mkdir server-dir
trap 'fusermount -u server-dir; rmdir server-dir' 0 HUP INT TERM
sshfs "username@server:$path" server-dir
cp -p server-dir/ubpbilp* cust.cmp* bunc.cmp* .

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.