【问题标题】:Unix command to list all the files and directories in the current directory whose second character is a digitUnix命令列出当前目录中第二个字符为数字的所有文件和目录
【发布时间】:2020-07-01 01:14:11
【问题描述】:

我的代码是 ls | grep .[0-9]*

输出显示为

d2
d4
di3
dir1
f1
f2
fil4
file3
g2t
g3t

预期的输出是

d2
d4
f1
f2
g2t
g3t

我知道我可以直接使用ls ?[0-9],但是我的输出顺序不同

f1  f2  g2t  g3t

d2:

d4:

【问题讨论】:

  • ls | grep .[0-9].*
  • 如果你要将ls 传递给grep(你不应该这样做),你需要锚定模式。例如grep '^.[0-9]' 但是这有很多问题。使用find

标签: unix grep pipe ls filepattern


【解决方案1】:

好的,这行得通 ls -d ?[0-9]*

【讨论】:

    【解决方案2】:

    此查找命令列出当前目录中名称中第二个字符为数字的文件和目录

    find . -maxdepth 1 -name '?[0-9]*'
    

    【讨论】:

      【解决方案3】:

      试试这个,会成功的

      ls -1 | grep '^.[0-9][.]*'
      

      【讨论】:

      • 你能解释一下你的答案吗?
      猜你喜欢
      • 2016-07-04
      • 2014-04-16
      • 2015-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 2012-09-02
      相关资源
      最近更新 更多