1

For changing a specific image's size I use the following command:

mogrify -geometry x50 my_image.png

Every time that I take a new print screen, an image is saved on my ~/Pictures folder. I'd like to make a script that watches my Pictures folder and takes an action copying a reduced sized version of my new image to a different folder on my computer, e.g. ~/.icons/...

I know I could solve this problem using cron, but I don't really want to take actions at regular intervals of time. I want a command ( or a script) that can find out what's different on a folder based on logs or something like this. Is that possible? How can I do it?

1

I've created this functional script that solves my problem using inotify-tools. So I'm leaving it here in case it's useful for someone else.

#!/bin/bash

watchedFolder=~/Pictures
iconsFolder=~/.icons
imageGeometry=100

while [ true ]
do
   fileName=$(inotifywait -q -e create --format "%f" "$watchedFolder")
   if ! [ -d $iconsFolder ]; then mkdir -p $iconsFolder ; fi
   sleep 1s
   cp $watchedFolder/"$fileName" $iconsFolder
   mogrify -geometry x$imageGeometry $iconsFolder/"$fileName"
done

Save it as e.g. ~/automatedIcons.bash and make it executable with chmod +x ~/automatedIcons.bash. Now if you run it, it's already working, it's going to copy every new picture that is created inside the Pictures folder to a new location and change its size. To make it run on boot use crontab -e and write one line with the script's location on it, e.g. @reboot /home/myUserName/automatedIcons.bash.


This is just a functional script. So if anyone has any suggestion about improving the way it works, feel free to write in the comments.

  • you could get rid of the temp file with just fileName=$(inotifywait ...). And you probably should double-quote all the variable expansions, esp. fileName, since it relies on data that comes from outside the script (for the ones set inside the script, you could say you know they are safe). – ilkkachu May 16 '18 at 20:02
  • @ilkkachu Thanks, I've incorporated the changes on the answer already. I've realized that this script doesn't work when there're spaces on the picture's name though. I thought that the double quotes on fileName would solve that but it's still not working. Do you you have any idea why? – Rafael Muynarsk May 16 '18 at 21:07
  • ah, the tr trashes the spaces. You'd need something more careful to remove the unnecessary parts, or just use inotifywait -q -e create --format "%f" to only get the filename. The event and directory are constant here, so they're not really needed. – ilkkachu May 16 '18 at 21:18
  • @ilkkachu Ah yeah... That's easier. I've updated the script again. Now it works with spaces as well. Thanks!! – Rafael Muynarsk May 16 '18 at 21:31
  • 1
    Note that while [ true ] is just testing whether the string true is nonempty. while [ false ] would work just as well. – Kusalananda Jun 30 '18 at 8:50
0

This question (or similar) was answered in Stack Overflow

Mainly says, that you need to use inotify-tools

  • 2
    This would be much more useful with at least a brief sample of how to do it, instead of just the link. Especially when the accepted answer in that SE question is also just a link-only answer, gahh. – ilkkachu May 16 '18 at 13:39
  • You're right. But it's just a point to start from. I would like to "teach" him, but I would use cron or watch – Kyrie001 May 16 '18 at 13:44

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.