For example I have an image file that I open, is there a way to attach a bash command to run when I open that file? I have ruby script that I'd like to run when this file is accessed.

Thank you.

Why: For an experiment of mine, nothing hacky in my mind :)

  • The answer will depend on what is the use of all that script.
    – Pilot6
    Jul 19, 2015 at 15:26
  • @Pilot6 I just want to test something for myself, on my pc only.. I want to demo to someone that you could actually open one file and run a script at the same time that you are not aware off. And since I am good at ruby, I'd like to know if I can add a bash script to a file that will run that ruby script. Thanks. Jul 19, 2015 at 15:34
  • Can be easily done with a background script, keeping an eye on if a certain window appears with the filename in its window name. There is a small risk on name clashes however. Jul 19, 2015 at 16:09

1 Answer 1


The only method that springs to my mind is to change the default application for particular type of file to custom bash script:

  1. First of all create a bashscript.sh that does two things: starts Your Ruby script and opens a file in "default" application, something like this:

    /path/to/Your/rubyscript.rb &
    /path/to/default/application "$1"

    Sign & and variable "$1" are crucial - & cause to not wait for execution of ruby script, "$1" is the name (and path) of Your file, so You have to pass them to "default" application. You may also pass them to Your ruby script. Make sure Your bash script is executable - sudo chmod +x bashscript.sh

  2. Right click on Your file, select Open with ... -> Other application

  3. Click on Use a custom command and type a path to bashscript.sh

  4. Tick Set as default to make changes permanent

Now when You double-click on file with the same type Your ruby script should run and file should open in default application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.