I have > 35,000 scanned diapositives from a scientific archive in one directory. (Fortunately, the filenames are rather uniformly formatted: {year}-{place}-{film-#}-{photo-#} OR {year}-{year}-{place}-{film-#}-{photo-#}
.)
I'd like to create directories via regex
/awk
named {year}-{place}
OR {year}-{year}-{place}
, and then I'd like to move the corresponding photos into these directories.
I'm aware of these posts which are concerned with very similar problems, but I can't make the "transfer":
- creating directory from filename and move bash
- Sort files into multiple directories based on filename?
There are several problems, but I think my biggest one is that I can't get the regex to work (although they work beautifully on regex101.com
and I escape (
, |
and )
). And even if I could them to work, awk
doesn't seem to support alternative groups of regexes.
Any help is greatly appreciated =)
Non-working example to show problem with my regex:
#!/bin/bash
regex='\([0-9]*-[0-9]*\|[0-9]*\)-[a-zA-Z]*'
liste='1975-Bali.jpg'
echo $liste
[[$liste=~$regex]]
echo "${BASH_REMATCH[0]}"
echo "${BASH_REMATCH[1]}"
gives:
1975-Bali.jpg
./ALT_ordner_erstellen.sh: line 8: [[1975-Bali.jpg=~\([0-9]*-[0-9]*\|[0-9]*\)-[a-zA-Z]*]]: command not found
EDIT:
as Paul_Pedant commented: this is my idea of the structure of the script, in pseudocode:
loop begin
# via awk or sed?
for every filename
mkdir based on {year}-{place} or {year}-{year}-place
mv corresponding files to directory
loop end
mkdir has an option to not create a directory that already exists, correct? Because otherwise my abovementioned idea to do it all in one loop would be not very clever, as many photos are supposed to go in one directory, creating it anew every time would lose the photos already moved, right?
Examples of filenames:
1980-1981-Bali-055-21a.jpg
1980-1981-Bali-055-21.jpg
1980-1981-Bali-055a-21.jpg
1980-Bali-055-21.jpg
1980-Bali-055a-21.jpg
I also thought about not searching the filenames from the beginning, as there definitely can be year-year-place-film-photo as well as year-place-film-photo, but from the end, see this codesnippet:
echo "1980-Bali-055a-028.jpg" | sed -e "s/-[0-9a-zA-Z]*-[0-9]*.jpg$//"
but I'm not 100 % sure that film-photo.jpg is always (!) the end of the filename, there are so many and I didn' scan the diapositives.
Thanks for your input!
[[...]]
extended test syntax has very specific whitespace requirements:[[ $liste =~ $regex ]]
=~
follows the extended regex (ERE) dialect - so you'd want to remove the escaping of parentheses and OR operator:regex='([0-9]*-[0-9]*|[0-9]*)-[a-zA-Z]*'
. See also Why does my regular expression work in X but not in Y?