Skip to main content
added 1214 characters in body
Source Link
Beres
  • 53
  • 5

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!

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!

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":

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

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":

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

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":

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

I have >35 000> 35,000 scanned diapositives from a scientific archive, all in one directory. Fortunately(Fortunately, the filenames are rather uniformly formatted: {year}-{place}-{film-#}-{photo-#} OR {year}-{year}-{place}-{film-#}-{photo-#}{year}-{place}-{film-#}-{photo-#} OR {year}-{year}-{place}-{film-#}-{photo-#}.)

I'd like to create directories via regexregex/awk, these should be calledawk named {year}-{place}{year}-{place} OR {year}-{year}-{place}{year}-{year}-{place}, and then I'd like to move the corresponding photos should be moved tointo these directories.

I'm aware of these posts (https://stackoverflow.com/questions/13020720/creating-directory-from-filename-and-move-bash; Sort files into multiple directories based on filename?) which are concerned with very similar problems, but I can't make the "transfer".:

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.comregex101.com and I escape "(" and "|"(, | and ")") ). And even if I could them to work, awkawk 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

I have >35 000 scanned diapositives from a scientific archive, all in one directory. Fortunately, the filenames are rather uniformly: {year}-{place}-{film-#}-{photo-#} OR {year}-{year}-{place}-{film-#}-{photo-#}. I'd like to create directories via regex/awk, these should be called {year}-{place} OR {year}-{year}-{place}, then the corresponding photos should be moved to these.

I'm aware of these posts (https://stackoverflow.com/questions/13020720/creating-directory-from-filename-and-move-bash; Sort files into multiple directories based on filename?) which are concerned with very similar problems, but I can't make the "transfer".

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 ")" ). 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

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":

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
Source Link
Beres
  • 53
  • 5
Loading