How do i use grep command to find a text that includes a string like /usr/vm/data/? - linux

I want to use grep command to search for files containing the string "/usr/vm/data". For searching a normal string like "how are you", i know i can do:
grep -inr "how are you" *
to search recursively. But i am getting stuck in the cases where i need to search a path like "/usr/vm/data". I tried:
grep -inr "\/usr\/vm\/data" directory1
and also
grep -inr "/usr/vm/data/" directory1
but didn't get any success.

Don't torture yourself, and it is a normal string (especially when you put it in quotes).
echo "/usr/vm/data Hello world" | grep -i "/usr/vm/data"

Your command works fine:
$ cat directory1/somefile
foo
bar
this line contains /usr/vm/data/
$ grep -inr "/usr/vm/data/" directory1
directory1/somefile:3:this line contains /usr/vm/data/
$
Perhaps your file is beyond a symlink which grep doesn't follow, or perhaps you don't have any matching files?

Related

Linux Command to search for text in particular file

I need a little help. Basically, I am trying to search a text string in wordpress theme files.
That string is located in wp-content/themes/style.css.
So basically I am trying to search a string in all style.css files on all wordpress installations of home directory.
Is it possible to search all style.css files only and if the string is found display it?
Thanks in advance!
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE or standard input.
PATTERN is, by default, a basic regular expression (BRE).
Example: grep -i 'hello world' menu.h main.c
Use grep to search for text inside files.
grep -rniw '/path/to/somewhere/' -e 'pattern'
-r or -R is recursive,
-n is line number, and
-i - case insensitive search
-w stands for match the whole word.
-l (lower-case L) can be added to just give the file name of matching files.
For you case, use:
grep -rniw '/path/to/somewhere/' -e 'wp-content/themes/style.css'
Use the following commands:
cd /home/
find . -iname "*.css" -exec grep -l "text_string" {} +
Do not forget to replace text_string with the actual text string.

Finding multiple strings in directory using linux commends

If I have two strings, for example "class" and "btn", what is the linux command that would allow me to search for these two strings in the entire directory.
To be more specific, lets say I have directory that contains few folders with bunch of .php files. My goal is to be able to search throughout those .php files so that it prints out only files that contain "class" and "btn" in one line. Hopefully this clarifies things better.
Thanks,
I normally use the following to search for strings inside my source codes. It searches for string and shows the exact line number where that text appears. Very helpful for searching string in source code files. You can always pipes the output to another grep and filter outputs.
grep -rn "text_to_search" directory_name/
example:
$ grep -rn "angular" menuapp
$ grep -rn "angular" menuapp | grep some_other_string
output would be:
menuapp/public/javascripts/angular.min.js:251://# sourceMappingURL=angular.min.js.map
menuapp/public/javascripts/app.js:1:var app = angular.module("menuApp", []);
grep -r /path/to/directory 'class|btn'
grep is used to search a string in a file. With the -r flag, it searches recursively all files in a directory.
Or, alternatively using the find command to "identify" the files to be searched instead of using grep in recursive mode:
find /path/to/your/directory -type f -exec grep "text_to_search" {} \+;

Identifying multiple file types with bash

I'm pretty sure I've seen this done before but I can't remember the exact syntax.
Suppose you have a couple of files with different file extensions:
foo.txt
bar.rtf
index.html
and instead of doing something with all of them (cat *), you only want to run a command on 2 of the 3 file extensions.
Can't you do something like this?
cat ${*.txt|*.rtf}
I'm sure there's some find trickery to identify the files first and pipe them to a command, but I think bash supports what I'm talking about without having to do that.
The syntax you want is cat *.{txt,rft}. A comma is used instead of a pipe.
$ echo foo > foo.txt
$ echo bar > bar.rft
$ echo "bar txt" > bar.txt
$ echo "test" > index.html
$ cat *.{txt,rft}
bar txt
foo
bar
$ ls *.{txt,rft}
bar.rft bar.txt foo.txt
But as Anthony Geoghegan said in their answer there's a simpler approach you can use.
Shell globbing is much more basic than regular expressions. If you want to cat all the files which have a .txt or .rtf suffix, you'd simply use:
cat *.txt *.rtf
The glob patterns will be expanded to list all the filenames that match the pattern. In your case, the above command would call the cat command with foo.txt and bar.rtf as its arguments.
Here's a simple way i use to do it using command substitution.
cat $(find . -type f \( -name "*.txt" -o -name "*.rtf" \))
But Anthony Geoghegan's answer is much simpler. I learned from it too.

Command to open a file which contains the given data

I had this question in interview.
He put a situation in front of me that there are 12 files in your Linux operating system.
Give me a command which will open a file containing data "Hello"..
I told him I just know grep command which will give you the names of files having "Hello" data.
Please tell me if there is any command to open a file in this way..
Assuming it will be only one file containing the word hello:
less $(grep -H "hello" *.txt | sed s/:.*//)
Here it is first capturing the file name using grep with -H parameter. Then using sed removing everything except the filename. And finally its using less to open the file.
Maybe this could help:
$ echo "foo" > file1.txt
$ echo "bar" > file2.txt
$ grep -l foo * | xargs cat
foo
You have 2 files, and you are looking for the one with the string "foo" in it. Change cat with your command of choice to open files. Might try vi, emacs, nano, pico... (no, another flame war!)
You may want to try a different approach if there are several files that contains the string you are looking for... Just thought of only one file containing the string.

How to use grep in a shell script to find a word inside of a file

How can I use grep to find an exact word inside of a file entered by the user as string?
For example I need to select the word I want to find and the file I want to find it in. I've been told I am really close but something is not working as it should be. I'm using bash shell under Linux.
Here's what I've done so far:
#!/bin/bash
echo "Find the file you want to search the word in?"
read filename
echo "Enter the word you want to find."
read word1
grep $word1 $filename
How can I use grep to find an exact word inside of a file entered by
the user as string.
Try using -F option. Type man grep on shell for more details.
grep -F "$word1" "$filename"
It's recommended to enclose search string and file name with quotes to avoid unexpected output because of white spaces.
Not sure why you have fi on the last line. It's not needed.
Try:
grep -R WORD ./ to search the entire current directory, or grep WORD ./path/to/file.ext to search inside a specific file.
#!/bin/bash/
echo "Find the file you want to search the word in?"
read filename
echo "Enter the word you want to find."
read word
cat $filename | grep "$word"
This works fine to find the exact word match in a file.
If you want an exact word match within lines use
grep "\b$word\b"

Resources