Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using find with -exec {}, is there a way to count the total?

I am using a command similar to this one:

find . -name "*.php" -exec chmod 755 {} \; 

Although, I am not using chmod, I am using a different command which I will not list here. This command is working fine. However, there are thousands of files and directories to be operated on, and this operation takes some time. I am wondering if there is a way to display some sort of total when the operation is complete? Perhaps a count of modified files?

The only thing I can think of is simply to do something like:

find . -name "*.php" -exec chmod 755; echo "+"; {} \; 

Will that work? At least we can see that something is happening... Anyone have a better suggestion?

like image 632
ethanpil Avatar asked Oct 10 '11 23:10

ethanpil


People also ask

Can a Countif () function count array?

Sum, SumProduct, Frequency, Linest, lookup functions, etc. take both range and array objects. To use countif, you have to use range in cells, defining the array in the formula on the go will not work.

How do you count how many times something appears?

Use the COUNTIF function to count how many times a particular value appears in a range of cells. For more information, see COUNTIF function.


2 Answers

This works:

$ find . -name "*.php" -exec chmod 755 {} \; -exec /bin/echo {} \; | wc -l 

You have to include a second -exec /bin/echo for this to work. If the find command has no output, then wc has no input to count lines for.

like image 169
David W. Avatar answered Sep 23 '22 19:09

David W.


You can chain multiple -exec commands with a single find command. The syntax for that is:

find . -exec cmd1 \; -exec cmd2 \; -exec cmd3 \; 

which in your case would look like this:

find . -name '*.php' -exec chmod 755 {} \; -exec echo '+' \; 

Although you have a few other options for this. You can redirect output to a file:

find . -name '*.php' -exec chmod 755 {} \; > logfile.txt 

Or, you can use tee, which will allow you to write the output to a logfile, and still output to the screen. I find this useful, as the continuously-streamed output to the screen lets me know that the command is still running (not crashed or hung), and I still have the log file to refer to later.

find . -name '*.php' -exec chmod 755 {} \; | tee logfile.txt wc -l logfile.txt           // prints the lines in the file grep -c '^+$' logfile.txt   // prints the lines containing a single '+' 
like image 45
Tim Avatar answered Sep 25 '22 19:09

Tim