1

Situation:

  • a folder with 80,412 files
  • all files named with CamelCase, some have a dash (-) separating the first word
  • files can be grouped by beginning with the same word or letters(i.e. "ClientslistRebecca..." and "Clientslist-Adam...")
  • no file name begins with a number
  • very few files begin with a unique word

I need to take the first word, find all files that begin with that word, and create a folder named with that word. Repeat for all files.

The main complication is that sometimes a name begins with multiple capitals, followed by a word that also begins with a capital (i.e. "AWDRebecca..."). Not sure how to resolve that, but I'm fine with doing some manual cleanup afterwards if I get folders with "AWDRebecca", "AWDReports", "AWDNovember", but having a folder "A" will make it harder.

9
  • 1
    This probably requires a scripting solution. What have you tried so far?
    – nohillside
    Commented Feb 22, 2022 at 7:36
  • Also, I assue that files starting with "AWDRebecca", "AWDReports", "AWDNovember" should end up in a folder called "AWD", right?
    – nohillside
    Commented Feb 22, 2022 at 7:37
  • I just started on this task around, I use Automator workflows with Run Shell Script for most automatization. Commented Feb 22, 2022 at 7:42
  • 3
    We are not a script writing service, so if you have some steps of this already, please add them to the question.
    – nohillside
    Commented Feb 22, 2022 at 7:44
  • 1
    I would just use shell scripts rather than automator calling shell scripts. ALso hard code the odd cases like AWD and move those first
    – mmmmmm
    Commented Feb 22, 2022 at 9:20

1 Answer 1

1

A shell snippet like

for i in *; do
  case "$i" in
    [A-Z][A-Z]*) # $i has several leading caps
        dir=$(sed -E -e 's|([A-Z]+)[A-Z].*|\1|' <<< "$i")
        echo mkdir -p "$dir"
        echo mv "$i" "$dir"/
        ;;
    [A-Z][a-z]*) # $i has one leading cap
        dir=$(sed -E -e 's|([A-Z][a-z]+).*|\1|' <<< "$i")
        echo mkdir -p "$dir"
        echo mv "$i" "$dir"/
        ;;
    *) echo "Huh: $i"
       ;;
  esac
done

should work. As written it will just print the commands on the screen. If they look sensible, remove the four echo before mkdir/mv.

Also, for 80'000 files, the for i in * part may fail. If this is the case, run it in batches with for i in [A-E]*, for i in [F-K]* etc.

PS: It will fail for files with a ALLCAPS name. If you have does, please comment so I can add another rule.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .