4

I am trying to organize my webcam picture files into folders otherwise I get thousands of pictures in one folder.

I have a script foscam-move.sh (from Create sub-directories and organize files by date) with the following:

#!/bin/bash

for x in *.jpg; do
  d=$(date -r "$x" +%Y-%m-%d)
  mkdir -p "$d"
  mv -- "$x" "$d/"
done

I have the script located in the folder with all of the .jpg files.

When I run it in terminal all is fine and it organizes it beautifully.

When I add the following cron task it doesn't run.

* * * * * /home/pi/Desktop/FI9821W_************/snap/foscam-move.sh # JOB_ID_2

I have it set to run every minute because this camera takes a lot of pictures.

How do I get cron to run my script every minute?

2 Answers2

2

cron doesn't run with the same environment as your user. It's likely having issues because it's not in the proper directory. Have your script cd to the directory containing the images before executing your for loop.

jesse_b
  • 35,934
  • 12
  • 91
  • 140
  • Thank you I simply added a cd to my script and it works great. – Michael Lakner Aug 04 '17 at 16:41
  • No problem. This is a good example of why it's generally a bad idea to use relative paths in scripts. It's possible that a script like this could hose your whole system if it's executed in the wrong directory. Whenever possible use full paths to point to a file/dir. – jesse_b Aug 04 '17 at 18:32
  • 1
    im new to all of this, so now I know. – Michael Lakner Aug 04 '17 at 19:43
2

Cron jobs are started with the current working directory being the home directory of the user.

When you execute a script, it doesn't automagically change the working directory to the parent of the script. For that, you'd need to do the cd in the script:

#! /bin/sh -
cd -P -- "$(dirname -- "$0")" || exit
# rest of your script that has nothing bash-specific in it
# by the way.

Or, since that script is not specific to the folder you want to run it, store it in a generic place (like ~/bin) and pass the path of the directory as argument:

#! /bin/sh -
cd -P -- "${1?no directory provided}" || exit
# ...
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
  • Why do you force a login shell (`sh -`)? – n.st Aug 04 '17 at 15:43
  • 1
    @n.st, that's not to _force a login shell_, it's the first (0th) argument that has to start with `-`, not the 2nd for it to be a login shell. See [Why the "-" in the "#! /bin/sh -" shebang?](//unix.stackexchange.com/q/351729) as to why. – Stéphane Chazelas Aug 04 '17 at 15:48
  • @StéphaneChazelas i'm new to scripts and coding. What do I need to do to make a directory based off creation date and not modified date? – Michael Lakner Aug 04 '17 at 16:38
  • @MichaelLakner, that would be a different question. Not many systems make a _creation (birth) time_ available. And it's also an confuse notion. Is that when the file was first open with `O_CREAT` or when it appears in a given directory or when whatever application creating it finished writing to it? Or for jpgs are you refering to the time the picture was taken as stored in the jpg meta data by most cameras? – Stéphane Chazelas Aug 04 '17 at 16:45
  • @StéphaneChazelas I am talking about when it was taken and transferred to the folder I used the script from: https://unix.stackexchange.com/questions/66494/create-sub-directories-and-organize-files-by-date/383941#383941 It states it reads out the modification date of the file. I was just wondering if there was a way for it to read out the creation date so that if something gets accidentally modified it still goes off when it was created not when its modified. – Michael Lakner Aug 04 '17 at 16:58
  • @MichaelLakner, see [group files in folders by name](//unix.stackexchange.com/q/350525) for instance – Stéphane Chazelas Aug 04 '17 at 17:01