0

I need to count all the .txt files in the current folder. I tried ls | grep .txt but if my folder content is: a.txt btxt c.c it will select a.txt and btxt and I only want files that end with .txt. I tried various combinations of regexp but with no result.

3 Answers 3

3

Find may be better than in this case since it is designed for handling file names:

find . -maxdepth 0 -name '*.txt' | wc -l 

Buf if you are very cautious about possibly strange file names:

find . -maxdepth 0 -name '*.txt' -exec echo 1 \; | wc -l 
Sign up to request clarification or add additional context in comments.

Comments

1

For Grep, using the character '.' means: "any character"... so you'll need to escape the dot:

ls | grep -e "\.txt" 

edit in fact the -e option is not even necessary. this will do the trick:

ls | grep "\.txt" 

Comments

0

If all you need is number of files with extension '.txt' in current directory only, then this will also help.

ls -l *.txt | wc -l 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.