Find files containing text using grep command starts with file name charector - linux

I want find the file containing some text but that should start with some character.
grep -inr "my text" .
The above command will show all the files containing the above text. But what I want is if the file contains a text and the name should starts with like E*.

You can use this,
find . -name 'E*' -exec grep -Hl "sample" {} \;
Explanation:
-H : Print the file name for each match.
-l : Suppress normal output

You can combine find and grep:
find . -name "E*" | xargs grep -nH "my text"
You can also use finds exec parameter instead of xargs. Take a look at its man mange for this: man find

If you want a max-depth for 1 layer, then i think most efficient way would be...
grep <pattern> E*
for multiple levels you can use like this
grep <pattern> */*/E*

Related

How to know which file holds grep result?

There is a directory which contains 100 text files. I used grep to search a given text in the directory as follow:
cat *.txt | grep Ya_Mahdi
and grep shows Ya_Mahdi.
I need to know which file holds the text. Is it possible?
Just get rid of cat and provide the list of files to grep:
grep Ya_Mahdi *.txt
While this would generally work, depending on the number of .txt files in that folder, the argument list for grep might get too large.
You can use find for a bullet proof solution:
find --maxdepth 1 -name '*.txt' -exec grep -H Ya_Mahdi {} +

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.

Search and replace entire files

I've seen numerous examples for replacing one string with another among multiple files but what I want to do is a bit different. Probably a lot simpler :)
Find all the files that match a certain string and replace them completely with the contents of a new file.
I have a find command that works
find /home/*/public_html -name "index.php" -exec grep "version:1.23" '{}' \; -print
This finds all the files I need to update.
Now how do I replace their entire content with the CONTENTS of /home/indexnew.txt (I could also name it /home/index.php)
I emphasize content because I don't want to change the name or ownership of the files I'm updating.
find ... | while read filename; do cat static_file > "$filename"; done
efficiency hint: use grep -q -- it will return "true" immediately when the first match is found, not having to read the entire file.
If you have a bunch of files you want to replace, and you can get all of their names using wildcards you can try piping output to the tee command:
cat my_file | tee /home/*/update.txt
This should look through all the directories in /home and write the text in my_file to update.txt in each of those directories.
Let me know if this helps or isn't what you want.
I am not sure if your command without -l and then print it is better than to add -l in grep to list file directly.
find /home/*/public_html -name "index.php" -exec grep -l "version:1.23" '{}' \; |xargs -i cp /home/index.php {}
Here is the option -l detail
-l, --files-with-matches
Suppress normal output; instead print the name of each input
file from which output would normally have been printed. The
scanning will stop on the first match. (-l is specified by
POSIX.)

How can I search multiple files for a single word or phrase using grep and strings?

Im trying to look for a word like "numbers" in multiple files not just txt files using terminal. I have tried strings -r /media/E016-5484/* | grep numbers But it still doesn't work !
let say you are looking for 1234 in all files which in name contain file_pattern
grep 1234 ` find . -name "*file_pattern*"`
or
find . -name "*file_pattern*" -exec grep 1234 {} \;
If I am not mistaken, you are looking for
grep numbers -r /media/E016-5484
From the manpage:
-r, --recursive
Read all files under each directory, recursively, following symbolic links only if they are on the command line. This is equivalent to the -d recurse option.

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