谨慎使用 Linux find 命令
当使用 Linux 下的 find 命令时,请使用 -ok 选项来避免文件被意外删除,这个选项会在移除任何文件之前都会请求你的许可。
最近有朋友提醒我有一个有用的选项来更加谨慎地运行 find
命令,它就是 -ok
。除了一个重要的区别之外,它的工作方式与 -exec
相似,它使 find
命令在执行指定的操作之前请求权限。
这有一个例子。如果你使用 find
命令查找文件并删除它们,你可能使用的是下面的命令:
<span class="pln">$ </span><span class="kwd">find</span><span class="pun">.</span><span class="pun">-</span><span class="pln">name runme </span><span class="pun">-</span><span class="kwd">exec</span><span class="kwd">rm</span><span class="pun">{}</span><span class="pln"> \;</span>
在当前目录及其子目录中中任何名为 “runme” 的文件都将被立即删除 —— 当然,你要有权限删除它们。改用 -ok
选项,你会看到类似这样的东西,但 find
命令将在删除文件之前会请求权限。回答 y
代表 “yes” 将允许 find
命令继续并逐个删除文件。
<span class="pln">$ </span><span class="kwd">find</span><span class="pun">.</span><span class="pun">-</span><span class="pln">name runme </span><span class="pun">-</span><span class="pln">ok </span><span class="kwd">rm</span><span class="pun">{}</span><span class="pln"> \;</span>
<span class="pun"><</span><span class="kwd">rm</span><span class="pun">...</span><span class="pun">./</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">runme </span><span class="pun">></span><span class="pun">?</span>
-execdir 命令也是一个选择
另一个可以用来修改 find
命令行为,并可能使其更可控的选项是 -execdir
。-exec
会运行指定的任何命令,而 -execdir 则从文件所在的目录运行指定的命令,而不是在运行
find` 命令的目录运行指定的命令。这是两个它的例子:
<span class="pln">$ </span><span class="kwd">pwd</span>
<span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">shs</span>
<span class="pln">$ </span><span class="kwd">find</span><span class="pun">.</span><span class="pun">-</span><span class="pln">name runme </span><span class="pun">-</span><span class="pln">execdir </span><span class="kwd">pwd</span><span class="pln"> \;</span>
<span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">shs</span><span class="pun">/</span><span class="pln">bin</span>
<span class="pln">$ </span><span class="kwd">find</span><span class="pun">.</span><span class="pun">-</span><span class="pln">name runme </span><span class="pun">-</span><span class="pln">execdir </span><span class="kwd">ls</span><span class="pln"> \;</span>
<span class="kwd">ls</span><span class="kwd">rm</span><span class="pln"> runme</span>
到现在为止还挺好。但要记住的是,-execdir
也会在匹配文件的目录中执行该命令。如果运行下面的命令,并且目录包含一个名为 “ls” 的文件,那么即使该文件没有执行权限,它也将运行该文件。使用 -exec
或 -execdir
类似于通过 source
来运行命令。
<span class="pln">$ </span><span class="kwd">find</span><span class="pun">.</span><span class="pun">-</span><span class="pln">name runme </span><span class="pun">-</span><span class="pln">execdir </span><span class="kwd">ls</span><span class="pln"> \;</span>
<span class="typ">Running</span><span class="pln"> the </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">shs</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="kwd">ls</span><span class="kwd">file</span>
<span class="pln">$ </span><span class="kwd">find</span><span class="pun">.</span><span class="pun">-</span><span class="pln">name runme </span><span class="pun">-</span><span class="pln">execdir </span><span class="kwd">rm</span><span class="pun">{}</span><span class="pln"> \;</span>
<span class="typ">This</span><span class="kwd">is</span><span class="pln"> an imposter </span><span class="kwd">rm</span><span class="pln"> command</span>
<span class="pln">$ </span><span class="kwd">ls</span><span class="pun">-</span><span class="pln">l bin</span>
<span class="pln">total </span><span class="lit">12</span>
<span class="pun">-</span><span class="pln">r</span><span class="pun">-</span><span class="pln">x</span><span class="pun">------</span><span class="lit">1</span><span class="pln"> shs shs </span><span class="lit">25</span><span class="typ">Oct</span><span class="lit">13</span><span class="lit">18</span><span class="pun">:</span><span class="lit">12</span><span class="kwd">ls</span>
<span class="pun">-</span><span class="pln">rwxr</span><span class="pun">-</span><span class="pln">x</span><span class="pun">---</span><span class="lit">1</span><span class="pln"> shs shs </span><span class="lit">36</span><span class="typ">Oct</span><span class="lit">13</span><span class="lit">18</span><span class="pun">:</span><span class="lit">29</span><span class="kwd">rm</span>
<span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">r</span><span class="pun">--</span><span class="lit">1</span><span class="pln"> shs shs </span><span class="lit">28</span><span class="typ">Oct</span><span class="lit">13</span><span class="lit">18</span><span class="pun">:</span><span class="lit">55</span><span class="pln"> runme</span>
<span class="pln">$ </span><span class="kwd">cat</span><span class="pln"> bin</span><span class="pun">/</span><span class="kwd">ls</span>
<span class="kwd">echo</span><span class="typ">Running</span><span class="pln"> the $0 </span><span class="kwd">file</span>
<span class="pln">$ </span><span class="kwd">cat</span><span class="pln"> bin</span><span class="pun">/</span><span class="kwd">rm</span>
<span class="kwd">echo</span><span class="typ">This</span><span class="kwd">is</span><span class="pln"> an imposter </span><span class="kwd">rm</span><span class="pln"> command</span>
-okdir 选项也会请求权限
要更谨慎,可以使用 -okdir
选项。类似 -ok
,该选项将请求权限来运行该命令。
<span class="pln">$ </span><span class="kwd">find</span><span class="pun">.</span><span class="pun">-</span><span class="pln">name runme </span><span class="pun">-</span><span class="pln">okdir </span><span class="kwd">rm</span><span class="pun">{}</span><span class="pln"> \;</span>
<span class="pun"><</span><span class="kwd">rm</span><span class="pun">...</span><span class="pun">./</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">runme </span><span class="pun">></span><span class="pun">?</span>
你也可以小心地指定你想用的命令的完整路径,以避免像上面那样的冒牌命令出现的任何问题。
<span class="pln">$ </span><span class="kwd">find</span><span class="pun">.</span><span class="pun">-</span><span class="pln">name runme </span><span class="pun">-</span><span class="pln">execdir </span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="kwd">rm</span><span class="pun">{}</span><span class="pln"> \;</span>
find
命令除了默认打印之外还有很多选项,有些可以使你的文件搜索更精确,但谨慎一点总是好的。