12

I'm in an SFTP session and navigated to a deep directory. I did ls -l, saw a small file, and want to just read it. I would prefer to do it while I'm still in SFTP, rather than starting up a new terminal, doing SSH, navigating to that deep directory, and then reading it, then going back to SFTP to continue.

Is there a way to do that?

SFTP has no cat command.

I tried get file -, hoping it would treat - as stdout, but that just creates a file called "-".

I tried get file /dev/stdout, but that results in:

sftp> get file /dev/stdout
Fetching /home/username/file to /dev/stdout
/home/username/file                100%  506     9.0KB/s   00:00
ftruncate "/dev/stdout": Invalid argument
Couldn't write to "/dev/stdout": Illegal seek

Another way might be get file /tmp/file, come out of SFTP, read /tmp/file, delete /tmp/file, and get back into SFTP, but that's equally as tedious. Is there an easy way?

k314159
  • 445

2 Answers2

14

Having cat in sftp would mean that the contents of the file will still have to travel through the network, to your local machine, to be displayed for your eyes, which is basically, you get ther file. You don't have to "come out of SFTP", as sftp has !command.

So, you can do:

sftp> get file
sftp> !cat file
Pourko
  • 1,844
  • 4
    Useful to know. I guess you might also want do lcd /tmp/ or similar before downloading, so the file goes into your own /tmp folder rather than littering whatever directory you ran sftp from. – mwfearnley Jan 30 '21 at 14:36
3

You cannot execute cat via sftp because it is designed specifically for file transfer. You can check "man sftp" to find the list of commands that you can use while in the interactive shell.

max
  • 41