Reputation: 325
When I take portraits, I name them by the initials of the person in them. For example, the first picture of Robert Gordon would be (in regex formatting) rg.(dng|tiff?|jpe?g|ps(d|b)|xcf)
. The next one would be rg2...
and so on.
If I need to sort them into folders, I use this:
mkdir <initials>/
mv <initials>*.* !$
But more recently, I needed to sort a large amount of photos into folders that contained multiple subjects, so I used this (initials are df for the example:
for f in *.*; do
if echo "$f" | grep -i *df*.*; then
cp -n "$f" df/
echo "$f"
fi
done
But I have a lot of images to go through with a lot of different people, plus when I am done, I have to manually delete them, after confirming that I didn't mess up when naming it so that it didn't get matched.
Is there a way I could separate everything before the extension into two-letter groups, and copy the image to the matching folder, and deleting it when done?
EDIT: I realize a regular expression is not necessary in the given example, but in some of them, I got fast and careless when naming and had errors, so it was easier to use a regular expression to catch all of them at once, even the typos. Obviously, the regex was bigger in that case. Here is an example, name once again edited to Daniel Fortner, in which they were the only subject.
for f in *.*; do
if echo "$f" | grep -Eq '^df?([0-9]*)?(hr)?\..*$'; then
mv "$f" df/
echo "$f"
fi
done
Upvotes: 1
Views: 152
Reputation: 798566
Take advantage of bash's [[
's regex matching and subsequent setting of $BASH_REMATCH
.
#!/bin/bash
for file in *.*
do
[[ $file =~ .*([[:alpha:]]{2})([[:digit:]]*)\.[^.]+ ]] || continue
mkdir "${BASH_REMATCH[1]}" &> /dev/null
mv -v "$file" "${BASH_REMATCH[1]}"
done
Upvotes: 1