如何在unix中找到忽略区分大小写的文件名

Poo*_*a25 3 unix awk grep ls sed

在我的情况下,有可能获得像abb_Timestamp.csv或ABC_TIMESTAMP.CSV这样的文件.我在用ls -l | grep ABC* | wc -l.无论如何,我都想要文件数.即使我收到CAPITAL Letter中的文件,我也应该得到Count,即使我收到小写字母,我也应该得到unix中的计数.请建议.

Lev*_*sky 6

解析输出ls被认为是不好的做法.你可以使用find:

find . -iname 'ABC*' | wc -l
Run Code Online (Sandbox Code Playgroud)

man find:

  -iname pattern
          Like -name, but the match is case insensitive.  For example, the
          patterns `fo*' and `F??' match  the  file  names  `Foo',  `FOO',
          `foo',  `fOo',  etc.   In these patterns, unlike filename expan?
          sion by the shell, an initial '.' can be matched by  `*'.   That
          is, find -name *bar will match the file `.foobar'.   Please note
          that you should quote patterns as a matter of course,  otherwise
          the shell will expand any wildcard characters in them.
Run Code Online (Sandbox Code Playgroud)

正如Johnsyweb在评论中指出的那样find,默认会将其递归到子目录中.为避免这种情况,您可以提供-maxdepth 1:

   -maxdepth levels
          Descend at most levels (a non-negative integer) levels of direc?
          tories below the command line arguments.  -maxdepth 0
           means only apply the tests and  actions  to  the  command  line
          arguments.
Run Code Online (Sandbox Code Playgroud)

  • 注意:将`-maxdepth 1`提供给`find`否则会将其递归到子目录中,其中问题中的命令不会. (2认同)