Bash – Sort files in a folder into dated folders

bashcronmkdirshell

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?

Best Answer

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.

Related Question