Search code examples
linuxbashshellteeprocess-substitution

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


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)?


Solution

  • 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