'How to check if there are jpg in a folder and then sort them by date in other folders in Bash?
I am making a Bash script to order the photos that are entering a folder at different times and days (not every day there are photos) as follows. The photos must be moved to a folder called PhotosOrder where for each day there is a folder with the date. The task is executed in a synology server and later it is synchronized with syncthing to a windows server. First I must say that I generalize it since I must execute it in many different folders and I am duplicating the script for each one. That surely is optimizable but we will get to that after it works. The script must check if there are jpg and lists them in an auxiliary variable arr Checks that this list is not empty in an if, if it is it does nothing but if there is jpg then it makes:
Creates the folder for the current day. It counts the number of photos that there are, because as at different times different people put photos I want to avoid that none is overwritten. It moves the photos renaming them taking into account the previous number and the parameters of name that I set at the beginning. I have to say that I can't delete the empty folders afterwards because if I don't delete a folder that syncthing uses later to synchronize (I synchronize that folder with a folder on another server). So far an alternative script works for me that creates a folder every day whether or not there are photos and moves them (if there are any) but then I have to delete the empty folders by hand. If I tell the script to delete those empty folders then it deletes the folder that syncthing uses and it doesn't sync with the other server anymore (besides that I don't think it's optimal either). Hence the if loop to check if there are photos before doing anything.
The script I have for now is this one:
this one:
#!/bin/sh
#values that change from each other
FOLDER="/volume1/obraxx/jpg/"
OBRA="-obraxx-"
#Create jpg listing in variable arr:
arr=$$(ls -1 /volume1/obraxx/jpg/*.jpg 2>/dev/null)
#if the variable is not empty, the if is executed:
if [[ !(-z $arr) ]]; then.
#Create the folder of the day
d="$(date +"%Y-%m-%d")"
mkdir -p "$FOLDER"/PhotosOrdered/"$d"
DESTINATION="$FOLDER/PhotosOrder/$d/"
#Count existing photos:
a=$$(ls -1 $FOLDER | wc -l)
#Move and rename the photos to the destination folder.
for image in $arr; do
NEW="$PICTURE$a"
mv -n $image $DESTINATION$(date +"%Y%m%d")$NEW.jpg
let a++
done
fi
Solution 1:[1]
- The shebang line should look like
#!/bin/bash, not#!/bin/sh. - Your usage of arrays has syntax problems.
- You should not parse the output of
ls. - You are counting the existing photos in the source folder. It should be the destination folder.
- You are putting the current date in both folder name and the file name. (I do not know if this is the requirement.)
- The variable
OBRAis defined but not used. - The variable
PICTUREis not defined. - It is not recommended to use uppercases for user's variables because they may conflict with the system variables.
Then would you please try the following:
#!/bin/bash
prefix="picture" # new file name before the number
src="/volume1/obraxx/jpg/" # source directory
# array "ary" is assigned to the list of jpg files in the source directory
mapfile -d "" -t ary < <(find "$src" -maxdepth 1 -type f -name "*.jpg" -print0)
(( ${#ary[@]} == 0 )) && exit # if the list is empty, do nothing
# first detect the maximum file number in the destination directory
d=$(date +%Y-%m-%d)
dest="$src/PhotosOrder/$d/" # destination directory
mkdir -p "$dest"
for f in "$dest"*.jpg; do
if [[ -f $f ]]; then # check if the file exists
n=${f//*$prefix/} # remove substings before prefix inclusive
n=${n%.jpg} # remove suffix leaving a file number
if (( n > max )); then
max=$n
fi
fi
done
a=$(( max + 1 )) # starting (non-overwriting) number in the destination
# move jpg files renaming
for f in "${ary[@]}"; do
new="$prefix$a.jpg"
mv -n -- "$f" "$dest$new"
(( a++ )) # increment the file number
done
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tshiono |
