【问题标题】:Shell - What does this command do?Shell - 这个命令有什么作用?
【发布时间】:2013-11-19 07:01:11
【问题描述】:

任何人都可以向我解释这个命令行的作用:

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

我想知道为什么这个命令末尾有分号。

非常感谢!

【问题讨论】:

标签: regex shell command-line find executable


【解决方案1】:

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

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

【讨论】:

  • 拜托,你能给我更多的细节吗?例如“-type f -exec”以及“{}”和“\;”到底?再次感谢!
  • @Farah:添加了一些解释,请在man find阅读更多关于find命令的内容
  • @Farah:你应该考虑在这里接受一个答案。
【解决方案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。谢谢你的答案和我选择的一样好。但我不得不选择一个。再次感谢。
【解决方案3】:

末尾的分号是 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.

【讨论】:

    猜你喜欢
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多