How To Grep Words From a File in Unix / Linux

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
To search for an option that is starting with either - or --, run:
command --help | grep -- '-option'
command --help | grep -- '--long-option'
curl --help | grep -- '--write-out'
curl --help | grep -w -- '-I'


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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 3 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
3 comments… add one
  • ravi Oct 29, 2015 @ 10:14

    How Can I grep only the word ( instead of complete line)?

    • kamlesh Jun 23, 2016 @ 10:38

      grep -wo "word" file_name

  • Monica Nov 2, 2015 @ 4:40

    Hello Ravi,
    you can use this command to get only word
    $ grep -io "word" filename

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum