Find Command that returns line number of the string - linux

I have a bunch of files organized into directories..All these are text files (c/c++). I am trying to understand this code and i need to look at the declarations of many variables..How can i use find command to get the exact location( File Name with line number(s) ) using Find command in ubuntu linux?? Or is there any graphical tool for doing the same?

You can do this with grep. grep -n 'search-term' *.c will give you the filename and line number where the term appears.

find . -name *.c -exec grep -Hn "your search term here" {} \;
If you really want to use find.
EDIT
explanation
find . -name *.c - find files in current dir and below where name is like *.c
-exec - execute command that follows
grep -Hn - grep and print results with file name and line number of match
{} \; - {} marks where the name of each file found will be substituted and the backslash-
semicolon marks the end of the command to execute.

Related

How to find filename in file content with bash script?

I wrote this command in my shell but it's not doing what I wanted and I can't figure out what I did wrong. I want to get files from current directories which include their names in their content.
find -type f -exec grep -il {} +
I understand find -type f gives me standard files from the directory and -exec executes following grep command with pattern - which is filename - on given file ('+'). Am I right? Because it seems like I don't understand something since it's not finding my file specially created for this purpose.
on given file ('+')
+ executes the command on all found files. The {} is replaced by the list of files in current directory.
Debug with -exec echo grep -il {} +.
You want:
-exec grep -Fil {} {} ';'
To search the filename {} as a pattern in the file named {}. The ';' terminates the command.
I also added -F to interpret pattern literally.
File in current directory test.txt
Content of file includes "test"
find . -name 'test.txt' -exec grep -i 'test' {} \;

Regarding searching a keyword in all files in particular directory in linux

I want to search a word suppose "abcd" in all the files(Including hidden and all possible files) in dir suppose /home/john/?
This is what I tried, I am running the below command and its getting stuck for more than 24 hours.
command --> find /home/john -type f -exec grep -iH 'abcd' {} \;
Result something which will show all the files which have this particular word or any file which is have the name as our search word.
Thanks
What about using grep recursion option ?
grep -r abcd /home/john

Search a word into all makefiles linux from terminal

I'm using Ubuntu. I have to search the word "send" into multiple makefiles.
Suppose that all makefiles are into /home/mypath. I tried this command but doesn't work. Could you please help me? Thanks in advance.
find /home/mypath/ -name Makefile && grep -r "send"
You can use the below command to find for send keyword in all the Makefiles
recursively.
find /home/mypath -name "Makefile" | xargs grep -r "send"
Here the find command list all the files with name Makefile under the specified directory. xargs command will pass all the files listed with serially to the grep command to search for the string send
If your base path is the same, you can use grep only:
grep -rn --color "send" /home/mypath/Makefile
The find command has an option to transfer the found file name to an exec command which is here grep. Important is that you give {} which will be replaced with the filename found by find. You need also the \; as end mark of your command given for the ' -exec' option for find.
find /home/mypath -name Makefile -exec grep "what you search" {} \;

for searching between specific lines in a file with only FIND command in linux command

i want to use only FIND command in linux and search between specific lines in a file for example find a word "hello" between lines 2 and 4 of a file named test.txt,please help.
By this way you can use grep command but not using find for sure.
grep -w "hello" test.txt
The following example uses find to display all .txt files containing the word "hello":
find -name '*.txt' -exec grep -q hello \{\} \; -a -exec echo \{\} \;
You might be able to modify the grep expression to only match where the targeted word is between certain lines.
If your teacher is asking you not to find files which have this match but is instead asking you to match within a specific known file then they have misinformed you - find is most certainly not the command for that job, making your question unanswerable.

Linux : Search for a Particular word in a List of files under a directory

I have a big list of log files in a particular directory , related to my java Application under my Linux Remote Servers .
When i do ls on that particular directory it shows a list of files (nearly 100 files )
Now in that List of files , i need to find out a particular word , please tell me , how can i do this ??
The problem is that I cannot open each and every file and search for that word using /
Please tell me how can i search for a word in the list of files provided .
You can use this command:
grep -rn "string" *
n for showing line number with the filename
r for recursive
grep is made for this.
Use:
grep myword * for a simple word
grep 'my sentence' * for a literal string
grep "I am ${USER}" * when you need variable replacement
You can also use regular expressions.
Add -r for recursive and -n to show the line number of matching lines.
And check man grep.
This is a very frequent task in linux. I use grep -rn '' . all the time to do this. -r for recursive (folder and subfolders) -n so it gives the line numbers, the dot stands for the current directory.
grep -rn '<word or regex>' <location>
do a
man grep
for more options
also you can try the following.
find . -name '*.java' -exec grep "<yourword" /dev/null {} \;
It gets all the files with .java extension and searches 'yourword' in each file, if it presents, it lists the file.
Hope it helps :)
You could club find with exec as follows to get the list of the files as well as the occurrence of the word/string that you are looking for
find . -exec grep "my word" '{}' \; -print
use this command
grep "your word" searchDirectory/*.log
Get more on this link
http://www.cyberciti.biz/faq/howto-recursively-search-all-files-for-words/
You are looking for grep command.
You can read 15 Practical Grep Command Examples In Linux / UNIX for some samples.

Resources