How to grep specific *name* in subfolders in Linux? - linux

To grep some string in all subfolders I can use:
grep -rl "test" /path
I would like to grep some specific string assuming that the first part and the second part of the string could be different (has got different begin and end) and is case insensitive.
I tried to do something like this, but it doesn't work:
grep -rli "*test*" /path
Could you please tell me how to do this?
To be clear I would like to find all files (grep) including subfolders, where there is specific string, like e.g.:
"*test*"

You should try grep accepting perl regex:
grep -irlP ".*test.*" /path
The .* means matching (zero or more times) any characters except newline. More about meta-characters here
You can use almost any regex, so it's a really flexible solution to match complex patterns. For instance, if you searched for entire words only:
grep -rlP "\b(test)\b" /path
Of course there are limitations. For instance, multiline search with this method can be difficult.

You can try like this using the "--include" option:
grep -rli --include="test*" /path
From the man page of grep
--include=GLOB
Search only files whose base name matches GLOB (using wildcard
matching as described under --exclude).

grep -ilr "string" path/to/subdir
Will search sub-strings as well. grep searches pattern - you don't need those asterisks (or dot asterisks as in my pre-edit answer).
If you want to search the exact word and not its presence in sub-strings, use -w option.

Related

Find files whose content match a line from text file

I have a text file - accessions.txt (below is a subset of this file):
KRO94967.1
KRO95967.1
KRO96427.1
KRO94221.1
KRO94121.1
KRO94145.1
WP_088442850.1
WP_088252850.1
WP_088643726.1
WP_088739685.1
WP_088283155.1
WP_088939404.1
And I have a directory with multiple files (*.align).
I want to find the filenames (*.align) which content matches any line within my accessions.txt text file.
I know that find . -exec grep -H 'STRING' {} + works to find specific strings (e.g replacing STRING with WP_088939404.1 returns every filename where the string WP_088939404.1 is present).
Is there a way to replace STRING with "all strings inside my text file" ?
Or
Is there another (better) way to do this?
I was trying to avoid writing a loop that reads the content of all my files as there are too many of them.
Many thanks!
You're looking for grep's -f option.
find . -name '*.align' -exec grep -Fxqf accessions.txt {} \; -print
grep can take a list of patterns to match with -f.
grep -lFf accessions.txt directory/*.align
-F tells grep to interpret the lines as fixed strings, not regex patterns.
Sometimes, -w is also needed to prevent matching inside words, e.g.
abcd
might match not only abcd, but also xabcd or abcdy. Sometimes, preprocessing the input list is needed to prevent unwanted matching if the rules are more complex.

GREP - Searching for specific string going backwards

I would like to search file.txt with grep to locate a url ending with. ".doc". When it finds .doc, I want grep to go backwards and find "http://" at the begining of that string.
The output would be http://somesite.com/random-code-that-changes-daily/somefilename.doc
There is only 1 .doc url on this page, so multiple search results should not be an issue.
Please excuse, I am a novice. I did locate the answer at one time but search for 1 hour and can no longer find. I am willing to read and learn but I do not think I'm using the correct search terms for what I want to do. Thank you.
You can use regular expressions,
with the marker ^ you can indicate the start of the line you are looking for.
with the marker $ you can indicate the end of the line you are looking for.
then, you can do something like
grep '^http:\\' \ '.doc$' file.txt
or
grep '^http://\|.doc$' file.txt
or not using regular expressions but just a matching pattern with wildcards as #choroba suggested:
grep 'http://.*\.doc' file.txt
You can also search for http:// and print the line if it contains .doc somewhere after it:
grep 'http://.*\.doc' file.txt
If you want to only print the matching part, use the -o option (if your version of grep supports it).

search for files with pattern [A-Z] in unix

I want to search files matching with the pattern [A-Z].
I created some files like test.txt,MANUAL.txt,etc in a directory. Now, I want to filter the files according to the search pattern [A-Z]
Example: suppose I have a file which has following content :
A SCRAPER sITe is a WEBsite that features content123 stolen from other sites and presented as original.
I want my search results matching the pattern [A-Z].
EDIT: only "SCRAPER" should show up in the search results
If you're looking for uppercase words in a file:
grep -o '\<[[:upper:]]\+\>' file
If you're looking for which files in this directory contain an uppercase word:
grep -lo '\<[[:upper:]]\+\>' *
You can use grep like this:
grep '[A-Z]' *
to search all lines matching any upper case letter.
EDIT: In order to search lines only with uppercase letters use:
egrep '^[A-Z]+$' file
OR to grep only uppercase words:
grep -Po '\b[A-Z]+\b' file

Looking for tool to search text in files on command line

Hello
I'm looking some script or program that use keywords or pattern search in files ex. php, html, etc and show where is this file
I use command cat /home/* | grep "keyword"
but i have too many folders and files and this command causes big uptime :/
I need this script to find fake websites (paypal, ebay, etc)
find /home -exec grep -s "keyword" {} \; -print
You don't really say what OS (and shell) you are using. You might want to retag your question to help us out.
Because you mention cat | ... , I am assuming you are using a Unix/Linux variant, so here are some pointers for looking at files. (bmargulies solution is good too).
I'm looking some script or program that use keywords or pattern search in files
grep is the basic program for searching files for text strings. Its usage is
grep [-options] 'search target' file1 file2 .... filen
(Note that 'search target' contains a space, if you don't surround spaces in your searchTarget with double or single quotes, you will have a minor error to debug.)
(Also note that 'search target' can use a wide range of wild-card characters, like .,?,+,,., and many more, that is beyond the scope of your question). ... anyway ...
As I guess you have discovered, you can only cram so many files at a time into the comand-line, even when using wild-card filename expansion. Unix/linux almost always have a utiltiyt that can help with that,
startDir=/home
find ${startDir} -print | xargs grep -l 'Search Target'
This, as one person will be happy to remind you, will require further enhancements if your filenames contain whitespace characters or newlines.
The options available for grep can vary wildly based on which OS you are using. If you're lucky, you type the following to get the man page for your local grep.
man grep
If you don't have your page buffer setup for a large size, you might need to do
man grep | page
so you can see the top of the 'document'. Press any key to advance to the next page and when you are at the end of the document, the last key press returns you to the command prompt.
Some options that most greps have that might be useful to you are
-i (ignore case)
-l (list filenames only (where txt is found)
There is also fgrep, which is usually interpretted to mean 'file' grep
becuase you can give it a file of search targets to scan for, and is used like
fgrep [-other_options] -f srchTargetsFile file1 file2 ... filen
I need this script to find fake websites (paypal, ebay, etc)
Final solution
you can make a srchFile like
paypal.fake.com
ebay.fake.com
etc.fake.com
and then combined with above, run the following
startDir=/home
find ${startDir} -print | xargs fgrep -il -f srchFile
Some greps require that the -fsrchFile be run together.
Now you are finding all files starting /home, searching with fgrep for paypay, ebay, etc in all files. The -l says it will ONLY print the filename where a match is found. You can remove the -l and then you will see the output of what is found, prepended with the filename.
IHTH.

How to use grep with regex to find all the files containing a 'String'?

I am using SSH secure shell in Unix, I want to search file with some 'TEXT' or string
and that string is like 'PACKAGE', followed by '%PA%EVENTS%'('PA_PACKAGE %PA%EVENTS%')
this % i am assuming as in SQL*PLUS, means i a string PA can occur any where before EVENTS
and EVENTS can occur any where after PA,
I want to search those file which this REGEX matches.
I ma not sure exactly what you are hoping to match, but the following will print out the lines that contain the text "PACKAGE" followed but zero or more characters and then the text "EVENTS", from all the files in the current directory.
grep "PACKAGE.*EVENTS" *
Please give some more examples of what you want, so I can be more specific.
grep -l "PACKAGE*EVENTS" *
recursive:
grep -Rl "PACKAGE*EVENTS" *
try this: egrep -e "PACKAGE PA*EVENTS" file

Resources