0

I am trying to make a photo organizer with a zsh shell script. But i am having trouble creating sub directories within each main directory(based on date). Currently the script starts from a folder i created and gets one argument(the file it needs to edit, hence the first cd $1). Secondly, i do some name changing which is irrelevant for my question. Next i create a directory for each date and move the photo to the correct directory.

The issue is, i want to loop through each date folder and make 2 new sub directories(jpg and raw). But when i run the code i get an error that there is no such file or directory..

Here is my current script:

#!/bin/zsh.
cd $1
for i in *.JPG;
do
        mv $i $(basename $i .JPG).jpg;
done
        
for i in *;
do
       d=$(date -r "$i" +%d-%m-%Y)
       mkdir -p "$d"
       mv -- "$i" "$d/";
done
for d in *;
do
        cd $d
        for i in *.jpg;
        do
                mkdir -p "jpg"
                mv -- "$i" "jpg";
        done

        for i in *.NEF;
        do
                mkdir -p "raw"
                mv -- "$i" "raw";
        done
done

If anyone knows where i made a mistake that would be really helpfull since i have no clue what goes wrong and there is no debugger in nano as far as i know.

Error

➜  files sh test2.sh sdcard1 

test2.sh: line 16: cd: 05-03-2022: No such file or directory
mv: rename *.jpg to jpg/*.jpg: No such file or directory
mv: rename *.NEF to raw/*.NEF: No such file or directory
test2.sh: line 16: cd: 23-10-2021: No such file or directory
mv: rename *.jpg to jpg/*.jpg: No such file or directory
mv: rename *.NEF to raw/*.NEF: No such file or directory
5

1 Answer 1

0
for d in *;
do
        cd $d

As far as I can tell this is the error. You've created a loop over your directories d. You cd into $d. But your loop never cd's back out

        done
        cd ..
done

So on the second iteration with the second $d, you're still in the first subdir, which of course does not contain the second $d as a subsubdir.

Incidently you're ordering by increasing day, %d-%m-%Y. You're free to do that of course, but you might find ordering by year organises the dirs more tidily, %Y-%m-%d.

1
  • Great thank a lot!! :)
    – Mathijs
    Commented May 28, 2022 at 19:11

Not the answer you're looking for? Browse other questions tagged .