0

I'm new to bash, and coding. I have a list of files:

test-T01___2022.txt
test-T01__2021.txt
test-T01_NONE.txt
test-T02___2022.txt
test-T02__2021.txt
test-T02_NONE.txt
test-T03___2022.txt
test-T03__2021.txt
test-T03_NONE.txt

I'm trying to write a script to create folders T01 (containing *T01 files), T02 (containing all files with T02), etc. I'm trying with wildcards and regexps and something similar to this post but having some trouble. I appreciate some help.

Many thanks!

shellter
  • 36,525
  • 7
  • 83
  • 90
HwiNoree27
  • 11
  • 2

1 Answers1

0

Use the bash prefix and suffix removal operations. See the link in the comments for more details.

For example:

files=...
for file in $files
do
    a=${file#test-}
    dir=${a%%_*}
    mkdir "$dir"
    mv "$file" "$dir"
done
rghome
  • 8,529
  • 8
  • 43
  • 62