I am just beginning to learn how to use Unix and its commands. Assuming my home directory is named LearnCS, I want to write out the command to count how many files I have in my home directory that end with the extension *.txt with the resultant answer being a single number.
I believe I need to use grep and piping, but I'm not exactly sure...something like:
grep LearnCS *.txt | wc -l
Maybe? Any suggestions would be greatly appreciated!
Actually, what you have their will look into each file that matches "*.txt" and search for "LearnCS".
Instead, what you want to do is take a listing, then pipe it through grep for your pattern, then pipe that through wc, like so:
-
ls /home/LearnCS | grep ".txt" | wc -l
-
You don't need the * ".txt" because it isn't doing any type of globbing, instead, it is just a pattern to be looked for. I am only doing an "ls" and not an "ls -l" because I don't want the full listing, just the file names in the directory.
Just as a help to you, here are some links to some good sites for Unix commands and learning Unix. There are many, but these are some that I found:
Unix Command Reference Card Intro to Unix Commands Unix Tutorial For Beginners
Happy learning!
Regards,
Jeff