Linux logoA few days ago a friend of mine asked me how he could find some text which was in a file in his system. The problem was that he didn't know which file that was so he had to search his whole filesystem. So, here are a couple of simple ways to do this in Linux through command line.

As an example I have two files called distros1 and distros2. Both of them are inside a directory called test. I am going to search for the word ubuntu in all files of test directory.

$ cat test/distros1
ubuntu
fedora
archlinux
$ cat test/distros2
debian
gentoo
mint
opensuse

The first command is

find <directory to search> -type f | xargs grep -rl '<text to search for>'

Example:

$ find /home/axel/test/ -type f | xargs grep -rl 'ubuntu'
/home/axel/test/distros1

The second command which also works fine with pathnames cotaining blanks and quotes is

find <directory to search> -type f -exec grep -l ‘<text to search for>’ {} +

Example:

$ find /home/axel/test/ -type f -exec grep -l 'ubuntu' {} +
/home/axel/test/distros1

The third command is

grep -rl ‘<text to search for>’ <directory to search>/*

Example:

$ grep -rl 'ubuntu' /home/axel/test/*
/home/axel/test/distros1

That was all, a quick and sometimes useful tip!

Comments (2)

Subscribe to this comment's feed
Student
0
Good it works well!
Fazlullah Khan , January 26, 2012
works
0
Awesome, works great.
JamesB , October 09, 2012

Write comment

smaller | bigger
security image
Write the displayed characters

busy

Linux

Login Form

Follow me on...

  • Facebook
  • Twitter
  • Google+: u/0/b/113039112812192417058/
  • Digg
  • Reddit: myguides
  • RSS Feed
  • e-mail

Member Login