How To Find Files by Content Under UNIX

by Vivek Gite on September 21, 2008 · 16 comments

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:

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

This FAQ entry is 6 of 7 in the "Linux / UNIX grep Command Tutorial" series. Keep reading the rest of the series:
Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 16 comments… read them below or add one }

1 mhymn September 21, 2008

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

Reply

2 mhernandez September 21, 2008

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…

Reply

3 Gokdeniz Karadag September 21, 2008

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

Reply

4 vivek September 21, 2008

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

Reply

5 _ September 21, 2008

egrep -ri word location/folder/*

Reply

6 henry September 21, 2008

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.

Reply

7 mhernandez September 21, 2008

@henry

find ./bin -name “cp”

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

Reply

8 Vaishnavi September 22, 2008

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

This command should also work

Reply

9 sathiya September 22, 2008

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

Reply

10 yoander September 22, 2008

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

Reply

11 Carl November 23, 2010

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” “{}” \;

Reply

12 yoander (sedlav) November 23, 2010

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

Reply

13 Carl November 23, 2010

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

Reply

14 Vijay March 17, 2011

Thanks a lot for valuable posts.

Reply

15 Dave February 8, 2012

Tried
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?

Reply

16 yoander February 8, 2012

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

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 9 + 9 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: