1

Say I had a folder full of cat images. I'd like to create a symlink that points to a random cat from that folder and changes each time the symlink is accessed.

I suppose I could create a script that is constantly re-assigning the symlink to a different random cat image at N intervals. Could be a bit more ideal to only change it each time the symlink is accessed. Is there any simple way to get an on-accessed event for a sym-link to trigger some code?

2
  • 1
    If you're using Linux, there's incron
    – muru
    Commented Dec 24, 2021 at 4:20
  • I am on Linux and that looks like exactly what I need :) Thank you. Commented Dec 24, 2021 at 4:31

1 Answer 1

1

Instead of a symlink, you could use an alias, which does not require constant updating of the file path:

alias random-cat='sxiv "$(ls <path/to/images/*> | shuf | head -1)"'

This will list out all your images, shuffle them in random order and pick the first random result to be opened in an image viewer.

Replace sxiv with your preferred image viewer.

This can be added to your ~/.bashrc and be executed from anywhere in the terminal. Or you could simply add the command line inside the single quotes to a script.

1
  • This is a good solution. My needs personally though call for a symlink as the software I want to pull random cat images can only be configured to handle a valid filepath, so an executable script or alias won't do. Commented Dec 24, 2021 at 4:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .