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:


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

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 Answers 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.

  • Thank you I simply added a cd to my script and it works great. Aug 4, 2017 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 4, 2017 at 18:32
  • 1
    im new to all of this, so now I know. Aug 4, 2017 at 19:43

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
# ...
  • Why do you force a login shell (sh -)?
    – n.st
    Aug 4, 2017 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? as to why. Aug 4, 2017 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? Aug 4, 2017 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? Aug 4, 2017 at 16:45
  • @StéphaneChazelas I am talking about when it was taken and transferred to the folder I used the script from: unix.stackexchange.com/questions/66494/… 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. Aug 4, 2017 at 16:58

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.