0

I wanted to write a command that would help me fetch recursively in a folder all filenames that have a particular text in them . Suppose my folder contains lot of files two of them being largest_pallindrome_subsequence_1.cpp and largest_pallindrome_subsequence_2.cpp . Now I want to find files which have sub in it . So the search should return me these 2 cpp files as mentioned above. The thing is that I also want to look for a file with particular extension say .txt or .cpp .

I tried using grep --include=\*{.cpp} -rnw . -e "sub" but this doesnot work for me.

5
  • What are those {} doing in that include argument? Commented Feb 18, 2015 at 19:52
  • Are you looking for files that contain that string in the filename, or in the file contents? Commented Feb 18, 2015 at 19:53
  • I am looking for filename and not the string in file contents Commented Feb 18, 2015 at 19:59
  • {} just so i can include more extension say in case i want to search for .cpp and also .h file Commented Feb 18, 2015 at 20:00
  • You can't use {} as a shell expansion without a comma or they stay literal (try echo {foo,bar} versus echo {foo}). Does that command work without them? Commented Feb 18, 2015 at 20:05

1 Answer 1

1

You can do:

find ./ -name "*sub*" 

or:

find ./ | grep "sub" 
Sign up to request clarification or add additional context in comments.

4 Comments

How do i get a filename of a particular extension here ? say .cpp or .txt or .h
@Invictus With find . -name "*sub*.cpp", and then spending some time reading up on shell globbing patterns...
When you want both mysub.xxx and different.cpp, use egrep "sub|.cpp"
No, just use -o for "or": find . \( -name '*sub*.cpp' -o -name '*.cpp' \)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.