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

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.

Related

Simple Bash Script that recursively searches in subdirs for a certain string

i recently started learning linux because a ctf contest is coming in the next months. The problem that I struggle with is that i am trying to make a bash script that starts from a directory, checks if the content is a directory or other kind of file. If it is a file,image etc apply strings $f | grep -i 'abcdef', if it is a directory cd to that directory and start over. i have c++ experience and i understand the logic but i can't really make it work.I can't succesfully implement the loop that goes thru all the subdirectories. All help would be appreciated!
you don not need a loop for this implementation. The find command can do what you are looking after.
for instance:
find /home -type f -exec sh -c " strings {} | grep abcd " \;
explain:
/home is you base directory can be anything
-type f: means a regular file
-exec from the man page:
"Execute command; true if 0 status is returned. All
following arguments to find are taken to be arguments to
the command until an argument consisting of ;' is encountered. The string {}' is replaced by the current
file name being processed everywhere it occurs in the
arguments to the command, not just in arguments where it
is alone, as in some versions of find. Both of these
constructions might need to be escaped (with a `') or
quoted to protect them from expansion by the shell. See
the EXAMPLES section for examples of the use of the -exec
option. The specified command is run once for each
matched file. The command is executed in the starting
directory. There are unavoidable security problems
surrounding use of the -exec action; you should use the
-execdir option instead."
If you want to just find the string in a file and you do not HAVE TO first find a directory and then a file and then search, you can just simply find the text with grep.
Go to the the parent directory and execute :
grep -iR "abcd"
Or from any place,
grep -iR "abcd" /var/log/mylogs/
Suggesting a grep command on find filter results:
grep "abcd" $(find . -type f)

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

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.

Find Command that returns line number of the string

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.

Resources