What Command On Mac To Search For A Word In A Text

Many users find that using an external keyboard with keyboard shortcuts for Word 2016 for Mac helps them work more efficiently. For users with mobility or vision disabilities, keyboard shortcuts are an essential alternative to using a mouse. This article itemizes the keyboard shortcuts for.

Active1 month ago

I'm trying to find a way to scan my entire Linux system for all files containing a specific string of text. Just to clarify, I'm looking for text within the file, not in the file name.

When I was looking up how to do this, I came across this solution twice:

However, it doesn't work. It seems to display every single file in the system.

Is this close to the proper way to do it? If not, how should I? This ability to find text strings in files would be extraordinarily useful for some programming projects I'm doing.

What command on mac to search for a word in a textboxPeter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
NathanNathan
26.1k8 gold badges28 silver badges44 bronze badges

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

42 Answers

12 next

Do the following:

  • -r or -R is recursive,
  • -n is line number, and
  • -w stands for match the whole word.
  • -l (lower-case L) can be added to just give the file name of matching files.

Along with these, --exclude, --include, --exclude-dir flags could be used for efficient searching:

  • This will only search through those files which have .c or .h extensions:

  • This will exclude searching all the files ending with .o extension:

  • For directories it's possible to exclude a particular directory(ies) through --exclude-dir parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:

This works very well for me, to achieve almost the same purpose like yours.

For more options check man grep.

rakib_rakib_
88.5k3 gold badges13 silver badges24 bronze badges

You can use grep -ilR:

  • i stands for ignore case (optional in your case).
  • R stands for recursive.
  • l stands for 'show the file name, not the result itself'.
  • / stands for starting at the root of your machine.
fedorquifedorqui
182k57 gold badges377 silver badges420 bronze badges

You can use ack. It is like grep for source code. You can scan your entire file system with it.

Just do:

In your root directory.

You can also use regular expressions, specify the filetype, etc.

UPDATE

I just discovered The Silver Searcher, which is like ack but 3-5x faster than it and even ignores patterns from a .gitignore file.

RAJ
8,7411 gold badge26 silver badges56 bronze badges
EarlOfEgoEarlOfEgo
8,8065 gold badges36 silver badges51 bronze badges

You can use:

The r stands for recursive and so will search in the path specified and also its sub-directories. This will tell you the file name as well as print out the line in the file where the string appears.

Or a command similar to the one you are trying (example: ) for searching in all javascript files (*.js):

This will print the lines in the files where the text appears, but it does not print the file name.

In addition to this command, we can write this too:grep -rn 'String to search' /path/to/directory/or/file-r: recursive searchn: line number will be shown for matches

learner_19learner_19
2,5511 gold badge15 silver badges8 bronze badges
Sudipta
3,2881 gold badge18 silver badges38 bronze badges
A RA R
1,5392 gold badges14 silver badges31 bronze badges

List of file names containing a given text

First of all, I believe you have used -H instead of -l. Also you can try adding the text inside quotes followed by {} .

Example

Let's say you are searching for files containing specific text 'Apache License' inside your directory. It will display results somewhat similar to below (output will be different based on your directory content).

Remove case sensitiveness

Even if you are not use about the case like 'text' vs 'TEXT', you can use the -i switch to ignore case. You can read further details here.

Hope this helps you.

lkamallkamal

grep (GNU or BSD)

You can use grep tool to search recursively the current folder, like:

Note: -r - Recursively search subdirectories.

You can also use globbing syntax to search within specific files such as:

Note: By using globbing option (**), it scans all the files recursively with specific extension or pattern. To enable this syntax, run: shopt -s globstar. You may also use **/*.* for all files (excluding hidden and without extension) or any other pattern.

If you've the error that your argument is too long, consider narrowing down your search, or use find syntax instead such as:

Alternatively use ripgrep.

ripgrep

If you're working on larger projects or big files, you should use ripgrep instead, like:

Checkout the docs, installation steps or source code on the GitHub project page.

It's much quicker than any other tool like GNU/BSDgrep, ucg, ag, sift, ack, pt or similar, since it is built on top of Rust's regex engine which uses finite automata, SIMD and aggressive literal optimizations to make searching very fast.

It supports ignore patterns specified in .gitignore files, so a single file path can be matched against multiple glob patterns simultaneously.

You can use the common parameters such as:

  • -i - Insensitive searching.
  • -I - Ignore the binary files.
  • -w - Search for the whole words (in opposite of partial word matching).
  • -n - Show the line of your match.
  • -C/--context (e.g. -C5) - Increases context, so you see the surrounding code .
  • --color=auto - Mark up the matching text.
  • -H - Displays filename where the text is found.
  • -c - Displays count of matching lines. Can be combined with -H.
kenorbkenorb
80.9k34 gold badges451 silver badges465 bronze badges

If your grep doesn't support recursive search, you can combine find with xargs:

I find this easier to remember than the format for find -exec.

This will output the filename and the content of the matched line, e.g.

Optional flags you may want to add to grep:

  • -i - case insensitive search
  • -l - only output the filename where the match was found
  • -h - only output the line which matched (not the filename)
RobEarlRobEarl
7,1566 gold badges28 silver badges48 bronze badges
  • i: Ignore case distinctions in both the PATTERN and the input files.
  • n: Prefix each line of output with the 1-based line number within its input file.
  • s: Suppress error messages about nonexistent or unreadable files.
  • r: Read all files under each directory, recursively.
Fabio Poloni
6,1393 gold badges34 silver badges69 bronze badges
enfinetenfinet

How do I find all files containing specific text on Linux? (...)

I came across this solution twice:

find / -type f -exec grep -H 'text-to-find-here' {} ;

If using find like in your example, better add -s (--no-messages) to grep, and 2>/dev/null at the end of the command to avoid lots of Permission denied messages issued by grep and find:

find is the standard tool for searching files - combined with grep when looking for specific text - on Unix-like platforms. The find command is often combined with xargs, by the way.

Faster and easier tools exist for the same purpose - see below. Better try them, provided they're available on your platform, of course:

Faster and easier alternatives

RipGrep - fastest search tool around:

The Silver Searcher:

ack:

Note: You can add 2>/dev/null to these commands as well, to hide many error messages.

Warning: unless you really can't avoid it, don't search from '/' (the root directory) to avoid a long and inefficient search! So in the examples above, you'd better replace '/' by a sub-directory name, e.g. '/home' depending where you actually want to search...

BludzeeBludzee
1,8065 gold badges25 silver badges36 bronze badges

There's a new utility called The Silversearcher

It works closely with Git and other VCS. So you won't get anything in a .git or another directory.

You can simply use

And it will do the task for you!

Neil AgarwalNeil Agarwal
kenorb
80.9k34 gold badges451 silver badges465 bronze badges
venkatvenkat

Use pwd to search from any directory you are in, recursing downward

UpdateDepending on the version of grep you are using, you can omit pwd. On newer versions . seems to be the default case for grep if no directory is giventhus:

grep -rnw -e 'pattern'

or

grep -rnw 'pattern'

will do the same thing as above!

mahatmanichmahatmanich
7,0212 gold badges45 silver badges67 bronze badges

grep can be used even if we're not looking for a string.

Simply running,

will print out the path to all text files, i.e. those containing only printable characters.

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
Alex JasminAlex Jasmin
34.4k5 gold badges66 silver badges61 bronze badges

Here are the several list of commands that can be used to search file.

Atul ArvindAtul Arvind
10.9k5 gold badges37 silver badges52 bronze badges

Explanation from comments

find is a command that lets you find files and other objects like directories and links in subdirectories of a given path. If you don't specify a mask that filesnames should meet, it enumerates all directory objects.

Vinod JoshiVinod Joshi

Try:

which will search all file systems, because / is the root folder.

For home folder use:

For current folder use:

kenorb
80.9k34 gold badges451 silver badges465 bronze badges
user4863663user4863663

Hope this is of assistance...

Expanding the grep a bit to give more information in the output, for example, to get the line number in the file where the text is can be done as follows:

And if you have an idea what the file type is you can narrow your search down by specifying file type extensions to search for, in this case .pas OR .dfm files:

Short explanation of the options:

  1. . in the find specifies from the current directory.
  2. -name '*.*' : for all files( -name '*.pas' -o -name '*.dfm' ) : Only the *.pas OR *.dfm files, OR specified with -o
  3. -type f specifies that you are looking for files
  4. -print0 and --null on the other side of the | (pipe) are the crucial ones, passing the filename from the find to the grep embedded in the xargs, allowing for the passing of filenames WITH spaces in the filenames, allowing grep to treat the path and filename as one string, and not break it up on each space.
Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
Gert van BiljonGert van Biljon

Silver Searcher is a terrific tool, but ripgrep may be even better.

It works on Linux, Mac and Windows, and was written up on Hacker News a couple of months ago (this has a link to Andrew Gallant's Blog which has a GitHub link):

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
AAAfarmclubAAAfarmclub

A Simple find can work handy. alias it in your ~/.bashrc file:

Start a new terminal and issue:

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
danglingpointerdanglingpointer
3,0433 gold badges14 silver badges31 bronze badges

I wrote a Python script which does something similar. This is how one should use this script.

The first argument, path, is the directory in which we will search recursively. The second argument, pattern_to_search, is a regular expression which we want to search in a file. We use the regular expression format defined in the Pythonre library. In this script, the . also matches newline.

The third argument, file_pattern, is optional. This is another regular expression which works on a filename. Only those files which matches this regular expression will be considered.

For example, if I want to search Python files with the extension py containing Pool( followed by word Adaptor, I do the following,

And voila, it generates the path of matched files and line number at which the match was found. If more than one match was found, then each line number will be appended to the filename.

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
DilawarDilawar
2,5557 gold badges33 silver badges50 bronze badges

Use:

This will report how many copies of your pattern are there in each of the files in the current directory.

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
Dr_HopeDr_Hope

grep is your good friend to achieve this.

if you don't care about the case of the text to find then use

PrashPrash

To search for the string and output just that line with the search string:

e.g.:

To display filename containing the search string:

e.g.:

user3124504

There is an ack tool that would do exactly what you are looking for.

You may ignore -i for case sensitive search

Daniel
3,3132 gold badges27 silver badges39 bronze badges
PalPal

All previous answers suggest grep and find. But there is another way: Use Midnight Commander

It is a free utility (30 years old, proven by time) which is visual without being GUI. It has tons of functions, and finding files is just one of them.

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges

What Command On Mac To Search For A Word In A Textbox

Peter M.Peter M.

The below command will work fine for this approach:

What Command On Mac To Search For A Word In A Text Message

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
Pradeep GoswamiPradeep Goswami

What Command On Mac To Search For A Word In A Text Box

Avoid the hassle and install ack-grep. It eliminates a lot of permission and quotation issues.

Then go to the directory you want to search and run the command below

How To Get Mac To Search For Printer

KareemKareem
Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
Tayab HussainTayab Hussain

I am fascinated by how simple grep makes it with 'rl'

Use '-r' without 'l' to see the file names followed by text in which the pattern is found!

What Command On Mac To Search For A Word In A Text File

Works just perfect..

Hope it helps!

nitinr708nitinr708

What Command On Mac To Search For A Word In A Text Start With A

9102 gold badges15 silver badges24 bronze badges

Not the answer you're looking for? Browse other questions tagged linuxtextgrepdirectoryfind or ask your own question.