This is a shell script to upload a file to the same subdirectory on a remote server with Shell Script. #!/bin/bash if [ -d $HOME/.ssh ]; then mkdir $HOME/.ssh/authorized_keys export SSH_AUTH_SOCK=/tmp/authorized_keys fi $ echo “Usage: ssh [-h] [-v] [-C ] [-D ] [-F ]” » $HOME/.ssh/config $ ssh -h localhost -v 2>&1 | tee $HOME/.ssh/config


So, I’ve taken it upon myself to write a shell script to automate this for me, using scp for secure file copying. (Important Side Note:ftp is horribly insecure, use WinSCP instead)

The main problem to solve was that my WordPress install on my development machine has a different base directory than the installation on my server. On my development machine it’s /var/www, and on my server is something more like /var/www/howtogeek/docs/. I decided to just move those into variables at the beginning of the script, like this:

Note that the local directory(LDIR) in the script has a \ before each / in the path. This is because we are going to pass this into a sed command later in the script, so we have to escape those values. I’m sure there’s some other way around this, but I’m unaware of it.

Next, I set up automated ssh logins. To do this, you need to first run the ssh keygen utility like so:

Then you need to copy the ~/.ssh/id_rsa.pub file to ~/.ssh/authorized_keys file on the remote server. If the ~/.ssh directory is already created on the remote server (might want to check), then you can run this command to copy the keys over:

Now we’ve got automated ssh logins, which will enable using the scp utility without a password. Now let’s take a look at the scp command we’ll use in the script:

The $1 is the parameter you’ll pass to the script for the filename you are trying to upload. $SSHSERVER and $RDIR are variables we’ll set at the beginning of the script to set the user@hostname and the preceding remote path.

The first block that starts with pwd|sed takes the current directory path and strips off the preceding part of the path that doesn’t match the remote server, but leaves the rest of the path. For instance, if you were in /var/www/wp-content/ when you ran the command, the sed block would strip off the /var/www based on our LDIR variable, but leave the /wp-content/ part of it.

The second block just makes sure to strip off the entire preceding path off the filename passed to the script so you just get the filename.

For example, if you named the script up.sh and ran it from your local /var/www/wp-content/plugins/ directory on a file:

This would have been expanded by the shell as the following:

Instead of trying to create your own script file based on the article, I’ve provided it for you to download. That way there won’t be any issues with special characters not working… and yes, I used the script to upload itself =)

Download geek-uploader.sh

Just put it somewhere in your path, and name it whatever you want. I renamed mine to just “up”, so I can just type “up filename.txt” to upload. Also remember to open it up and change the variables at the beginning of the script to match your environment.

This script should work on anything with a *nix shell and the scp utility. I’m using Ubuntu, but it should work on Solaris, OS X, or even Windows with Cygwin installed.