Finding all files with certain extension in Unix?
MacosUnixSearchTerminalMacos Problem Overview
I have a file /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:
find . -name "*.jdk"
But it doesn't find anything. Not even the one I know that I have. How come?
Macos Solutions
Solution 1 - Macos
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
Solution 2 - Macos
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
Solution 3 - Macos
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.
Solution 4 - Macos
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.
Solution 5 - Macos
find / -type f -name "*.jdk" works on Mac also
Solution 6 - Macos
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.
Solution 7 - Macos
ls *.jpg | cut -f 1 -d "."
sub out the '.jpg' to whatever extension you want to list
Solution 8 - Macos
This works for me on macOS.
find . -type f -iname '*.jdk'