Mac – search files on command line using mdfind

Mac spotlight and finder are great way to find files on Mac. But Mac also has command line utility mdfind (utility to query meta data) which can be used to find files on Mac. It uses the same meta data which is used by spotlight search but can also find system files. Here are some use cases.
mac-mdfind-example

Create sample test files for mdfind tutorial

  1. Create file1234.txt in /opt/tmp/dir1 with following content
    file1234
    
  2. Create file4567.txt in /opt/tmp/dir2 with following content
    file1234
    

mdfind – find files (file name or content) anywhere on Mac

$ mdfind file1234
/opt/tmp/tmp3/dir2/file5678.txt
/opt/tmp/tmp3/dir1/file1234.txt

mdfind – find files (file name or content) in a dir

$ mdfind -onlyin dir1 file1234
/opt/tmp/tmp3/dir1/file1234.txt

mdfind – find files (file name only) anywhere on Mac

$ mdfind -name file1234
/opt/tmp/tmp3/dir1/file1234.txt
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments