Single sftp line from terminal

Several times during the day I can run a test where I need to view the log file on a remote server. I am used to using my terminal before sftp on a remote server and pulling the desired log file to /tmp on my local computer.

I looked through the options today using man sftp , trying to figure out a way to run the following commands basically on the same line, so I don’t need to type a command, press enter, type a command, press enter, etc.

(What am I doing now)

 sftp myuser@myserver --mypassword at prompt lcd /tmp get /dir/dir/dir/dir/file quit 

I found by looking at man sftp link to scp that I had not used before. I feel that this may be what I am looking for, but I have not seen a way to indicate where I want the reliable copy file to go.

Can someone provide me a way to get /dir/file from a remote server and upload it to /tmp/file_plus-my-description ?

I was hoping to run the sftp or scp command, similar to a regular UNIX copy, for example:

 scp myuser@myserver /dir/file /tmp/file_plus-my-description 

I am using the built-in Terminal on Mac OS X 10.8. Thank.

+42
unix terminal scp sftp
May 23 '13 at 19:02
source share
3 answers

Sep 2017 Update - tl; dr

Download one file from a remote ftp server to your computer:

 sftp username@hostname:remoteFileName localFileName 

Upload one file from your computer to the remote ftp server:

 sftp {user}@{host}:{remote_dir} <<< $'put {local_file_path}' 



Original answer:

OK, I feel a little dumb. But I realized that. I almost had it on top:

 sftp user@host remoteFile localFile 

The only documentation shown on the terminal is this:

 sftp [user@]host[:file ...] sftp [user@]host[:dir[/]] 

However, I came across this site , which shows the following in a summary:

 sftp [-vC1 ] [-b batchfile ] [-o ssh_option ] [-s subsystem | sftp_server ] [-B buffer_size ] [-F ssh_config ] [-P sftp_server path ] [-R num_requests ] [-S program ] host sftp [[user@]host[:file [file]]] sftp [[user@]host[:dir[/]]] 

So, the simple answer is: just do it : after your user and host, and then the remote file and local file name. Incredibly simple!

Single line, remote sftp copy file:

 sftp username@hostname:remoteFileName localFileName sftp kyle@kylesserver:/tmp/myLogFile.log /tmp/fileNameToUseLocally.log 

February 2016 update

In case someone is looking for a command to do the opposite of this, and click the file from the local computer to the remote server in one sftp line, the user @Thariama below posted the solution for this. The tail will tell them an additional code.

 sftp {user}@{host}:{remote_dir} <<< $'put {local_file_path}' 
+100
May 23 '13 at 20:19
source share

To DOWNLOAD a single file, you will need to create a bash script. Something like the following should work on OS X if you have sshpass installed.

Using:

 sftpx <password> <user@hostname> <localfile> <remotefile> 

Put this script somewhere in your path and name it sftpx :

 #!/bin/bash export RND=`cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-f0-9' | head -c 32` export TMPDIR=/tmp/$RND export FILENAME=$(basename "$4") export DSTDIR=$(dirname "$4") mkdir $TMPDIR cp "$3" $TMPDIR/$FILENAME export SSHPASS=$1 sshpass -e sftp -oBatchMode=no -b - $2 << ! lcd $TMPDIR cd $DSTDIR put $FILENAME bye ! rm $TMPDIR/$FILENAME rmdir $TMPDIR 
+4
Dec 23 '14 at 4:55
source share

sftp supports batch files.

On the man page:

 -b batchfile Batch mode reads a series of commands from an input batchfile instead of stdin. Since it lacks user interaction it should be used in conjunction with non-interactive authentication. A batchfile of `-' may be used to indicate standard input. sftp will abort if any of the following commands fail: get, put, rename, ln, rm, mkdir, chdir, ls, lchdir, chmod, chown, chgrp, lpwd, df, symlink, and lmkdir. Termination on error can be suppressed on a command by command basis by prefixing the command with a `-' character (for example, -rm /tmp/blah*). 
+1
May 23 '13 at 19:08
source share



All Articles