简体   繁体   中英

Linux find command shell expansion

I have just a little question I don't understand with the find command.

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"

I've searched wha find shell expansion says about that, but I found nothing. Can someone tell me why?

Thanks

If you just RTFM, you'll learn that the syntax for find is:

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 :

find /proc -name fd 

-name is the most common howToSearch expression:

-name pattern

  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):

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

Other expressions you might want to use are: -type -depth, -mindepth, -maxdepth -user, -uid

See find(1) to learn more about each search expressions. 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).


Usage of ls with globbing ( * ) is generally a code smell. Unless you use the -d flag, it'll list the contents of the directories that match the glob pattern in addition to the matches. I find the echo globpattern form generally more convenient for viewing the results of a glob pattern match.

This work :

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

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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