I am currently not on a Linux machine. Would the following command produce my wanted result?
ls | grep ".pdf" | wc
No, ".pdf" macthes way too much, e.g
Furthermore, even if you don't have files wrongly matching,
wc without options outputs the number of lines, words and bytes in the input, so you would get two numbers more than you're interested in. If you want
grep in the mix, you could do
ls | grep -E "\.pdf$" | wc -l, but unless you have a lot of pdf-files in that directory grep isn't needed, you can get by with
ls *.pdf | wc -l.
Added later, when I got to think this through:
grep can do the counting so if
grep is in the mix, just add the
-c option to grep instead of piping the output to
wc -l, so
ls | grep -cE "\.pdf$" or if you want to avoid counting directories named
ls -l | grep -cE "^-.*\.pdf$".
Your shell should be able to do the filtering:
ls *.pdf | wc -l
or you have to make sure you match the end of filenames:
ls | grep "*\.pdf$" | wc -l
(notice the dollar sign).
Note: both of these will also match directories ending in ".pdf", if any.
ls should behave as if you gave it option
-1 as soon as you pipe its output. Otherwise, add this switch.
You should use
ls | grep ".pdf" | wc -l
-l parameter will count only the number of resulted lines, while without the
-l you would get other counts as well, like newline, word, and byte count. Note that this will count filenames (and folders as well) which contain the ".pdf" chain of characters.
To count only files ending with
find . -type f -name "*.pdf" | wc -l