Linux 使用 find 和 -exec {},有没有办法计算总数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7719785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:36:14  来源:igfitidea点击:

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

linuxbashunixfind

提问by ethanpil

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?

虽然我没有使用 chmod,但我使用的是不同的命令,我不会在这里列出。此命令工作正常。但是,要操作的文件和目录数以千计,并且此操作需要一些时间。我想知道是否有办法在操作完成时显示某种总数?也许是修改文件的数量?

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?

那行得通吗?至少我们可以看到正在发生的事情......有人有更好的建议吗?

采纳答案by David W.

This works:

这有效:

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

You have to include a second -exec /bin/echofor this to work. If the findcommand has no output, then wchas no input to count lines for.

你必须包括第二个-exec /bin/echo才能工作。如果find命令没有输出,则wc没有输入来计算行数。

回答by Tim

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

您可以-exec使用单个 find 命令链接多个命令。其语法是:

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, andstill 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.

或者,您可以使用tee,这将允许您将输出写入日志文件,并且仍然输出到屏幕。我觉得这很有用,因为屏幕上的连续流输出让我知道命令仍在运行(没有崩溃或挂起),而且我还有日志文件供以后参考。

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 '+'

回答by ott--

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.

使用 -exec 选项 find 将为找到的每个文件启动一个子进程。您可以通过使用 xargs 来加快速度,例如find . -name '*.php' | xargs chmod 755- chmod 仅启动一次。

回答by James Sumners

You could use xargsand pv. Possibly:

您可以使用xargspv。可能:

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.

注意:这仅在您的 *.php 文件的路径或名称中没有任何空格或其他奇数字符时才有效。

回答by user unknown

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.

如果您使用 + 而不是“;”,find 将尝试并行处理许多文件上的 chmod 755。

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

您可以在第一个命令之后执行其他命令,例如打印一个点,并计算最后的点数。