How do I find a file by filename in Mac OSX terminal?

133

30

I want to find a file on my Macbook with the filename: abc.dmg. I've tried to use spotlight, but it doesn't find it. When I tried find, and used: find -name abc.dmg -path /, I got errors back.

What is the proper syntax to find a file by filename with the find command on a Mac OSX terminal?

SPRBRN

Posted 2010-12-28T17:42:34.363

Reputation: 5 185

1@VxJasonxV Apple questions are 100% on topic for Super User, and no real need to have every Apple question moved there. iPhone or iPad questions however will be moved when/if required. – BinaryMisfit – 2010-12-28T22:57:17.597

It wasn't so much a reason of being off-topic, but the fact that there is a more specific topic/site for this general subject. – VxJasonxV – 2010-12-28T23:57:31.957

@Diago: Has there been any discussion regarding this on meta or chat? Just wondering because AFAIK apple.SE will be launching out of beta pretty soon, so it would be nice to get everyone on the same page. – Robert S Ciaccio – 2010-12-29T00:19:47.300

1

@calavera. Fairly old discussion but relevant can be found here. Apple SE, Ubuntu SE and Linux SE is all there to compliment Super User, but in no way do we want to force users to move. I prefer asking my Apple questions here rather then Apple, since I do use 3 different operating systems. If I have a very specific question, I will ask it on a more specialized site.

– BinaryMisfit – 2010-12-29T07:22:48.637

Answers

182

In its simplest form, find takes one parameter: the path. In its actually useful form, it takes the path followed by narrowing criteria.

Thus, you want:

  • find (the program)
  • / (the path), and
  • -name abc.dmg (the criteria).
find / -name abc.dmg

VxJasonxV

Posted 2010-12-28T17:42:34.363

Reputation: 2 166

This shows folders too. – Iulian Onofrei – 2017-02-02T11:55:02.833

Yes. That can be controlled via the -type flag. – VxJasonxV – 2017-02-02T13:09:15.123

13You may get permission denied type errors when searching the entire drive but they can be ignored unless you expect the file to be in a restricted folder. If so use sudo find / -name abc.dmg – Chris Nava – 2010-12-28T18:31:10.333

Tried it, but no result. – SPRBRN – 2010-12-28T18:55:04.690

6Presumably then, you don't have a file named abc.dmg on your computer. You can use -iname instead of -name to make the search case insensitive. I highly doubt you're actually searching for "abc.dmg", and given that (presumably) fact, it's hard to tell you what your issue is without having the actual details. – VxJasonxV – 2010-12-28T19:18:44.677

2@rxt: if find dooesn't find it, it's not there... – Robert S Ciaccio – 2010-12-28T19:37:07.083

5Given that OP report have tried find in the question text, it is worth noting that the order of the arguments to find matters. If must be find [path]+ [selection, grouping, and actions]. – dmckee --- ex-moderator kitten – 2010-12-28T22:21:48.023

@dmckee: correct, so if he's not typing it as @VxJasonxV suggested it won't work. But if @rxt used J's syntax, the file ain't there :) – Robert S Ciaccio – 2010-12-29T00:25:46.377

26

find . -name '*.csv' for instance worked for me with wildcards. OP could also use find . -name '*.dmg' to check his current directory, assuming he was in /.

pjammer

Posted 2010-12-28T17:42:34.363

Reputation: 371

10

Rich Homolka

Posted 2010-12-28T17:42:34.363

Reputation: 27 121

2But Spotlight doesn't find it, per the OP's original question. – VxJasonxV – 2010-12-28T19:24:31.493

6

You can use the locate command.

locate abc.dmg

Wuffers

Posted 2010-12-28T17:42:34.363

Reputation: 16 645

3Note the osx tag. slocate db doesn't populate by default. – VxJasonxV – 2010-12-28T17:46:11.743

Tried it, after creating the database, but no result. – SPRBRN – 2010-12-28T18:56:24.500

if locate and mdfind both fail, chances are the file isn’t on your machine. Or its not named how you think its named. perhaps searching for ALL DMGs may be your next step? – peelman – 2010-12-28T19:20:27.477

3

The simplest way (which I'm sure you've already tried, but hey, let me add it to the thread anyway) is to enter abc.dmg into the search box on the top right of any finder window, then select "File Name" from the options on the Search Bar that appears.

No need for the terminal.

Also remember that Spotlight only indexes directories specified in the Spotlight control panel and abc.dmg may not be in one of those directories.

Correct me if i'm wrong, but i think the find command needs to know what to output:

find / -name abc.dmg -print

...should print any results to the terminal (including permission errors).

If you don't want permission errors and want to search other User directories then:

sudo find / -name abc.dmg -print

Jupiter

Posted 2010-12-28T17:42:34.363

Reputation: 31

2

You may use following command line functions to quickly find and open relevant file. I find this easier than typing long string of query in spotlight window.

Add following functions in ~/.bash_aliases.

# find any item matching search query in file name
spot(){
  mdfind "kMDItemDisplayName=='*$1*'cd";
}

# restrict to files under (recursive) a specific path 
findpaper(){
    mdfind -onlyin "/Users/foo/articles" "kMDItemDisplayName=='*$1*'cd";
}

# default to open the first entry unless 2nd positional argument is given
openpaper(){
    FILEID=$(printf "%sp" ${2:-1})

    open "$(mdfind -name -onlyin "/Users/foo/articles" "kMDItemDisplayName=='*$1*'cd" | sed -n "${FILEID}")"

}

Now, either source ~/.bash_aliases or open a new terminal load functions. To search for files with words, pie and 2016 anywhere in the file name, do

spot pie*2016 #or
spot 2016*pie

There is no need to prepend or append * to your query as the search pattern, '*$1*' already tags wild card entry at beginning and end of your query. Additional cd is for case insensitive and ignoring diacritical marks, e.g., fred will return both, Frédéric and FrEDeric.

findpaper will restrict search to results under a specific path (recursive) while openpaper pie*201 will open a (or first of multiple results) search result or openpaper pie*201 3 will open third result entry. To avoid opening bash scripts or other non-document files, you may restrict file contents by additional search attributes. See File Metadata Query Expression Syntax and https://ss64.com/osx/mdfind.html for using other search operators.

Samir

Posted 2010-12-28T17:42:34.363

Reputation: 131

Awesome! Stilling it :D – Atcold – 2019-07-15T18:17:09.863

0

Capture a list of every file on your disk as root from /

sudo find / &> ~/file-list.txt
sudo chown $(whoami) ~/file-list.txt

Cat the entire file through grep to search entire drive:

cat ~/file-list.txt | grep abc.dmg

Use regular expressions to show only .jpg and .dmg files:

cat ~/file-list.txt | grep -E "(\.dmg|\.jpg)"

Result:

Applications/Visualisations/CurvedSpaces-forMac.app/Contents/Resources/Textures/paper.jpg

...etc. Unfortunately will also capture all mounted disks so best to eject those Time Machine ones they have a lot of links. So in another window I run watch tail -n 10 ~/file-list.txt which shows me where it's up to in my dastardly hack!

Tomachi

Posted 2010-12-28T17:42:34.363

Reputation: 154

1Bad practices or things that deserve some comment: (1) sudo find / > ~/file-list.txt seems better than running the entire shell as root. (2) find * will skip hidden (dot) objects (if any) in /. (3) &> is a bashism. (4) Useless use of cat. (5) grep abc.dmg will print e.g. /foo/bar/123abcXdmg456/baz/whatever. // One good thing your answer brings to this thread (and other answer don't) is the approach where user runs find once and then reuses the saved result multiple times without querying the filesystem(s) again and again. This potential advantage is not accented though. – Kamil Maciorowski – 2019-06-09T12:26:16.500

good point thanks Kamil, have done that and also chown $(whoami) – Tomachi – 2019-07-22T12:00:59.227