在 Linux 中使用 -exec 与使用 xargs 和locate 命令比较

发布于 2025-01-14 06:47:58 字数 1058 浏览 2 评论 0原文

关于我试图理解的 -exec {} 和 xargs 之间的区别,这是一个有点神秘的地方。我将不胜感激任何对此的指导。 作为背景,我刚刚了解了“locate”命令,并且使用此命令成功创建了网络驱动器的数据库 ~/.u.db(顺便说一句,这是在 Windows WSL 中):

$ updatedb -l 0 -o ~/.u.db -U /mnt/u

我正在尝试用它来查找当前工作目录中的所有 .m 文件并显示详细的文件信息。通常我使用这样的“find”命令:

$ find . -type f -name '*.m' -exec ls -l \{\} \;

它成功地为我提供了 .m 文件的列表,其文件属性如下:

{all the file info} ./{relative path}/filename1.m
{all the file info} ./{relative path}/filename2.m

当我尝试以类似的方式运行locate 命令时,我收到错误。

$ locate -d ~/.u.db "$PWD*/*.m" -exec -ls -l \{\} \;
locate: invalid option -- 'x'

仅供参考,这工作正常(没有我正在寻找的额外文件信息):

$ locate -d ~/.u.db "$PWD*/*.m"

/{absolute path}/filename1.m
/{absolute path}/filename2.m

我发现我可以像这样成功地使用 xargs:

$ locate -d ~/.u.db "$PWD*/*.m" | xargs ls -l

{all the file info} /{absolute path}/filename1.m
{all the file info} /{absolute path}/filename2.m

我只是想理解为什么在这种情况下我应该使用 xargs 而不是 -执行{}。我将不胜感激对行为差异的任何见解。

This is a little mystery regarding the difference between -exec {} and xargs that I'm trying to understand. I would appreciate any guidance on this.
As background, I've just learned about the "locate" command and I successfully created a database ~/.u.db of a network drive using this command (this is in Windows WSL, by the way):

$ updatedb -l 0 -o ~/.u.db -U /mnt/u

I'm trying to use it to find all .m files from my current working directory and display the detailed file information. Normally I use the "find" command like this:

$ find . -type f -name '*.m' -exec ls -l \{\} \;

which successfully gives me a list of .m files with their file properties like this:

{all the file info} ./{relative path}/filename1.m
{all the file info} ./{relative path}/filename2.m

when I try to run the locate command in a similar manner I get an error.

$ locate -d ~/.u.db "$PWD*/*.m" -exec -ls -l \{\} \;
locate: invalid option -- 'x'

Just for reference, this works properly (without the extra file info that I'm looking for):

$ locate -d ~/.u.db "$PWD*/*.m"

/{absolute path}/filename1.m
/{absolute path}/filename2.m

I found that I can use xargs like this successfully:

$ locate -d ~/.u.db "$PWD*/*.m" | xargs ls -l

{all the file info} /{absolute path}/filename1.m
{all the file info} /{absolute path}/filename2.m

I'm just trying to understand why I should use xargs in this case and not -exec {}. I would appreciate any insight into the difference in behavior.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是你 2025-01-21 06:47:58

我不确定这有多大差异,但在我手头的 Linux 上(都运行 Debian 衍生版本),locate 命令没有 -exec开关。它们都使用 GNU 版本的 locate,它遵循 GNU 约定,即长选项用 -- 引入,短选项用 - 引入,并且如果短选项不带参数,则可以组合它们。换句话说,-exec 等同于 -e -x -e -c,这就是它抱怨无效 -x 开关的原因。

I’m not sure how much this varies, but on the Linuxes I have at hand — both running Debian derivatives — the locate command does not have an -exec switch. They all use the GNU version of locate, which follows the GNU convention that long options are introduced with -- and short options with -, and that short options can be combined if they don’t take arguments. In other words, -exec is eqivalent to -e -x -e -c, which is why it complains about an invalid -x switch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文