Ubuntu – Count pattern instances using grep

command linegrep

I'm trying to count / in a certain path, but grep counts all instances as 1 when it is in 1 line.

/home/usr/bin/test | grep / -c 

gives an answer 1.

Best Answer

Your command would actually count the number of lines containing / in the standard output of command /home/usr/bin/test

Here are some options to count the instances of / in the string /home/usr/bin/test:

grep -o '/' <<< "/home/usr/bin/test" | wc -l

tr -dc '/' <<< "/home/usr/bin/test" | wc -c