4

I'm looking for a txt file that contains a word. However when I search my hard drive (or my TimeCapsule backup) I always get stuck in a recursive search of /Volumes/Hard Drive (etc).

The hardlink (or softlink, not sure) is causing me to never complete the search.

How can I search the entire harddrive (or backup drive) for a file without getting stuck in recursion?

4
  • 2
    How are you searching for it? Spotlight doesn't get confused by this, and find doesn't follow symlinks (although it will traverse other volumes unless you specify the -x option)... Commented Mar 7, 2013 at 2:08
  • Spotlight won't show results for the entire drive.
    – jaume
    Commented Mar 7, 2013 at 6:02
  • 1
    @jaume: It does if you explicitly tell it to include system files and invisible files in the results. Commented Mar 8, 2013 at 4:23
  • @GordonDavisson: Thank you, I didn't know that, I always use locate in Terminal when doing a system wide search. In my comment above I was thinking of Spotlight as the search field that appears when you press Space+Command, not as the search function in Finder...
    – jaume
    Commented Mar 8, 2013 at 10:14

2 Answers 2

4

Use locate, grep or find at the command line.

Open Applications>Utilities>Terminal and either use:

  1. sudo locate -0 '*.txt' | xargs -0 grep WORD 2>/dev/null

    Substitute WORD with the word you want to search for. This is by far the fastest method.

    I'd recommend to recreate the locate database first to improve accuracy:

    sudo /usr/libexec/locate.updatedb

    Note that on OS X, the locate database is created by user nobody. That means that files hidden to nobody won't be indexed. You can modify /usr/libexec/locate.updatedb and comment out (that is, prefix with #) this section:

    if [ "$(id -u)" = "0" ]; then
         rc=0
         # 2013-03-08 jaume Correct mktemp
         export FCODES=`mktemp --tmpdir updatedbXXXX`
         chown nobody $FCODES
         tmpdb=`su -fm nobody -c "$0"` || rc=1
         if [ $rc = 0 ]; then
             install -m 0444 -o nobody -g wheel $FCODES /var/db/locate.database
         fi
         rm $FCODES
         exit $rc
     fi
    

    so that it is indexed under user root (this applies to OS X 10.8 (Mountain Lion), on other OS X versions locate may index files differently).

  2. sudo grep -R --exclude-dir '/Volumes/<OS volume>' --exclude-dir '/Volumes/MobileBackups' --exclude-dir '/.Spotlight*' --exclude-dir '/.MobileBackups' --exclude-dir '/.DocumentRevisions*' --include '*.txt' WORD / 2>/dev/null

    Substitute WORD with the word you want to search for and <OS volume> with the name of your OS volume. The grep options used are:

    • -R: does a recursive search in the specified path, that is, in /. If you search your TimeCapsule backup, replace / with something like /Volumes/TimeCapsule.

    • --exclude-dir: excludes the specified folders. As you see, I exclude Spotlight, the Versions folder and local TimeMachine folders. If you want to search your local drive only while your TimeCapsule is mounted, add --exclude-dir /Volumes/TimeCapsule.

    • --include '*.txt': tells grep to search files which match *.txt.

    Additionally:

    • 2>/dev/null: sends errors to /dev/null so that you don't see them.
  3. sudo find / ! -path '/.Spotlight*' ! -path '/Volumes/<OS volume>/*' ! -path '/Volumes/MobileBackups/*' ! -path '/.MobileBackups/*' ! -path '/.DocumentRevisions*' -name *.txt -type f -exec grep -H WORD {} + 2>/dev/null

    Again, substitute WORD with the word you want to search for and <OS volume> with the name of your OS volume. As before, 2>/dev/null sends errors to /dev/null so that you don't see them, and if you search your TimeCapsule backup, replace / with something like /Volumes/TimeCapsule.

    The find options used are:

    • ! -path: exclude paths specified. If you want to search your local drive only while your TimeCapsule is mounted, add ! -path /Volumes/TimeCapsule/*.

    • -name *.txt: search files named *.txt. As you see, I exclude Spotlight, the Versions folder and local TimeMachine folders.

    • -type f: search only files, ignore symlinks, folders, etc.

    • grep -H WORD {}: grep file for WORD. -H forces grep to print the filename.

The output of any of the commands above looks like this:

filename: matched line

for example, when searching for 'GNU':

/usr/local/share/doc/p7zip/DOCS/readme.txt:7-Zip is free software distributed under the GNU LGPL 
/usr/local/share/doc/p7zip/DOCS/readme.txt:1) You can compile and use compiled files under GNU LGPL rules, since 
/usr/local/share/doc/p7zip/DOCS/readme.txt:  copying.txt    - GNU LGPL license
/usr/local/share/doc/xz/history.txt:    Alexandre Sauvé helped converting the build system to use GNU

If you want to do a case insensitive search, replace grep with grep -i above.

3
  • re #1- I'm not sure what needs editing in that file. I run the find command and orginally the file didn't exist, then I ran as administrative user and got the error locate: locate database header corrupt, bigram char outside 0, 32-127: -1 Commented Mar 8, 2013 at 15:57
  • You don't need to edit /usr/libexec/locate.updatedb, it is more of a comment for expert users than something you must do. As I write, this applies to OS X 10.8, you may have another version. The error you mention: locate: locate database header corrupt says that the locate database is corrupt. Recreate it as explained in the answer doing: sudo /usr/libexec/locate.updatedb. You may want to delete the database first: sudo rm /var/db/locate.database. If you have trouble with answer #1, try another, that's why I describe 3 commands :-)
    – jaume
    Commented Mar 8, 2013 at 16:12
  • Did you manage to create the locate database? I've noticed that the locate command was wrong. I corrected it. Could you try it and report back?
    – jaume
    Commented Mar 9, 2013 at 7:18
0

You could use the program EasyFind. It can search inside files and has a nice GUI.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .