How To Find Files by Content Under UNIX / Linux

I had written lots of code in C for my school work and saved it as source code under /home/user/c/*.c and *.h. How do I find files by content such as string or words (function name such as main() under UNIX shell prompt?

You need to use the following tools to find files by content under Unix or Linux operating systems:
  1. grep command : print lines matching a pattern.
  2. find command : search for files in a directory hierarchy.

Using grep Command To Find Files By Content on Unix or Linux

Type the command as follows:

grep 'string' *.txt
grep 'main(' *.c
grep '#include<example.h>' *.c
grep 'getChar*' *.c
grep -i 'ultra' *.conf
grep -iR 'ultra' *.conf


  • -i : Ignore case distinctions in both the PATTERN (match valid, VALID, ValID string) and the input files (math file.c FILE.c FILE.C filename).
  • -R (or -r): Read all files under each directory, recursively.

Highlighting searched patterns

You can highlight patterns easily while searching large number of files:
$ grep --color=auto -iR 'getChar();' *.c

Displaying file names and line number for searched patterns

You may also need to display filenames and numbers:
$ grep --color=auto -iRnH 'getChar();' *.c

  • -n : Prefix each line of output with the 1-based line number within its input file.
  • -H Print the file name for each match. This is the default when there is more than one file to search.

Hence, type the following command:
$ grep --color=auto -nH 'DIR' *

Fig.01: grep command displaying searched pattern

Using find command to search files by words or string

We can also use the find command. The syntax is as follows:
find /dir/to/search -name "file-pattern" -print | xargs grep "word-to-search"
## OR ##
find /dir/to/search -iname "file-pattern" -print0 | xargs -I {} -0 grep "string-to-search" "{}"

For example, search all c program files (*.c) and look for “main(” and print it on the screen when matched in the current directory:
$ find . -name "*.c" -print | xargs grep "main("
$ find /projects/ -iname "*.c" -print0 | xargs -I {} -0 grep "main(" "{}"
Where find command options are:

  • -name : Base of file name. For instance, look for all Perl files (*.pl)
  • -iname : Same as above (-name), but the match is case insensitive. For example, the pattern 'prj*' match the file names, Project, project, PROJECT, etc.
  • -print : Print the full file name on the standard output.
  • -print0 : Display the full file name on the standard output, followed by a null character (instead of the newline character that -print uses). This allows file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output. This option corresponds to the -0 option of xargs.

And, the xargs command are:

  • -I {} : Replace occurrences of {} in the initial-arguments with names read from standard input. In other words, pass {} as input to the grep command.
  • -0 : Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes. The GNU find -print0 option produces input suitable for this mode.
  • grep "main(" "{}" : Search for "main(" string using the grep in a file found by find command.


You learned how to find files by content under UNIX and Linux using various commands. See the following resources:

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

🐧 25 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
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 glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep 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
25 comments… add one
  • mhymn Sep 21, 2008 @ 10:10

    Thanks for the good tip,
    But what a bout PDF files? you mentioned only text type of files which grep can work with them, I need to know the trick for the PDF files :/

  • mhernandez Sep 21, 2008 @ 10:16

    Hi and tanks for all your tips!

    I also find useful invoking grep with -nH, so that it also shows the file and the line that contains the pattern.

    Sometimes, -E or -P options come handy too, because they allow me to search for regexp.

    Have a lot of fun…

  • Gokdeniz Karadag Sep 21, 2008 @ 10:43

    “ack” is a really better alternative to grep. It is especially useful for programmers because it skips temporary files and version control files by default and searches only on code files. It searches recursively by default. check it out.

  • 🐧 nixCraft Sep 21, 2008 @ 10:53


    Thanks for your post, I’ve update FAQ.


    I will check it out ack and will update faq with it.


    Try offical pdf tool as follows:
    acroread /a search='word' *.pdf

    Appreciate all of your posts.

  • _ Sep 21, 2008 @ 11:17

    egrep -ri word location/folder/*

  • henry Sep 21, 2008 @ 17:54

    i was trying to use the find or grep commands to search for the cp command in the ./bin directory but i couldn’t get it. could you teach me how to get it. i know it’s there. just want to try out the find and the grep commands.

  • mhernandez Sep 21, 2008 @ 18:03


    find ./bin -name "cp"

    If you were trying to find where cp program lies, you can use
    which cp

  • Vaishnavi Sep 22, 2008 @ 4:18

    find . -type f -name "*.c" -print \
    -exec grep -s "main(" {}\;

    This command should also work

  • sathiya Sep 22, 2008 @ 8:07

    when you want to copy all the files which contains the specified function name.,

    find -iname "*.c" -exec grep -l 'main(' {} \; -a -exec cp {} test1/ \;

  • yoander Sep 22, 2008 @ 13:51

    If you want to get a list o file where the string match the you must use -l option: grep -l ‘main(‘ *.c

  • Carl Nov 23, 2010 @ 16:00

    I prefer using finds -exec option instead of the pipe and xargs, because with this I managed to deal with paths containing spaces, which are not searched by using the simple pipe/xargs combination in this example.

    find . -type f -print -exec grep --color=auto --no-messages -nH "search string" "{}" \;

    • yoander (sedlav) Nov 23, 2010 @ 16:58

      You can deal with path containing spaces using -print0 and -0 options for find and xargs commands respectively

  • Carl Nov 23, 2010 @ 17:00

    thanks, didn’t know that. I thought it was possible, but I didn’t know how to achieve this and so the -exec option was easier, since it’s pretty obvious to surround the {} with “.

  • Vijay Mar 17, 2011 @ 12:56

    Thanks a lot for valuable posts.

  • Dave Feb 8, 2012 @ 8:31

    grep -iR 'fred' *.c
    only to be told *.c : no such file or directory
    of course a quick look in the sub directories does reveal some *.c files.

    I really would love to know why some people think linux is better than cpm… for heavens sake doesn’t anything actually work with this mess?

  • yoander Feb 8, 2012 @ 15:16

    Dave -R option means recursive so if you want to search for every *.c file that contains fred keyword you must type: grep -i ‘fred’ DIR/*.c, if exists *.c in DIR/SUBDIR then you must combine find, xargs and grep

    • alwayslinuxnoob May 16, 2012 @ 20:47

      Hi. Yoander, I understand Dave … and I don´t understand you :( If I read “-R : Read all files under each directory, recursively” I think that it will walk through the actual directory and if it founds a directory it will walk through it. Yes, I came from MSWindows and I know I am missing some basic concept about what ‘recursive’ is but I can not found it.

      In other way, I don´t grasp the difference between:
      grep -i word *.c
      grep -iR word *.c


      • yoander Aug 28, 2012 @ 22:25

        I mean that Dave used a wrong syntax in order to find every *.c file that contains the fred word if you want to do that then you must type something like this:
        find . -name ‘*.x’ -print0 | xargs -0 grep fred

  • satish Jul 18, 2012 @ 8:54

    i want to list and find the file according some condition. below is the command. it is showing the files correctly. but not in the sort order. i mean in date order.

    find /somelocation/log_output -type f -ctime +40 -exec ls -l {} \;

  • Yoander Valdés Rodríguez Jul 18, 2012 @ 17:53

    You must use: ls -t instead of ls -l

  • Judi Aug 9, 2012 @ 23:38

    Looking command to list out specified files out of a directory. For example, in a directory containing 10 files which have names of fruit, I want to do an ls -l and grep for only a specific three and list perms and ownership.

    Directory is /fruit containing 10 fruit file names. i on;y want to ls -l three.

    # ls -l /fruit | grep orange, apple, banana

    Desired output should be something like:

    rwxr--r--  1 root root       392 Mar 24  2009 orange
    rwxr--r--  1 root root       392 Mar 24  2009 apple
    rwxr--r--  1 root root       392 Mar 24  2009 banana

    Unable to find the syntax to do this.

    Any ideas would be appreciated.


  • Yoander Valdés Rodríguez Aug 10, 2012 @ 13:34

    # ls -l /fruit | egrep 'orange|apple|banana'

  • Perfect Solution Mar 6, 2013 @ 19:28

    This is the best solution ever, because there are only matching lines in the output.

  • Thanh Le Jan 26, 2017 @ 17:23

    This solution meets my expectation. Great work!

  • J Bao Mar 4, 2021 @ 14:49

    find command combined with grep option solves my problem. i appreciate your tutorial

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