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

后端 未结 5 644
无人共我
无人共我 2020-12-29 18:48

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 differ

相关标签:
5条回答
  • 2020-12-29 19:31

    With the -exec option find will start a subprocess for each file found. You could speed this up by using xargs like find . -name '*.php' | xargs chmod 755 - chmod is started only once.

    0 讨论(0)
  • 2020-12-29 19:34
    find . -name "*.php" -exec chmod 755 {} + -printf '.' | wc -c
    

    If you use + instead of ";", find will try to process chmod 755 on many files in parallel.

    You can perform additional commands after the first one, here, for example print a dot, and count the dots in the end.

    0 讨论(0)
  • 2020-12-29 19:35

    You could use xargs and pv. Possibly:

    find . -name "*.php" | pv --line-mode | xargs chmod 755
    

    Note: this is only going to work if your *.php files do not have any spaces or other odd characters in the path or name.

    0 讨论(0)
  • 2020-12-29 19:53

    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 '+'
    
    0 讨论(0)
  • 2020-12-29 19:54

    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.

    0 讨论(0)
提交回复
热议问题