Thursday 6 December 2012

Bash Script: Random File Sync

A simple bash script I wrote.

It will randomly sync files from a source directory to a destination directory.

Features:

  • Prompt for megabytes to move; defaults to 4 GB.
  • Prompt for a source destination.
  • Searches all sub-folders in source directory for files
  • Prompt for a destination directory.
  • Option to delete some, all or none of the files in destination directory.
  • If "none" is typed. No files are deleted.
  • If "all" is typed. All files and folders in destination directory are deleted.
  • If "some" is typed. User is prompted for megabytes to delete. This then becomes the amount of megabytes moved.
  • Files are randomly deleted from destination directory.
  • Files are chosen at random from source directory and moved to destination directory.
Why:

This was written because I could not find a media player that would sync a directory to a directory. I mount my Android phone via sshfs to a folder in my home directory and wanted to randomly sync music files to it from my server. My music collection is quite large and going though it can be laborious. This program suits my needs. It can delete songs at random from my phone and replace them; or it can add files to my phone if I want increase the amount of music on it; or it can completely rebuild the music directory.

Limitations:
  • No error checking. If you enter a wrong parameter it will probably just error out.
  • No auto-completion when choosing directories. You must know where your file are coming from and going to.
  • Does not check if there is enough room in the destination folder. Sshfs (the version I use at least) does not support the checking of free space on a mounted directory. There is some code commented out at the bottom of the script to perform this action for regular directories. It was not tested very much so it may work for you, it may not.
  • Default values are set for me. Change them in the script to your preferences.
Usage:

Create a text file and copy the code below to it.
Save file into a folder you have permission to work in
From the command line type: 

chmod +x foo

where foo is the name you saved the file under.

To run the script from the command line type:

./foo

while in the directory you saved the file in.


No comments:

Post a Comment