331

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?

HopelessN00b
  • 1,891
SPRBRN
  • 8,149

10 Answers10

399

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
  • 4,363
75

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
  • 861
20

The mdfind command uses the Spotlight database

http://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/mdfind.1.html

Rich Homolka
  • 32,350
10

You can use the locate command.

locate abc.dmg
Wuffers
  • 19,619
6

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/mac/mdfind.html for using other search operators.

Samir
  • 171
5

Skip Operation not permitted

The problem when using find in the root of the operating styem, is that then you have to find the result between a lot of operation not permitted error. This grep can limit the number of the error to only "No such file or directory".

sudo find / -name abc.dmg  2>&1 | grep -v "Operation not permitted"
G M
  • 343
5

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
  • 59
3

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
  • 259
0

In your terminal use

man find

to get the full manual of the find command for your specific version of your OS.

0
  • Install homebrew:

  • /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

  • Install fuzzy finder:

  • brew install fzf

  • Access fzf through terminal:

  • fzf

  • Insert query (you will find results as you soon as you begin to write your first letters):

  • name with spaces and details you remember (add format for more detail)

  • For example: abc.dmg

  • Access to file full path.

nilon
  • 109