How to print result of a command as well as the count of results?

604 Views Asked by At

I have to print all files containing a query given as a console argument, the following line does so

find . "$path" -type f -name "*$key*" -print

however I would also like to get the total file count, but doing this:

find . "$path" -type f -name "*$key*" -print | wc -l

will give me the count but not the names of those files, which isn't a desired result. How to fix it with a one liner (if possible)?

1

There are 1 best solutions below

2
On BEST ANSWER

With the command tee duplicating the output of the pipe and Bash‘s Process Substitution as a placeholder for the file to provide the duplicated stream to wc:

$ seq 11 15 | tee >(wc -l)
11
12
13
14
15
5