How can I use file command to know which language my file is written? File command use language tests to get the language file is written, but it seems it do not execute when first two tests - file type and magic number execute properly. How to check only the language a file is written in?
['https://unix.stackexchange.com/questions/363876', 'https://unix.stackexchange.com', 'https://unix.stackexchange.com/users/230526/']
Maybe not very smart solution:

* sort files by name
* loop through names
* compare characters of last loop:

```
last=""
ls -1 $1 | sort | while read file; do
    sub=${file:0:3}
    [ "$last" != "$sub" ] && { echo "NEW GROUP"; last="$sub"; }
    echo "[$sub] $file"
done

```

Instead of echo-ing collect filenames inside an array ...

Just an idea ... example output:

```
NEW GROUP                                                                                                                                                                                                                                    
[Hi8] Hi8-01-002.avi                                                                                                                                                                                                                         
NEW GROUP                                                                                                                                                                                                                                    
[VHS] VHS-01-001.avi                                                                                                                                                                                                                         
[VHS] VHS-01-002.avi
[VHS] VHS-02-002.avi
NEW GROUP
[XZU] XZU

```

Edit 1:
based on [Anthony Geoghegan](https://unix.stackexchange.com/users/22812/anthony-geoghegan) 's answer avoid the pipes at the beginning of the loop and use bash globbing. Take a look at his comment.

Improved script:

```
last=""
for file in *avi; do
    sub=${file:0:3}
    [ "$last" != "$sub" ] && { echo "NEW GROUP"; last="$sub"; }
    echo "[$sub] $file"
done

```

Edit 2:

as asked by @ [Tony Tan](https://unix.stackexchange.com/users/115678/tony-tan) in his third comment:
here you find a straigt forward solution to parse the collected file names to a function. There are many ways to do so. And I don't have much experience in bash scripting ... ;)

```
#!/bin/bash

SOURCE_DIR="$1"
cd "$SOURCE_DIR" || { echo "could not read dir '$SOURCE_DIR'"; exit 1; }

function parseFiles() {
  echo "parsing files:"
  echo "$1"
}

last=""
declare -a fileGroup

for file in *avi; do
  # first 3 chars of filename
  sub=${file:0:3}

  if test -z "$last"; then
    # last is empty. first loop
    last="$sub"
  elif test "$last" != "$sub"; then
    # new file group detected, parse collected
    parseFiles "${fileGroup[*]}"
    # reset array
    fileGroup=()
    last="$sub"
  fi

  # append name to array
  fileGroup[${#fileGroup[@]}]=$file
done

parseFiles "${fileGroup[*]}"

```
With `zsh`:

```
files=(???-??-*.avi)
for prefix (${(Mu)files#???-??-}) encode $prefix*.avi

```

(or `encode ${(M)files:#$prefix*}`)

The equivalent with the GNU shell (`bash`) and tools would be:

```
while IFS= read -u3 -rd '' prefix; do
  encode "$prefix-"*.avi 3<&-
done 3< <(printf '%s\0' ???-??-*.avi | grep -oz '^...-..-' | sort -zu)

```

Same principle. We get the list of files matching the `???-??-*.avi` pattern in the current directory, extract the part that matches (`(M)`/`grep -o`) `???-??-` (regexp `...-..-`), unique them (`(u)`/`sort -u`), and then loop over that list of prefixes.