简体   繁体   English

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

[英]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.jdkfoo.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.

相关问题 命令使用Unix Terminal打开当前文件夹中具有指定扩展名的所有文件吗? - Command to open all files with a specified extension the current folder using Unix Terminal? Unix命令在所有文件中搜索文本字符串 - Unix command to search for text string in all files Glob 匹配除某些扩展名以外的文件 - Glob to match files except certain extension 查找具有特定名称的所有子文件夹,以便超级文件夹具有特定模式 - finding all subfolder with certain name such that superfolder has certain pattern Bash 查找具有特定扩展名的文件,但排除文件名中具有特定关键字的文件 - Bash find files with certain extension, but exclude those with certain keyword in filename 如何递归删除 UNIX 目录中的所有隐藏文件? - How do you recursively delete all hidden files in a directory on UNIX? 递归查找与特定模式匹配的所有文件 - Recursively find all files that match a certain pattern 终端命令(Mac)更改文件夹中所有文件的扩展名 - A terminal command (Mac) to change the extension of all the files in a folder 在Mac Terminal中以特定名称查找所有文件中的字符串 - Find a String in all Files with a certain name in Mac Terminal 编写终端命令以将具有特定扩展名的所有文件列出到 csv 文件 - Writting a terminal command to list all files with certain ext to a csv file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM