是否可以将find命令传递给另一个find命令的-exec? [英] Is it possible to pass a find command to -exec of another find command?

查看:76
本文介绍了是否可以将find命令传递给另一个find命令的-exec?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是好奇心的问题.我已经尝试了多种变体,并进行了研究,但似乎无法弄清楚这是否可能.

This is just a question of curiosity. I've tried multiple variants, and did research, but can't seem to figure out if this is possible.

我想在find命令中执行find命令,如下所示:

I want to execute a find command within a find command, something like the following :

find /some/dir -type d -exec find -type f -delete {} \;

因此,如果我们以上面的示例为例,则第一个find命令将在特定文件夹中查找目录,第二个find命令将删除每个文件夹中的文件.

So if we take the example above, this first find command finds directories in a specific folder, and the second find command deletes the files with each of those folders.

那么,简单地说,在bash中是否有可能在find命令中执行find命令?另外,如果有一个主意,为什么这不是一个好主意.不能将其用作递归查找文件的方法吗?

So, simply put, is it possible in bash to execute a find command within a find command? Also, why would this be a bad idea, if there is one. Could this not be used as a way to find files recursively?

推荐答案

原则上,您可以传递find命令作为要为-exec执行的命令,就像您可以执行其他任何命令一样.这很少是一个好主意,但这是另一个讨论.

In principle, you can pass a find command as the command to execute for -exec, just as you can any other command. It would seldom be a good idea, but that's another discussion.

但是,如果两个find命令都需要-exec,则存在语法问题.两个find命令都需要一个标记来结束该命令,即;+.即使您尝试混合结束标记,也无法正常工作.第一个find命令将把第一个结束标记解释为它的结束标记,然后它将反对第二个结束标记.由于没有执行第二个find命令,因此缺少结束标记也没关系.

However, there's a syntactic problem if both of the find commands require -exec. Both of the find commands need a marker to end the command, either ; or +. That won't work well, even if you try mixing the end markers. The first find command will interpret the first end marker as its end marker, and it will then object to the second end marker. Since the second find command isn't executed, it won't matter that its end marker is missing.

在命令中,您将{}放在错误的位置:

In your command, you have the {} in the wrong place:

find /some/dir -type d -exec find {} -type f -delete \;

这将起作用(应该起作用;我尚未测试过—我喜欢我的文件!).如果在目录级搜索中遇到诸如-mtime +365之类的条件,这将更有意义.如果目录一年没有修改过,请删除其中的文件.

This will work (should work; I haven't tested it — I like my files!). It would make more sense if you had a condition such as -mtime +365 on the directory-level search; if a directory hasn't been modified for a year, delete the files that are in it.

但是,只要只有一个-exec,您就应该能够从find运行find.

However, as long as there's only one -exec, you should be able to run find from find.

这篇关于是否可以将find命令传递给另一个find命令的-exec?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆