Bash find:更改 -exec 中使用的匹配名称

发布于 2024-12-15 07:23:27 字数 428 浏览 0 评论 0原文

我正在编写一个部署脚本,并且需要对目录中的所有 .less 文件运行 less 编译器。使用以下 find 命令很容易做到这一点:

find -name "*.less" -exec plessc {} {}.css \;

在包含名为 main.less 的文件的文件夹上运行此命令后,我留下了一个名为 main.less.css< 的文件/code>,但我希望它是 main.css

我知道我可以使用以下命令轻松删除生成文件的 .less 部分: rename 's/\.less//' *.css 但我希望学习一些有关使用的新知识 -执行。

在 -exec 参数中使用时是否可以修改匹配的文件名?

谢谢!

I'm writing a deploy script, and I need to run a less compiler against all .less files in a directory. This is easy to do with the following find command:

find -name "*.less" -exec plessc {} {}.css \;

After running this command on a folder with a file named main.less, I'm left with a file named main.less.css, but I want it to be main.css.

I know I can easily strip the .less portion of the resulting files with this command: rename 's/\.less//' *.css but I'm hoping to learn something new about using -exec.

Is it possible to modify the name of the file that matches while using it in the -exec parameter?

Thanks!

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

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

发布评论

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

评论(2

小鸟爱天空丶 2024-12-22 07:23:27

您的 find 命令使用了几个非标准 GNU 扩展:

  • 您没有说明在哪里查找,这是 POSIX 中的错误,但在这种情况下 GNU find 选择当前目录
  • 您使用非隔离的 {},POSIX find 不会在这种情况下扩展它。

下面是一个单行代码,应该适用于大多数查找实现并解决双扩展问题:

find . -name "*.less" -exec sh -c "plessc \$0 \$(dirname \$0)/\$(basename \$0 less)css" {} \;

在 Solaris 10 及更早版本上,如果满足以下条件,sh -c 应替换为 ksh -c: PATH 不符合 POSIX 标准。

Your find command is using a couple of non standard GNU extensions:

  • You do not state where to find, this is an error in POSIX but GNU find select the current directory in that case
  • You use a non isolated {}, POSIX find doesn't expand it in that case.

Here is a one liner that should work with most find implementations and fix your double extension issue:

find . -name "*.less" -exec sh -c "plessc \$0 \$(dirname \$0)/\$(basename \$0 less)css" {} \;

On Solaris 10 and older, sh -c should be replaced by ksh -c if the PATH isn't POSIX compliant.

甜宝宝 2024-12-22 07:23:27

不,不可能直接这样做。您只能使用 {} 直接插入完整文件名。但是,在 exec 中,您可以放入其他内容,例如 awk。或者您可以通过管道将输出重定向到另一个程序。

find man 页面:

   -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 arguments
          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.

No, it is not possible to do it directly. You can only use {} to directly insert the full filename. However, in exec, you COULD put in other things like awk. Or you can redirect output to another program via pipes.

From the find man page:

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