I am logged into ssh to a university account from my home computer. Then I find a file that I want to copy onto my local system. So I open a new shell terminal and am about to use scp to copy that file onto my system. But the path to the file on the system I am ssh‘ed into is long so I select it with the mouse and then paste it with the mouse into the other locally connected terminal.
What I would like to do is something along the lines of: feed the output of pwd into a variable that is temporary and visible to all terminals even though I made it under the ssh‘ed terminal, or some other way that I can’t think of.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The way to do that is to use an ordinary file or a named pipe.
Why not do the scp in the original terminal in the first place (even in the background)?
If the host system uses proc, in the second terminal do cd -P /proc/PID/cwd then do your scp from . (where PID is that of the shell which is in the cwd/pwd that you are interested in).
Method 2
If your terminal supports the zmodem protocol (KDE’s Konsole does), and the remote system has the lrzsz package installed, you can trigger a direct transfer via the terminal using:
sz foo.txt
Konsole prompts you were to save the file and that’s that. GNU screen apparently has zmodem support too.
Method 3
If you have X forwarding active (ssh -X, ForwardX11 in ~/.ssh/config, or any other method), you can manipulate the X selection from the shell:
printf %s "$PWD" | xsel -i
Depending on your terminal emulator, you may want to set the CLIPBOARD (xsel -bi) instead of the PRIMARY selection (xsel -p). You can also use the SECONDARY selection (xsel -si).
You can use the middle mouse button to paste the PRIMARY selection, or (in some terminals) the CLIPBOARD with Ctrl+V, or you can use xsel (or xsel -s or xsel -b) in the shell on the other side.
There are other possible workflows, involving for example a reverse ssh connection, ~ escapes, or zssh. See SSH easily copy file to local system, Download a file over an active SSH session. You could also mount the remote filesystem over sshfs.
Method 4
If you have ssh server running on your home machine, you may do the reverse scp:
[[email protected] ~/rather/long/path/to/interesting/file]$ scp ./secrect.txt [email protected]:~
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0