2

I have a Ruby script that I wrote that sorts some files in a jumble of directories based on it's file extension. It would be very difficult to sort it using a GUI, and its easier for me to just put the file in the topmost directory and let the sorter do the work.

Problem is, I'm a bit of a noob to unix scripting. What I want to be able to do is be able to run that sorter script from anywhere on my computer, without having to

cd Desktop/Whatever/Foo
ruby sorterscript.rb

just write sortfolders at the commandline and have the program be run.

I've tested the script many times, and it works fine, I just want a bit more convenience.

Bonus: If possible, and not too difficult, it would be even better if I could have the program run, say, every hour automatically.

Bobby Tables
  • 1,154
  • 4
  • 15
  • 25

1 Answers1

6

As far as your first question goes, you need to do couple of things:

  1. Add a shebang line to your script (make it the first line of the script):

    #!/usr/bin/ruby (or whatever the path to the Ruby interpreter's executable is, I forget its exact location)

  2. Make the script executable, either via the Finder's "Get Info" context menu, or via the command line, for example:

    chmod 755 my_script.rb

  3. Add the directory location of your script to the PATH environment variable to OS X's launchd.conf file, as described here. You need to add this line:

    setenv PATH /path/to/my/script:$PATH (substitute the real path to your script)

As far as your bonus question goes, you can use cron to set up a recurring job. I never really do this, but here's Apple's cron man page to get you started.

Community
  • 1
  • 1
Peter Roe
  • 446
  • 3
  • 6
  • `Dir.chdir` might also be useful. That changes the cwd of the script so, for example, the script will always run in the same directory no matter where you call it from. – Max Aug 13 '12 at 20:44