I have a directory with the following content:
$ mkdir dir && cd "$_"
~/dir $ mkdir a1 a2 a3 a4 b1 c1 c2 1a 2a 2b 2c 2d 3a _1 _2
~/dir $ touch a_1 a_2 a_3 a_4 b_1 c_1 c_2 1_a 2_a 2_b 2_c 2_d 3_a __1 __2
~/dir $ ls
__1 1_a __2 2_a 2_b 2_c 2_d 3_a a_1 a_2 a_3 a_4 b_1 c_1 c_2
_1 1a _2 2a 2b 2c 2d 3a a1 a2 a3 a4 b1 c1 c2
Now I want to group all these files and directories based on their first letter and move them to directories of the same letter. So the output would be:
~/dir $ ls
_ 1 2 3 a b c
And using exa, the tree would look something like this:
~/dir $ exa --tree
.
├── 1
│ ├── 1_a
│ └── 1a
├── 2
│ ├── 2_a
│ ├── 2_b
│ ├── 2_c
│ ├── 2_d
│ ├── 2a
│ ├── 2b
│ ├── 2c
│ └── 2d
├── 3
│ ├── 3_a
│ └── 3a
├── _
│ ├── _1
│ ├── _2
│ ├── __1
│ └── __2
├── a
│ ├── a1
│ ├── a2
│ ├── a3
│ ├── a4
│ ├── a_1
│ ├── a_2
│ ├── a_3
│ └── a_4
├── b
│ ├── b1
│ └── b_1
└── c
├── c1
├── c2
├── c_1
└── c_2
I know I can move using wildcards:
~/dir $ mkdir a && mv a* a
Which throws an error:
mkdir: cannot create directory ‘a’: File exists
But it gets the job done. And I can do something like this to avoid the error:
~/dir $ mkdir temp && mv a* temp && mv temp a
And then I could use that in a for loop for every letter that I know. But the problem is that I don't know what those first letters could possibly be, we have quite a lot of letters. Is there a way I can achieve this without the need to know those letters?