How To Find Files by Content Under UNIX

by Vivek Gite [Last updated: September 21, 2008]

Q. 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?

A. You need to use following tools:

[a] grep command : print lines matching a pattern.

[b] find command: search for files in a directory hierarchy.

grep command to find files by content

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

Where

  • -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 : 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
Where,

  • -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.

$grep --color=auto -nH 'DIR' *
Sample output:

Fig.01: grep command displaying searched pattern

Fig.01: grep command displaying searched pattern

You can also use find command:
$ find . -name "*.c" -print | xargs grep "main("

Further readings:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 10 comments… read them below or add one }

1 mhymn 09.21.08 at 10:10 am

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 :/

2 mhernandez 09.21.08 at 10:16 am

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…

3 Gokdeniz Karadag 09.21.08 at 10:43 am

“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.

4 vivek 09.21.08 at 10:53 am

@mhernandez,

Thanks for your post, I’ve update FAQ.

@Gokdeniz,

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

@mhymn,

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

Appreciate all of your posts.

5 _ 09.21.08 at 11:17 am

egrep -ri word location/folder/*

6 henry 09.21.08 at 5:54 pm

Hello,
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.

7 mhernandez 09.21.08 at 6:03 pm

@henry

find ./bin -name “cp”

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

8 Vaishnavi 09.22.08 at 4:18 am

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

This command should also work

9 sathiya 09.22.08 at 8:07 am

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/ \;

10 yoander 09.22.08 at 1:51 pm

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

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , , , , ,

Previous post: Linux / UNIX: Change Crontab Email Settings ( MAILTO )

Next post: FreeBSD Install mod_security For The Apache HTTPD Server