开发者

Looping through alphabets in Bash

开发者 https://www.devze.com 2023-04-02 03:01 出处:网络
I want to mv all the files starting with \'x\' to directory \'x\'; something like: mv path1/x*.ext path2/x

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, ...,开发者_JAVA百科 z

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


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


This should get you started:

for letter in {a..z} ; do
  echo $letter
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 alphabets in Bash

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消