Is it possible to pass a find command to -exec of another find command?

大憨熊 提交于 2019-12-12 03:26:02

问题


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.

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 {} \;

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.

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?


回答1:


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.

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 \;

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.

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



来源:https://stackoverflow.com/questions/27555629/is-it-possible-to-pass-a-find-command-to-exec-of-another-find-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!