Shell - 这个命令有什么作用?

时间:2013-11-07 15:02:49

标签: regex shell command-line find executable

任何机构都可以向我解释这个命令行的用途:

find "$dir1" -regex ".*\.exe" -type f -exec cp "{}" "$dir2/my_executable.exe" \;

我想知道为什么这个命令最后有分号。

非常感谢!

3 个答案:

答案 0 :(得分:2)

$dir中查找包含.exe扩展名的文件并将其复制到$dir2/my_executable.exe,以便$dir2/my_executable.exe最终成为找到的最后一个文件。

解释

  • find "$dir1"$dir1
  • 中查找文件
  • -regex ".*\.exe"名称为XXX.exe
  • -type f是文件
  • -exec .... {} \;在找到的文件中执行命令。
  • cp "{}" "$dir2/my_executable.exe" \;将找到的文件复制到"$dir2/my_executable.exe"。因为它始终是相同的,所以在"$dir2/my_executable.exe"中,您最终会找到最后一个文件。

答案 1 :(得分:2)

这是在名为*.exe的目录中查找所有$dir1个文件。然后每次复制名称为$dir2/my_executable.exe的每个文件都会覆盖它。因此,最后$dir2/my_executable.exe将与.exe目录中最后找到的$dir文件相同。

  1. -type f =>仅查找文件
  2. -regex ".*\.exe" =>查找名称中包含.exe的文件
  3. -exec =>对每个找到的文件执行命令
  4. {} =>表示找到的带路径的文件名
  5. cp "{}" "$dir2/my_executable.exe" =>将找到的文件复制到$dir2/my_executable.exe 和\;终止exec语句

答案 2 :(得分:1)

末尾的分号是find命令中-exec选项语法的一部分:

   -exec command ;
          Execute command; true if 0 status is returned.  All following arguments to find are taken to be arguments
          to the command until an argument consisting of `;' is encountered.  The string `{}' is  replaced  by  the
          current file name being processed everywhere it occurs in the arguments to the command, not just in argu‐
          ments where it is alone, as in some versions of find.  Both of  these  constructions  might  need  to  be
          escaped (with a `\') or quoted to protect them from expansion by the shell.  See the EXAMPLES section for
          examples of the use of the -exec option.  The specified command is run once for each matched  file.   The
          command  is executed in the starting directory.   There are unavoidable security problems surrounding use
          of the -exec action; you should use the -execdir option instead.
相关问题