[英]Finding all files with certain extension in Unix?
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.我正在尝试查找我的硬盘驱动器上是否有 *.jdk。 So I do a search command:
所以我做了一个搜索命令:
find . -name "*.jdk"
But it doesn't find anything.但它没有找到任何东西。 Not even the one I know that I have.
甚至不是我所知道的那个。 How come?
怎么来的?
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 -
如果您有权在其他目录中查找文件(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
a/一种/
find .
means, "find (starting in the current directory)."意思是“查找(从当前目录开始)”。 If you want to search the whole system, use
find /
;如果要搜索整个系统,请使用
find /
; to search under /System/Library
, use find /System/Library
, etc.在
/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
find
will get a command-line of:如果运行此命令时工作目录中没有名为 *.jdk 的文件,则
find
将获得以下命令行:
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:但是,如果您在运行它时
foo.jdk
在当前目录中有文件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.jdk
and do the exact same thing again, you'd have如果您然后删除
foo.jdk
并再次执行完全相同的操作,您将有
find . -name junk.jdk
…which would never find a file named (eg) 1.6.0.jdk
. …永远找不到名为(例如)
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
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;如果未指定任何操作,可能您的 JDK 是大写的和/或 OS X 上可用的
find
版本不会默认为-print
; try:尝试:
find . -iname "*.jdk" -print
( -iname
is like -name
but performs a case-insensitive match; -print
says to find
to print out the results) (
-iname
与-name
类似,但执行不区分大小写的匹配; -print
表示find
打印出结果)
--- EDIT --- - - 编辑 - -
As noted by @Jaypal, obviously find .
正如@Jaypal 所指出的,显然
find .
... looks only into the current directory (and subdirectories), if you want to search the whole drive you have to specify /
as search path. ... 只查看当前目录(和子目录),如果你想搜索整个驱动器,你必须指定
/
作为搜索路径。
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.我希望这有帮助。
find / -type f -name "*.jdk" 也适用于 Mac
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:如果您在 Mac 终端上,并且已经在您想要进行搜索的目录中,那么这也可能对您有用:
find *.pdf
At least it worked for me.至少它对我有用。
ls *.jpg | ls *.jpg | cut -f 1 -d "."
cut -f 1 -d "."
sub out the '.jpg' to whatever extension you want to list将“.jpg”分出到您要列出的任何扩展名
这在 macOS 上对我有用。
find . -type f -iname '*.jdk'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.