Searching Mac Through Terminal?

23

13

are there any commands to search your Mac using terminal? I tried usin grep to search but it seems somewhat unresponsive and nothing comes up.

For example, I would type "grep Files" (A folder name) and it just prints a blank line and nothing happens.

JFW

Posted 2010-08-18T09:46:37.337

Reputation: 2 094

possible duplicate of Search through text files in Mac OS X

– Chealion – 2010-08-18T15:18:21.750

I'm interested in searching through all types of files; Not necessarily excluding text files but not only text files. – JFW – 2010-08-19T03:44:56.657

Answers

21

You can also use the mdfind command in order to perform a search with Spotlight. More info here.

Use mdfind -name searchterm in order to retrieve files with the name searchterm. Use mdfind searchterm to perform a search on file name and content.

reg

Posted 2010-08-18T09:46:37.337

Reputation: 827

26

If you just want to find files with a certain name, use find

The man page can be found HERE or by typing man find at the terminal prompt.

Basically, find will recursively look for a file meeting criteria you specify. The easiest example:

find . -name file_name -print

That will search for a file named "file_name" starting in the current directory and searching below and print the files with that name.

find ~ -name ".DS_Store" -delete

That will find all the .DS_Store files and delete them.

You can search by name, regex, date. You can act on the file in any Unix way with the -exec predicate.

You can also use find as the start of a more complex pipeline of actions. Example:

find . -type f -print | egrep -i '\.m4a$|\.mp3$'

Will find all the files with extensions .m4a or .mp3

find . -type f -print | egrep -i '\.m4a$|\.mp3$' | wc -l

Will give you a count of those files.

drewk

Posted 2010-08-18T09:46:37.337

Reputation: 984

If -print is the only predicate, it can be omitted; also, there's simple globbing available. Thus finding all of the .txt files under a directory foo would be done with find foo -name \\*.txt – Norman Gray – 2010-08-18T17:35:15.570

@yoshi: That is absolutely false. Typing find . IS recursive from the cwd. Try typing find . in your root directory! – drewk – 2012-12-01T02:49:21.750

My bad could swear it wasn't recursive but maybe server has chocked -shrug- – nuala – 2012-12-07T10:49:38.510

5

If you want to search through a whole folder, just use -r on grep:

grep -r pattern folder/to/search

With find, you can also use xargs:

find folder/to/search -name '*.txt' | xargs grep pattern

or to make sure that you search two files at a time and therefore have the filenames specified:

find folder/to/search -name '*.txt' | xargs grep -n2 pattern

Eric Darchis

Posted 2010-08-18T09:46:37.337

Reputation: 1 178

2

grep expects both a pattern and a filespec. If one is missing then it uses what is passed as the pattern, and waits for the data to search via standard input.

If you want to use a more complex filespec then use find.

find ~ -name '*.txt' -exec grep -q 'secret' {} \; -print

Ignacio Vazquez-Abrams

Posted 2010-08-18T09:46:37.337

Reputation: 100 516

I just basically want it to find files that have the name that I enter into the command. – JFW – 2010-08-18T12:11:56.830

In that case you don't need to use the -exec predicate in find. – Ignacio Vazquez-Abrams – 2010-08-18T12:13:05.283