linux数字与字母扩展排序

关于字母扩展排序

1. 花括号 { }

  • 按照ASCII编码顺序排序
  • 可倒序排序
[root@CentOS8/dir]# echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@CentOS8/dir]# echo {A..Z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
[root@CentOS8/dir]# echo {A..z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [  ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z

[root@CentOS8/dir]# echo {W..e}  #任意截取
W X Y Z [  ] ^ _ ` a b c d e
[root@CentOS8/dir]# echo {f..X}  #任意反向截取
f e d c b a ` _ ^ ]  [ Z Y X

2. 中括号 [ ]

不可以倒序排列!!!

  • 空目录的情况

    [root@CentOS8/dir]# ls
    [root@CentOS8/dir]# echo [a-c]
    [a-c]
    
  • 目录有内容的情况

    • 用于通配符,[a-d] = a A b B c C d !!!

      [root@CentOS8/dir]# touch {a..z}
      [root@CentOS8/dir]# ls
      a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z
      [root@CentOS8/dir]# ls [a-d]
      a  b  c  d
      [root@CentOS8/dir]# ls [abcd]
      a  b  c  d
      
      [root@CentOS8/dir]# touch {A..Z}
      [root@CentOS8/dir]# ls
      a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z
      A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z
      [root@CentOS8/dir]# ls [a-d]    #先小后大排序!!!!!!!!!
      a  A  b  B  c  C  d
      [root@CentOS8/dir]# ls [abcd]   #指定字符集
      a  b  c  d
      
      #总结:
      用于通配符时,[a-d]表示匹配字符a  A  b  B  c  C  d中的任何一个
                 [abcd]表示匹配字符a b c d中的任何一个
      
    • 用于正则表达式,[a-d] = a b c d !!!

      [root@CentOS8/dir]# touch {a..g}.log
      [root@CentOS8/dir]# touch {A..G}.log
      [root@CentOS8/dir]# ls
      a.log  b.log  c.log  d.log  e.log  f.log  g.log
      A.log  B.log  C.log  D.log  E.log  F.log  G.log
      [root@CentOS8/dir]# ls | grep "[a-d].log"   #只匹配小写
      a.log
      b.log
      c.log
      d.log
      [root@CentOS8/dir]# ls | grep "[A-E].log"   #只匹配大写
      A.log
      B.log
      C.log
      D.log
      E.log
      

数字排序

{ } 可以倒序排列,[ ] 禁止!

[root@CentOS8/dir]# echo {9..1}
9 8 7 6 5 4 3 2 1

[root@CentOS8/dir]# touch {1..9}
[root@CentOS8/dir]# ls
1  3  5  7  9      A.log  B.log  C.log  D.log  E.log  F.log  G.log
2  4  6  8  a.log  b.log  c.log  d.log  e.log  f.log  g.log
[root@CentOS8/dir]# ls [6-4]
ls: cannot access '[6-4]': No such file or directory  #报错
[root@CentOS8/dir]# ls [4-6]
4  5  6
[root@CentOS8/dir]# ls | grep "[6-2]"
grep: Invalid range end                   #报错
[root@CentOS8/dir]# ls | grep "[2-6]"
2
3
4
5
6
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。