简体   繁体   English

Linux查找命令shell扩展

[英]Linux find command shell expansion

I have just a little question I don't understand with the find command. 我有一个小问题,我不理解find命令。

I can do this : 我可以做这个 :

[root@hostnaoem# ❯❯❯ls /proc/*/fd

But this give me an error : 但这给了我一个错误:

[root@hostnaoem# ❯❯❯ find /proc/*/fd -ls
find: `/proc/*/fd': No such file or directory

even if I use "/proc/ /fd" , /proc/" "/fd or "/proc/*/fd" 即使我使用“/ proc / / fd” / proc /“ ”/ fd“/ proc / * / fd”

I've searched wha find shell expansion says about that, but I found nothing. 我已经搜索过, 发现 shell扩展说了这些,但我一无所获。 Can someone tell me why? 有人可以告诉我为什么吗?

Thanks 谢谢

If you just RTFM, you'll learn that the syntax for find is: 如果您只是RTFM,您将了解到find的语法是:

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

The usually used subset of that is: 通常使用的子集是:

find whereToSearch (-howToSearch arg)*

To find all files|directories named fd in /proc : 要在/proc找到名为fd所有文件|目录:

find /proc -name fd 

-name is the most common howToSearch expression: -name是最常见的howToSearch表达式:

-name pattern -name模式

  Base of file name (the path with the leading directories removed) matches shell pattern pattern. The metacharacters (`*', `?', and `[]') match a `.' at the start of the base name (this is a change in findutils-4.2.2; see section STANDARDS CON‐ FORMANCE below). To ignore a directory and the files under it, use -prune; see an example in the description of -path. Braces are not recognised as being special, despite the fact that some shells including Bash imbue braces with a special meaning in shell patterns. The filename matching is performed with the use of the fnmatch(3) library function. Don't forget to enclose the pattern in quotes in order to protect it from expansion by the shell. 

(Note the the last sentence) (注意最后一句)

If your pattern contains slashes, you need -path or -wholename (same thing): 如果你的模式包含斜杠,你需要-path-wholename (同样的东西):

find /proc/ -wholename '/proc/[0-9]*/fd' 2>/dev/null 

Other expressions you might want to use are: -type -depth, -mindepth, -maxdepth -user, -uid 您可能想要使用的其他表达式是:-type -depth,-mindepth,-maxdepth -user,-uid

See find(1) to learn more about each search expressions. 请参阅find(1)以了解有关每个搜索表达式的更多信息。 If you want to search the in-terminal manual ( man find or man 1 find ), you can use the / character to enter search mode (like Ctrl+F in most GUI apps). 如果要搜索终端内手册( man findman 1 find ),可以使用/字符进入搜索模式(如大多数GUI应用程序中的Ctrl+F )。


Usage of ls with globbing ( * ) is generally a code smell. 使用具有globbing( * )的ls通常是代码气味。 Unless you use the -d flag, it'll list the contents of the directories that match the glob pattern in addition to the matches. 除非使用-d标志,否则它将列出除匹配之外与glob模式匹配的目录的内容。 I find the echo globpattern form generally more convenient for viewing the results of a glob pattern match. 我发现echo globpattern表单通常更方便查看glob模式匹配的结果。

This work : 这项工作:

[root@hostname # ❯❯❯ find /proc/ -path /proc/*/fd -ls

Regards. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM