I have a folder /mqlocal/mqm. inside that there are 2 text files (bindu.txt,sujitha.txt). I need code to count the number of text files under the directory. The result should be an integer.
Asked
Active
Viewed 3,774 times
-1
-
Is the result always going to be zero, one or two depending on whether those two files exist or not, or may there be other files that you also want to count? – Kusalananda Oct 01 '19 at 08:36
2 Answers
0
You can try this :
find /mqlocal/mqm -type f -iname "*.txt" | wc -l
-type f
: It is for finding files. It will ignore the directories inside /mqlocal/mqm
directory.
-iname
: It will match only files ending with .txt
. -i
is for Case insensitive.
wc -l
: It will count the number of files.

Pacifist
- 5,787
-
3No,
wc -l
will count the number of lines outputted byfind
. To make this the number of files, use-exec echo x \;
to output a line for each file. If you don't, you would count filenames containing newlines as multiple files. – Kusalananda Oct 01 '19 at 08:43
-1
find *.txt | wc -l
change the present working directory to /mqlocal/mqm, and then execute the command.
It will gives you the count of the text files in that directory.

Bhargav Mandava
- 7
- 3
-
Try that with a file that you create with
touch $'file\nname.txt'
in an otherwise empty directory. – Kusalananda Oct 01 '19 at 08:42