Grep Words From a File in Unix / Linux

last updated in Categories , , , , , , , , ,

How do I grep words from a text file under UNIX / Linux / BSD / macOS operating systems?

The grep is a command line text search utility originally written for Unix, Linux, Windows, and MacOS. You need to use this tool to grep words from a text file. You can search and filter words or texts using regular expression syntax. Hence, grep is very useful for finding all appearances of a search term in a given file, filtering a Unix log file or stream/command output and so on. This page provides a quick overview of grep command usage and examples for Linux and Unix-like systems.
Grep Words From a File in Unix or Linux

Use grep to find words from a file

To find word from a file use following syntax:
grep "word" {filename}
Say, you wan to find a word named “Orange” in the file called data.txt, run:
$ grep "orange" data.txt
Grep prints all lines containing ‘orange’ from the file data.txt, regardless of word boundaries; therefore lines containing ‘orangeade’ or ‘oranges’ are also printed.

Grep Words Form the File in Unix

To print all lines containing ‘orange ‘ as a word (‘orangeade’ and ‘oranges’ will not match), run:
$ grep -w orange data.txt

Ignore case distinctions with grep

The grep command is case sensitive by default, so this example’s output does not include lines containing ‘Orange’ (with a capital O) unless they also contain ‘orange’. To perform ignore case distinctions use the -i option:
$ grep -i orange data.txt
You can combine all options together to find only ‘orange’ as a word with no case distinctions using the grep command
$ grep -wi orange data.txt

Grep multiple files

Pass the -r option to enable recursive search through a directory tree:
grep -r "main()" /home/vivek/projects/

Grep Compressed files with zgrep

One can search compressed file using the zgrep command. It invokes grep on compressed or gzipped file. The syntax is same as grep:
grep [option] "word-to-search" file.gz
grep "" /var/log/httpd/access.log.gz

How to filter commands with grep

The syntax is:
command | grep "word"
For example, search for password option
ansible-playbook --help | grep password
Linux grep command usage with examples


This page explained the grep command that is used to search text, strings or words for given text file.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at

Historical Comment Archive

3 comment

    Still, have a question? Get help on our forum!