LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Directory size changes (https://www.linuxquestions.org/questions/linux-newbie-8/directory-size-changes-909366/)

lonesoac0 10-21-2011 11:26 AM

Directory size changes
 
hello all,

I currently have a Feh script that points to a folder that is shared via Windows (Samba) share. I currently have the capability to drag and drop images from windows to my Debian shared folder and they show up in the Feh slide show. My issue is that I would like for Feh to restart when ever changes are made (IE: images are added) to the directory. Anyone know how to do this?

countach74 10-21-2011 07:58 PM

I'm not familiar with Feh, but you could simply create a script that checks the modified time of the folder and restarts Feh if it has changed and place the script into Cron, running once every couple minutes. It won't update it immediately, but that's probably a good thing. It would be a simple script to write: if you are not familiar with scripts or cron, I can help if you would like.

countach74 10-21-2011 08:18 PM

OK, I was bored, so I wrote up a quick script anyways. I'm not exactly a programmer, so be nice. :)

Code:

#!/bin/bash

# If no temporary file storing last modified date/time exists, create it.
if [[ ! -f /tmp/lastmodified ]]; then
  date -r /home/countach74/testdir +%s > /tmp/lastmodified 2>/dev/null

# Temp file must already exist, so let's scan it and see if the timestamp
# stored in it matches the timestamp of the Feh directory.
else
  prevmodified=`cat /tmp/lastmodified`
  lastmodified=`date -r /home/countach74/testdir +%s 2>/dev/null`

  # If the the timestamp stored in /tmp is different than the current
  # timestamp, update the file in tmp and restart Feh.
  if [[ $prevmodified -ne $lastmodified ]]; then
    echo $lastmodified > /tmp/lastmodified 2>/dev/null
    # Do other things here, such as restart Feh
  fi
fi

exit 0

Now make the script executable and add it to cron and run it every minute.

Code:

chmod +x /path/to/script.sh
crontab -e

Add an entry like this:

Code:

* * * * * /path/to/script.sh >/dev/null 2>&1

lonesoac0 10-22-2011 09:56 AM

1 Attachment(s)
Thank you! I have been playing with bourne shell scripts myself and sometimes it aint easy to program what you want. I will have to give your script a try. Additionally, I found an application called incron in the Debian Squeeze repository. It is very simular to the crontab file but instead of watching for time it watches for changes in directories and executes commands. The main issue that I have is that when I write multiple files to the watched directory by incron , incron launches several instances of Feh (slide show app). The really interesting thing is that I am executing a script called slide_show with incron. The script by itself works perfectly by first checking for any instance of Feh or mplayer and then launching Feh if no instance(s) are found. But with incron the only thing that seems to be executed properly is the part that launches Feh with all of the parameters.

lonesoac0 10-22-2011 09:58 AM

by the way, I took a closer look at your script and you read my mind. I was thinking of the same concept of script.

countach74 10-22-2011 10:12 AM

Doesn't the slideshow script need to terminate any existing Feh instances if they're found? Oh, by the way, one issue with my script is that it will not detect if an image changes--only if one is added or removed. If you wanted to test for modifications, it wouldn't be very hard, actually. I was thinking about it more and probably the easiest way to do that would be to do an ls -l of your image directory and hash it with something like md5sum. Store that instead of the UNIX time of the last modification. It would look something like:

Code:

#!/bin/bash

# If no temporary file storing last modified date/time exists, create it.
if [[ ! -f /tmp/lastmodified ]]; then
  ls -l /home/countach74/testdir | md5sum - | awk '{print $1}' > /tmp/lastmodified 2>/dev/null

# Temp file must already exist, so let's scan it and see if the timestamp
# stored in it matches the timestamp of the Feh directory.
else
  prevmodified=`cat /tmp/lastmodified`
  lastmodified=`ls -l /home/countach74/testdir | md5sum - | awk '{print $1}'`

  # If the the timestamp stored in /tmp is different than the current
  # timestamp, update the file in tmp and restart Feh.
  if [[ $prevmodified != $lastmodified ]]; then
    echo $lastmodified > /tmp/lastmodified 2>/dev/null
    # Do other things here, such as restart Feh
  fi
fi

exit 0


lonesoac0 10-22-2011 02:47 PM

Quote:

"Doesn't the slideshow script need to terminate any existing Feh instances if they're found?"
That is a very good question. I originally did not have the idea for scanning for changes in my images directory, I simply did not want more than one instance of either mplayer or feh to be running at the same time, because I have the options set for full screen on both applications. The applications would run but only the top most app would be seen.

as for your original script, I tried it out and it works great for only the root directory that it is monitoring. I placed another folder inside of the root dir and then removed a few files from the directory inside of the root dir. the application did not start as anticipated.

countach74 10-22-2011 08:56 PM

Yeah, that would be anticipated. If you wanted to scan all the directories, change both instances of the ls -l command to ls -lR in the most recent script that I posted (the one that uses hashing rather than date).

lonesoac0 10-24-2011 09:12 AM

I have had mixed results with impletmenting the second script. When I use the new script, my slide show takes FOREVER! I suspect that since hashing is taking place it may take my application longer to launch. The more photos/movies I have the longer it will take. I may end up going with the first script.

countach74 10-24-2011 11:37 AM

That is odd. Md5 hashing even a very long directory tree should still take less than a second.


All times are GMT -5. The time now is 02:06 AM.