在 Bash 中循环字母表

我希望将以“ x”开头的所有文件 mv到目录“ x”; 类似于:

mv path1/x*.ext path2/x

对所有字母 a,... ,z 都这样做

我怎样才能写一个 bash 脚本,使’x’循环通过字母表?

103069 次浏览

This should get you started:

for letter in {a..z} ; do
echo $letter
done
for x in {a..z}
do
echo "$x"
mkdir -p path2/${x}
mv path1/${x}*.ext path2/${x}
done

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.

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}*

With uppercase as well

for letter in \{\{a..z},{A..Z}}; do
echo $letter
done

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

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.