Shell Script for File Upload to Remote Server

Automating File Uploads with a Shell Script
A frequent requirement for web developers involves transferring a single file from a subdirectory on a local development environment to the corresponding subdirectory on a remote server. This process often proves to be a tedious and time-consuming manual operation, necessitating directory switching on both systems.
To streamline this workflow, a shell script was developed to automate the file transfer process, leveraging scp for secure file copying. (A crucial note: avoid using ftp due to its inherent security vulnerabilities; consider WinSCP as a safer alternative.)
Addressing Directory Differences
A primary challenge was the discrepancy in base directories between the local development WordPress installation and the server installation. The local environment utilizes /var/www, while the server employs a path similar to /var/www/howtogeek/docs/. To resolve this, variables were defined at the script's outset:
SSHSERVER=thegeek@hostname.comRDIR=/var/www/howtogeek/docsLDIR='\/var\/www'
It's important to note that the local directory (LDIR) includes a backslash before each forward slash. This escaping is necessary for subsequent use within a sed command, although alternative approaches may exist.
Setting Up Passwordless SSH Access
Automated SSH logins were implemented to facilitate passwordless scp usage. This begins with generating an SSH key pair using the following command:
ssh-keygen -t rsa
Subsequently, the contents of the ~/.ssh/id_rsa.pub file must be appended to the ~/.ssh/authorized_keys file on the remote server. Assuming the ~/.ssh directory exists on the remote server, the following command can be used to copy the key:
cat ~/.ssh/id_rsa.pub | ssh user@hostname 'cat >> .ssh/authorized_keys'
With passwordless SSH logins established, the scp utility can be employed without requiring password input.
The SCP Command
The core of the script utilizes the following scp command:
scp $1 $SSHSERVER:$RDIR`pwd|sed "s/${LDIR}//"`/`echo $1|sed 's/.*\///'`
Here, $1 represents the filename passed as an argument to the script. $SSHSERVER and $RDIR are variables defining the user@hostname and the remote path prefix, respectively.
The pwd|sed portion of the command extracts the current directory path and removes the local base path (defined by LDIR), preserving the relative path structure. For example, if executed from /var/www/wp-content/, the sed command would remove /var/www, leaving /wp-content/.
The second sed command isolates the filename itself, stripping any preceding path information.
Example Usage
Consider a scenario where the script is named up.sh and executed from /var/www/wp-content/plugins/ with the following command:
up.sh myplugin.php
The shell would expand this to:
scp myplugin.php thegeek@hostname.com:/var/www/howtogeek/docs/wp-content/plugins/myplugin.php
Script Availability
Rather than requiring manual script creation, a pre-built version is available for download. This avoids potential issues with special character handling. The script was, in fact, uploaded using itself.
Download geek-uploader.sh
Place the script in a directory within your system's PATH and rename it as desired (e.g., "up"). Remember to modify the variables at the beginning of the script to reflect your specific environment.
Compatibility
This script is designed to function on any system with a *nix shell and the scp utility. While tested on Ubuntu, it should also operate on Solaris, OS X, or Windows with Cygwin installed.