Search code examples
unixsedawkgrepls

how to find file name ignoring case sensitivity in unix


In my case there is a possibility of getting files like abb_Timestamp.csv or ABC_TIMESTAMP.CSV. I am using ls -l | grep ABC* | wc -l. I want the count of files irrespective of the case. even If I get files in CAPITAL Letter I should get the Count and even if I get in Small letters I should get the count in unix. Please Suggest.


Solution

  • Parsing the output of ls is considered bad practice. You can use find:

    find . -iname 'ABC*' | wc -l
    

    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.
    

    As Johnsyweb notes in the comment, find will recurse into subdirectories by default. To avoid that, you can supply -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.