This question already has an answer here:

I want to count number of files in current directory that contain the phrase "HW".

I tried

ls -l | grep HW | wc -l

marked as duplicate by don_crissti, Jenny D, Jakuje, terdon bash Jan 8 '16 at 18:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • HW is a string inside the file, or in the filename? – Kira Jan 8 '16 at 18:02

To count how many files have in it the string "HW", the following will do:

find . -maxdepth 1 -type f | xargs grep -m 1 HW | wc -l

If your directory has only regular files:

grep -m 1 HW * 2>/dev/null | wc -l

If you are looking for filenames that have the string "HW" in it:

find . -type f -name "*HW*" | wc -l
  • Is there any other way instead of using find ? – Out Of Bounds Jan 8 '16 at 18:11
  • It there a problem in using find, or that is just for the sake of curiosity? – Kira Jan 8 '16 at 18:12
  • find doesn't work. it gives me a list of every file in the directory. – Out Of Bounds Jan 8 '16 at 18:28
  • That is the idea, so you can pipe it to grep. You could use grep -m 1 HW * if your directory contains only regular files. – Kira Jan 8 '16 at 18:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.