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."
Aravind
  • 1,599
  • Use get instead of mget. And use just . (dot) for current directory – Romeo Ninov May 15 '15 at 15:54
  • You can look at Gilles' answer here - https://unix.stackexchange.com/questions/105667/upload-file-to-ftp-server-using-commands-in-shell-script – rahul May 15 '15 at 15:56

2 Answers2

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.

lcd047
  • 7,238
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* .