Help with counting results

kaye

New Member
Hello Friends!

New member here. Please let me know if this is not the appropriate forum for my question.

I have a directory with many .dbf files. For whatever reason, some extensions are all-caps (.DBF), and some are not (.dbf) .

I use this line to find all files with ".dbf" extension:

sudo find . -name "*dbf*"

I use this next line to count the number of results, that is, the number of files with ".dbf" extension.

sudo find . -name "*dbf*" | wc -l

Is it possible to join the two commands together such that I would see all the files with ".dbf" extension and also see right away how many they are.

For example, if there are 5 files with ".dbf" extension, I would see something like this:

./aaa.dbf
./bbb.dbf
./ccc.dbf
./ddd.dbf
./eee.dbf
5

Thank you!
 


dos2unix

Active Member
sudo find . -name "*dbf*" | wc -l

This doesn't do that?

I know... ls *.dbf | wc -l ... does do this.
 

JasKinasis

Well-Known Member
sudo find . -name "*dbf*" | wc -l

This doesn't do that?

I know... ls *.dbf | wc -l ... does do this.
No, the output from both of those commands will only display the output from the wc command. In both cases the output from ls and find does not get output to stdout - it gets piped as input to the wc command.

So the OP will only see the number of files found. But they want to see a list of the files found AND the number of files found.

I might be overthinking things, but in this case, I think the OP might need to use a temporary file by piping the output from find via tee.
That way it will go to the file and stdout.
Then they can count the number of lines in the file and delete the temporary file when they're finished!
e.g.
Bash:
find ./ -type f -iname "*.dbf" -print | tee tmp.txt && wc -l tmp.txt | sed "s/tmp.txt//" && rm tmp.txt
It's a little convoluted. There may be a simpler way of doing it. But I can't think of a better solution offhand.
The sed command is in there to remove tmp.txt from the output of wc.
In other words - after using sed - the output from wc will just be the count. Not the count and the name of the temporary file.
 
Last edited:

JasKinasis

Well-Known Member
Well, he could just
find ./ -type f -iname "*.dbf" && find ./ -type f -iname "*.dbf" | wc -l
True - Running find twice is an option too!
But if the search is long and contains a lot of files - you're looking at a long execution time.

So your solution has less characters to type, but mine will finish faster! :D
 

Members online


Top