Bash Script for Quick and Easy File Transfer via SSH

screenshot of terminal window showing music files being transferred.

In my last post, I covered using the command line to transfer music files to my Raspberry Pi media server. This time I want to share some bash scripts I wrote to make it even easier.

Automated login

It gets old typing ssh dj@192.168.4.59 and then entering my password every time I want to log on. I wanted a shortcut.

I’ve been itching to write some bash scripts for a while, so this was my chance to do something actually useful. It turns out the program is dead simple, with only 3 lines of code:

#!/bin/bash

#A script to log me into the Raspberry Pi

ssh dj@192.168.4.59

ssh dj@192.168.4.59

Easy, right? All I had to do was run my login-pi.sh file and I’m in. Except that it still asked me for a password. Hrmph.

The next thing I learned about was using ssh keys. Setting up ssh key authorization on the Pi meant that I no longer had to enter my password, as long as I’m logging in from my laptop. The Pi recognizes my unique key and says, “Oh hi. It’s you again. Come on in.” Nifty.

Moving the music files

The other thing I wanted to do was set up a program that moves my music files to the right place on the server, with a minimum of typing. So, after I’m done ripping one of my CDs, all I would need to do is run my script and it would automagically upload to the music folder on the server.

This one was a little more complicated to figure out. One thing I learned about programming these files is that you do things in baby steps, then test it. If it works, you move on to the next thing. That makes it easier to debug early on.

Doing it manually, I would navigate to where the files are, then run the command:

scp -r [name of folder] dj@192.168.4.59:/mnt/music/

That’s not really very difficult, but I wanted to type something much shorter, or at least more fun. Maybe it’s a measure of my laziness. Or intelligence. Or silliness. You can decide. Anyway, here’s what I came up with:

#!/bin/bash

#A script to move music files to Pi Media

DESTDIR=dj@192.168.4.59:/mnt/Music

echo -e "Which music folder do you want to transfer?"
read directory

#The quotes around the directory variable below are to allow for spaces in directory names.

scp -r "$directory" $DESTDIR

echo "Your music has been transferred successfully! Or not, you may want to check."

First, I set the variable for the destination directory on the Pi. Then, I have the program ask me which folder I want to transfer. Then, I enter the same old scp -r command with the variables.

Where I started to bang my head on my keyboard was with the directory names. Usually if you have spaces in file or folder names, you either use quotes or an escape character to make it work. Like this:

'The Ting Tings'

The\ Ting\ Tings

For some reason, my script wasn’t letting me do that and I kept getting error messages. With a little internet sleuthing, I found out that you have to allow for the escape characters in the script itself. That’s why the variable "$directory" has quotes around it. This way, I can just enter The Ting Tings when it asks me, without any escape characters at all. Brilliant!

The last echo statement is totally unnecessary for the program to function, but it’s fun.

Here’s the output when I run music.sh:

Terminal window showing Duran Duran songs uploaded

All I have to do is copy the folder name and paste it in where it asks for the directory. Then, {insert wet shloopy sounds}, the folder gets slurped up by the Raspberry Pi.

You can use this script for any file(s), my inspiration just happened to be moving my music.

Get the code

Obviously, you can just copy the code from above. You can also get a version of the code from my GitHub repo: https://github.com/itsjustdj/file-xfer. It’s not specific to music files and you’re free to adapt it for your own purposes. If you do something interesting with it, let me know!

What do you mean, there’s more?

Well, that’s it for now. But I’m not done by a long shot. These programs are kind of cute and all, but they could use some tweaking. For example, what if I want to set up a directory so the music.sh script just loads whatever files/folders are in there, instead of me having to answer a question? And I’m sure I’ll come up with more shortcuts to make my life easier. Stay tuned.

FIN

Did you enjoy this post? Subscribe privately in your favorite RSS reader!

Right-click the icon to copy the link to my feed.

RSS logo

Or, sign up below to get updates and exclusive content sent to your inbox:

Oh hi there 👋 It’s nice to meet you.

Sign up to receive new content in your inbox, at least every month. Maybe a couple of times.

I don’t spam! I’m vegan.


Posted

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *