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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8607888/
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-10-21 08:42:39  来源:igfitidea点击:

Finding all files with certain extension in Unix?

macosunixsearchterminal

提问by Prostak

I have a file /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

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

I am trying to find if I have the *.jdk anywhere else on my hard drive. So I do a search command:

我正在尝试查找我的硬盘驱动器上是否有 *.jdk。所以我做了一个搜索命令:

find . -name "*.jdk"

But it doesn't find anything. Not even the one I know that I have. How come?

但它没有找到任何东西。甚至不是我所知道的那个。怎么来的?

回答by jaypal singh

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 .只在您当前的目录中查找。如果您有权在其他目录中查找文件(root 访问权限),那么您可以使用以下命令来查找您的文件 -

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

回答by BRPocock

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.

find .意思是“查找(从当前目录开始)”。如果要搜索整个系统,请使用find /; 在/System/Library、 使用find /System/Library等下搜索。

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 findwill get a command-line of:

在通配符周围使用单引号更安全。如果运行此find命令时工作目录中没有名为 *.jdk 的文件,则将获得以下命令行:

    find . -name *.jdk

If, however, you happen to have files junk.jdkand foo.jdkin the current directorywhen you run it, findwill instead be started with:

但是,如果您在运行它时碰巧有文件junk.jdk并且foo.jdk当前目录中find则将改为:

    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.jdkand do the exact same thing again, you'd have

......这会(因为有两个)混淆它,并导致它出错。如果您然后删除foo.jdk并再次执行完全相同的操作,您将拥有

    find . -name junk.jdk

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

…永远不会找到名为 (eg) 的文件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

回答by Matteo Italia

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

如果未指定任何操作,您的 JDK 可能是大写的和/或findOS X 上可用的版本不是默认的-print;尝试:

find . -iname "*.jdk" -print

(-inameis like -namebut performs a case-insensitive match; -printsays to findto print out the results)

-iname就像-name但执行不区分大小写的匹配;-printfind要打印结果)

--- 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.

正如@Jaypal 所指出的,显然find .......只查看当前目录(和子目录),如果你想搜索整个驱动​​器,你必须指定/为搜索路径。

回答by shellter

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

这 '。' 您使用的是当前目录。如果你从你的主目录开始,它可能会错过 JDK 文件。

Worst case search is to start from root

最坏情况搜索是从 root 开始

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

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

否则,将“/”替换为您确定应该是 JDK 文件的父路径。

I hope this helps.

我希望这有帮助。

回答by Dajena

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

find / -type f -name "*.jdk" 也适用于 Mac

回答by zkennedy137

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

ls *.jpg | 剪切 -f 1 -d "."

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

将“.jpg”分出到您要列出的任何扩展名