Skip to main content
Add explanation, caveat/limitation by the script as pointed out by Stéphane Chazelas
Source Link
Jetchisel
  • 1.6k
  • 9
  • 12

NOTE: As per Stéphane Chazelas the separator must not be part of the filename, in this case a dot/period. Also the script works with the given file names by the OP.

Files like:

(In the future, it can be possible that there are files with the ending name.part001.rar , name.part002.rar , ... , name.part123.rar )

Cannot be parsed properly by the current script.


NOTE: As per Stéphane Chazelas the separator must not be part of the filename, in this case a dot/period. Also the script works with the given file names by the OP.

Files like:

(In the future, it can be possible that there are files with the ending name.part001.rar , name.part002.rar , ... , name.part123.rar )

Cannot be parsed properly by the current script.

added 3 characters in body
Source Link
Jetchisel
  • 1.6k
  • 9
  • 12

If yourUsing bash that has the loadable builtin kv (most probably 5.3+ version)

If your bash has the loadable builtin kv (most probably 5.3+ version)

Using bash that has the loadable builtin kv (most probably 5.3+ version)

Source Link
Jetchisel
  • 1.6k
  • 9
  • 12

If your bash has the loadable builtin kv (most probably 5.3+ version)

#!/usr/bin/env bash

shopt -s nullglob
enable kv || exit

kv -A assoc -s . -d '' < <(
  printf '%s\0' *.rar
)

for i in "${!assoc[@]}"; do
  printf '%s.%s\n' "$i" "${assoc["$i"]}"
done

Output

XYcUpv7b1ZpcczFT5y7Uc9mQTvAf88kl.part2.rar
DaHs0QJnJbt.part4.rar
n5oTzoLvG.part6.rar
W1Pn8SHf7pbMSf1u99C4f.part2.rar
okgbuh8VUxSguDNra9uMTtDlXhiLWQmY.part2.rar
T7yvBIqHK82qDCNTtz9iuvp2NhQ.part11.rar
RSmWMPb0vWr8LIEFtR7o.part5.rar
1yBWVnZCx8CoPrGIG.part23.rar

According to help kv

    kv: kv [-A ARRAYNAME] [-s SEPARATORS] [-d RS]
    Read key-value pairs into an associative array.
    
    Read delimiter-terminated records composed of a single key-value pair
    from the standard input and add the key and corresponding value
    to the associative array ARRAYNAME. The key and value are separated
    by a sequence of one or more characters in SEPARATORS. Records are
    terminated by the first character of RS, similar to the read and
    mapfile builtins.
    
    If SEPARATORS is not supplied, $IFS is used to separate the keys
    and values. If RS is not supplied, newlines terminate records.
    If ARRAYNAME is not supplied, "KV" is the default array name.
    
    Returns success if at least one key-value pair is stored in ARRAYNAME.

If sorted output is needed (from the OP's output), then one more loadable builtin named asort from: https://cgit.git.savannah.gnu.org/cgit/bash.git/plain/examples/loadables/asort.c

Something like:

#!/usr/bin/env bash

shopt -s nullglob
enable kv || exit
enable asort || exit

kv -A assoc -s . -d '' < <(
  printf '%s\0' *.rar
)

keys=("${!assoc[@]}")
asort keys

for i in "${keys[@]}"; do
  printf '%s.%s\n' "$i" "${assoc["$i"]}"
done

Output

1yBWVnZCx8CoPrGIG.part23.rar
DaHs0QJnJbt.part4.rar
n5oTzoLvG.part6.rar
okgbuh8VUxSguDNra9uMTtDlXhiLWQmY.part2.rar
RSmWMPb0vWr8LIEFtR7o.part5.rar
T7yvBIqHK82qDCNTtz9iuvp2NhQ.part11.rar
tBJDjsyJtFpY0d3aQ.part6.rar
W1Pn8SHf7pbMSf1u99C4f.part2.rar
XYcUpv7b1ZpcczFT5y7Uc9mQTvAf88kl.part2.rar

According to help asort

asort: asort [-nr] array ...  or  asort [-nr] -i dest source
    Sort arrays in-place.
    
    Options:
      -n  compare according to string numerical value
      -r  reverse the result of comparisons
      -i  sort using indices/keys
    
    If -i is supplied, SOURCE is not sorted in-place, but the indices (or keys
    if associative) of SOURCE, after sorting it by its values, are placed as
    values in the indexed array DEST
    
    Associative arrays may not be sorted in-place.
    
    Exit status:
    Return value is zero unless an error happened (like invalid variable name
    or readonly array).