在 Unix 中查找具有特定扩展名的所有文件?

我有一个文件 /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

我试图找到如果我有 * 。在我硬盘上的任何地方。所以我执行了一个搜索命令:

find . -name "*.jdk"

但它什么都没找到,连我知道的那个都没有,为什么?

92773 次浏览

Probably your JDKs are uppercase and/or the version of find available on OS X doesn't default to -print if no action is specified; try:

find . -iname "*.jdk" -print

(-iname is like -name but performs a case-insensitive match; -print says to find to print out the results)

--- EDIT ---

As noted by @Jaypal, obviously find . ... looks only into the current directory (and subdirectories), if you want to search the whole drive you have to specify / as search path.

find . only looks in your current directory. If you have permissions to look for files in other directories (root access) then you can use the following to find your file -

find / -type f -name "*.jdk"

If you are getting tons of permission denied messages then you can suppress that by doing

find / -type f -name "*.jdk" 2> /dev/null

The '.' you are using is the current directory. If you're starting in your home dir, it will probably miss the JDK files.

Worst case search is to start from root

 find / -name '*.jdk' -o -name '*.JDK' -print

Otherwise replace '/' with some path you are certain should be parent to you JDK files.

I hope this helps.

a/

find . means, "find (starting in the current directory)." If you want to search the whole system, use find /; to search under /System/Library, use find /System/Library, etc.

b/

It's safer to use single quotes around wildcards. If there are no files named *.jdk in the working directory when you run this, then find will get a command-line of:

    find . -name *.jdk

If, however, you happen to have files junk.jdk and foo.jdk in the current directory when you run it, find will instead be started with:

    find . -name junk.jdk foo.jdk

… which will (since there are two) confuse it, and cause it to error out. If you then delete foo.jdk and do the exact same thing again, you'd have

    find . -name junk.jdk

…which would never find a file named (e.g.) 1.6.0.jdk.

What you probably want in this context, is

    find /System -name '*.jdk'

…or, you can "escape" the * as:

    find /System -name \*.jdk

find / -type f -name "*.jdk" works on Mac also

ls *.jpg | cut -f 1 -d "."

sub out the '.jpg' to whatever extension you want to list

If you are on Mac terminal, and also already in the directory where you want the search to be conducted at, then this may also work for you:

find *.pdf

At least it worked for me.

This works for me on macOS.

find . -type f -iname '*.jdk'