EDIT: I continue to be the slowest to reply... as usual.
Hi safarin.
First, you can cycle through all your images with something like this:
Code:
#!/bin/bash
for file in *.JPG
do
echo "$file"
done
Note that this would only consider images ending in JPG.
If you also have jpg, jpeg, and others, you would need to include it in the expression:
Code:
#!/bin/bash
for f in *.JPG *.jpg *.jpeg
do
echo "$file"
done
Once you are sure you are getting all images in your loop, you can start using parameter substitution to get the directory part out of the filename.
The following expression would get the directory as you wanted:
Code:
${file%-[0-9][0-9]_*}
You can test that by doing:
Code:
#!/bin/bash
for file in *.JPG
do
echo "moving $file --> ${file%-[0-9][0-9]_*}"
done
But the directory does not exist yet. You could create it with the 'mkdir' command, but that would give you an error in subsequent intents to create the same directory. Testing if the directory exists would solve the problem.
Code:
#!/bin/bash
for file in *.JPG
do
dir="${file%-[0-9][0-9]_*}"
if [ ! -d "$dir" ]; then
echo mkdir "$dir"
fi
echo mv -iv "$file" "${file%-[0-9][0-9]_*}"
done
Now, if you feel you are ready to roll, remove both words echo on the script to actually create the directories and move the files.
Hope it helps. Let us know how it goes.
Regards.
P.S: If you want to continue exploring study the following alternative skeleton for the cycle:
Code:
#!/bin/bash
while IFS= read -r -d '' file
do
echo "$file"
done < <(find . -maxdepth 1 -regex '.*jpg\|.*jpeg' -print0)
Bookmarks