Question

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.

Was it helpful?

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.

OTHER TIPS

man grep:

$ echo "FOO" | grep foo

$ echo "FOO" | grep -i foo
FOO
$ echo "FOO" | grep -c -i foo
1

Using awk. Search all files with csv/CSV and count hits, with one awk.

ls -l  | awk 'tolower($9)~"csv" {a++} END {print a}'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top