Trigger file transfer from a shell session

Have you ever wished you could transfer files from/ to current working directory of remote shell without having to run a separate scp or sftp command?

Core Shell can help make your wish come true. This feature requires you install a simple script to report current working directory, we will discuss it later, first let's look at how Core Shell make your daily file transfer more efficient.

Upload

You drag a local file or folder, then drop it on the shell window:

image

And an uploading task will be created immediately:

The file end up uploaded to your current working directory of the host, i.e. ~/Public in this example.

Download

Downloading is also straightforward, right-click on the file name, select Download [FILEPATH] from context menu or press key combination โ‡งโŒ˜D:

image

Then a downloading task will be created:

The download progress is synced to Finder, you can also cancel a running download from the Finder or Dock.

image

Enable Reporting Current Working Directory

When you try to upload a file for the first time (or select Shell > Enable Reporting Current Working Directory from main menu), Core Shell would requests for update $PROMPT_COMMAND on the host:

image

Core Shell has no idea about the current working directory (aka $PWD) of the shell session, but in order to create SFTP transfer tasks on your behalf, Core Shell needs to know that value. An update on $PROMPT_COMMAND environment variable can tell Core Shell the value of $PWD at each prompt.

Take the above downloading task example, Zsh/ Bash reports the current working directory is pointed to /home/, when you try to download letsencrypt directory, Core Shell creates a SFTP task in background and push it into the queue:

sftp [OPTIONS] user@host -p PORT
> get /home/letsencrypt ~/Downloads/
> bye

You generally press Continue button then you're all set. Please continue reading if you are curious to know how it works.

How it works

After you press Continue button, Core Shell prints a command on current shell:

sh -c "$(curl -fsSL https://raw.githubusercontent.com/codinn/core-shell-scripts/master/install.sh)" && . "$HOME/.shrc_Core_Shell"

This command executes a shell script install.sh on your remote host, this script creates a Zsh/ Bash startup file .shrc_Core_Shell in $HOME directory, and will be loaded when you login via a SSH connection.

Is this startup file secure? The short answer is absolutely.

If you have some background in Zsh/ Bash scripting, you will find the .shrc_Core_Shell script does nothing special except reporting the current working directory at each prompt.

This function has been implanted by default on Apple's macOS for over a decade. In Terminal.app, the represented directory in title bar is changing with your current working directory automatically:

image
macOS achieve this by loading /etc/zshrc_Apple_Terminal when you start a shell. /etc/zshrc_Apple_Terminal goes into effect if you use Terminal.app to start a shell on macOS.

.shrc_Core_Shell uses exactly the same solution in /etc/zshrc_Apple_Terminal, except .shrc_Core_Shell goes into effect if you login via a SSH connection.

Troubleshooting

My ~/.bashrc is not executed anymore after .shrc_Core_Shell installed

If you login via a SSH connection, on some system the ~/.bashrc only be executed if ~/.bash_profile does not exists. While you were installing .shrc_Core_Shell, ~/.bash_profile was created for loading ~/.bashrc_Core_Shell with this line:

[[ -n "$SSH_TTY" || -n "$SSH_CONNECTION" ]] && [ -r "$HOME/.shrc_Core_Shell" ] && . "$HOME/.shrc_Core_Shell"

The solution is tell the ~/.bash_profile also loads ~/.bashrc, edit ~/.bash_profile and add this line right at the very beginning:

[ -r "$HOME/.bashrc" ] && . "$HOME/.bashrc"
1 Like