Linux 非选项后的 -maxdepth 选项 AND 查找:路径必须在表达式之前

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12443689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:21:55  来源:igfitidea点击:

-maxdepth option after a non-option AND find: paths must precede expression

linux

提问by MFIHRI

Hope someone can help with this:

希望有人可以帮助解决这个问题:

Am trying to delete session files on /tmp with this command:

我正在尝试使用以下命令删除 /tmp 上的会话文件:

find /tmp -name 'sess_*' -user Username -maxdepth 1 $CMD {} \;

but I got these errors:

但我收到了这些错误:

find: warning: you have specified the -maxdepth option after a non-option argument -name, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.

find: paths must precede expression Usage: find [-H] [-L] [-P] [path...] [expression]

find: 警告:您在非选项参数 -name 之后指定了 -maxdepth 选项,但选项不是位置性的(-maxdepth 影响在它之前指定的测试以及在它之后指定的测试)。请在其他参数之前指定选项。

find: paths must precede expression Usage: find [-H] [-L] [-P] [path...] [expression]

I looked for solutions over the web but could not find any. I have deleted other tmp files with other commands and wonder if that affected some volume or socket.

我在网上寻找解决方案,但找不到任何解决方案。我已经用其他命令删除了其他 tmp 文件,想知道这是否影响了某些音量或套接字。

Thank you in advance

先感谢您

采纳答案by Barmar

findhas three types of options: options that are used to match files (e.g. -name, -user), options that specify actions to perform on the matched files (-print, -exec), and options that control the overall behavior of the command (e.g. -maxdepth, -xdev). The third type must be put before the other two. So it should be:

find具有三种类型的选项:用于匹配文件的选项(例如-name, -user)、指定对匹配文件执行的操作的选项(-print, -exec)以及控制命令整体行为的选项(例如-maxdepth, -xdev)。第三种必须放在其他两种之前。所以应该是:

find /tmp -maxdepth 1 -name 'sess_*' -user Username -exec $CMD {} \;