1

I want to pull a single file from multiple similar named file using sftp.

Eg. if my remote server is having below files,

file_0722_005.dat
file_0722_020.dat
file_0722_033.dat
file_0722_0022.dat

I just want to pull any one among them.

In unix I can go for

ls file_0722_*|head -1

But in sftp head/tail won't work.

Please suggest how can this be done!

  • Why not execute the command with head remotely via ssh? – schaiba Jul 22 '16 at 12:43
  • SFTP (or plain FTP) is a protocol that can be implemented on any operating system and in no way linked to Unix. The only thing minimally relevant here is the comparison you make with a Unix shell command but your question remains off-topic. – Julie Pelletier Jul 22 '16 at 12:53
  • I need to pull the file from remote server using sftp and ssh to the server is not allowed! Even I can't use ssh to pull the file. – the_chin Jul 22 '16 at 12:58
  • Since you're not even dealing with a shell script, why don't you just choose the file manually? – Julie Pelletier Jul 22 '16 at 13:19
  • Are we talking about en.wikipedia.org/wiki/SSH_File_Transfer_Protocol ? If so, how can you use SFTP but not SSH? – schaiba Jul 22 '16 at 13:52
2

SSHFS lets you mount a remote filesystem on your local machine. It relies on SFTP as the communication protocol, so if the server allows SFTP, you can use SSHFS. Your client machine must support FUSE which is the case of most modern Unix variants, including Linux, OSX, *BSD, and Solaris.

mkdir myserver
sshfs username@server.example.com:/path/to/directory myserver
cd myserver
set file_0722_*
cp "$1" /local/directory
fusermount -u myserver
1

You could get a different client.

For instance with lftp (lftp sftp://user@host/dir):

glob eval -f 'get "$0"' file_0722_*

However, you cannot use that if file names may contain backslash, double quote or newline characters. If you don't have control over the names of the files, then it becomes a command injection vulnerability (for instance, a file named file_0722_";!reboot" would make you run the reboot command).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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