101

I want to mv all the files starting with 'x' to directory 'x'; something like:

mv path1/x*.ext path2/x

and do it for all alphabet letters a, ..., z

How can I write a bash script which makes 'x' loops through the alphabet?

Vadim Kotov
  • 7,766
  • 8
  • 46
  • 61
behzad.nouri
  • 69,003
  • 18
  • 120
  • 118

7 Answers7

166
for x in {a..z}
do
    echo "$x"
    mkdir -p path2/${x}
    mv path1/${x}*.ext path2/${x}
done
Foo Bah
  • 24,723
  • 5
  • 52
  • 79
Kamil Dziedzic
  • 4,395
  • 2
  • 27
  • 44
  • 1
    May I ask why you enclose x with braces on 4th and 5th line? – Jiaqi Li Sep 19 '18 at 21:39
  • It's not required here but well, it works, and makes parameters stand out better;) https://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion – Kamil Dziedzic Feb 13 '19 at 18:44
  • 1
    Usually `"$x"` is enough and a better way to make it stand out. – Weijun Zhou Mar 12 '19 at 02:15
  • @WeijunZhou that is a problem if you want to expand variable x followed by the letters 'y' and 'z'. if you try to expand `"$xyz"` the interpreter will think you're trying to print a variable named xyz while `"${x}yz"` will let the interpreter know exactly what you're trying to do – 4dc0 May 25 '21 at 23:45
  • I meant `"$x"yz`. – Weijun Zhou May 26 '21 at 04:37
45

This should get you started:

for letter in {a..z} ; do
  echo $letter
done
Mat
  • 195,986
  • 40
  • 382
  • 396
25

here's how to generate the Spanish alphabet using nested brace expansion

for l in {{a..n},ñ,{o..z}}; do echo $l ; done | nl
1  a
 ...
14  n
15  ñ
16  o
...
27  z

Or simply

echo -e {{a..n},ñ,{o..z}}"\n" | nl

If you want to generate the obsolete 29 characters Spanish alphabet

echo -e {{a..c},ch,{d..l},ll,{m,n},ñ,{o..z}}"\n" | nl

Similar could be done for French alphabet or German alphabet.

LMC
  • 8,575
  • 2
  • 25
  • 45
  • 1
    Ugly alternative to avoid splitting the alphabet: `for j in $(for i in $(echo {a..z} ñ ch ll ); do echo $i; done| sort | xargs); do echo "Hello-$j"; done` – Enrico Carlesso May 04 '21 at 23:18
4

Using rename:

mkdir -p path2/{a..z}
rename 's|path1/([a-z])(.*)|path2/$1/$1$2' path1/{a..z}*

If you want to strip-off the leading [a-z] character from filename, the updated perlexpr would be:

rename 's|path1/([a-z])(.*)|path2/$1/$2' path1/{a..z}*
anishsane
  • 19,124
  • 5
  • 38
  • 71
4

With uppercase as well

for letter in {{a..z},{A..Z}}; do
  echo $letter
done
Thanh Trung
  • 3,326
  • 3
  • 26
  • 38
2

This question and the answers helped me with my problem, partially.
I needed to loupe over a part of the alphabet in bash.

Although the expansion is strictly textual

I found a solution: and made it even more simple:

START=A
STOP=D
for letter in $(eval echo {$START..$STOP}); do
    echo $letter
done

Which results in:

A
B
C
D

Hope its helpful for someone looking for the same problem i had to solve, and ends up here as well

Alphons
  • 161
  • 1
  • 4
0

Looping through alphabet I hope this can help.

for i in {a..z}

for i in {A..Z}

for i in {{a..z},{A..Z}}

use loop according to need.

Ravikant
  • 19
  • 3
  • That is already covered in other answers. Please don't add duplicates, or explain how yours is better than the previous answers. – Robert Nov 23 '21 at 14:09