How to find a file containing specific text in Centos7? - linux

I have a server which runs on centos 7. I need to find a file containing 0774386850 so that I can replace with another string. Kindly give me a Linux command to give me that file

By using grep command, you can achieve what you expected
grep -rlnw '/path/to/somewhere/' -e '0774386850'
-r or -R is recursive,
-n is line number, and
-w stands for match the whole word.
-l (lower-case L) file name of matching files.

grep -r "0774386850 " /path/to/dir

You can use either grep/fgrep or find to achieve your goal.

Related

Wildcard in sed command to replace string not working

I'm trying to use the sed command in terminal to replace a specific line in all my text files with a certain extension by a specific string:
sed -i.bak '35s/^.*$/5\) 1\-4/' fitting_file*.feedme
So I am trying to replace line 35 in each of these files with the string "5) 1-4". When I run an ls fitting_file*.feedme | wc -l command in this directory, I get 221 files. However, when I run the above sed command, it only edits the FIRST file in the order of ls fitting_file*.feedme. I know this because grep '5) 1-4' fitting_file*.feedme continually only returns the first file on the list after I run the replacement command. I also tried replacing fitting_file*.feedme with a space-separated list of a couple of these files in my sed command as a test, but it still only operated on the one I chose to list first. Why is this happening?
sed operates on a single stream. It essentially concats all the files together and treats that as a single stream. So it replaces the 35th line of the big concatenated stream.
To see this, make a 20 line file called A and a 20 line file called B. Apply your sed command as
sed -i.bak '35s/^.*$/5\) 1\-4/' A B
and you will see the 15th line of B replaced.
I think this should answer your direct question. As far how to get done what you like, I assume you've already figured out that wrapping your sed command in a for is one way to do it. :)
Try
Create a file containing your sed instruction like this
#!/bin/bash
sed -i.bak '35s/^.*$/5\) 1\-4/' $1
exit 0
and call it prog.sh. Next make it executable :
chmod u+x prog.sh
now you can solve your problem using
find . -name fitting_file\*.feedme -exec ./prog.sh {} \;
You could do all this on one line but frankly the number of escapes required is a bit much. Good luck.
To do what you're trying to do without using a shell loop is:
awk -i inplace -v inplace::suffix=.bak 'FNR==35{$0="5) 1-4"}1' fitting_file*.feedme
Note that unlike sed which can just count lines across all input files, awk has NR to track the number of records (lines by default) across all files and FNR for the same but just within the current file.
The above uses GNU awk for inplace editing just like GNU sed has -i for that. The default awk on MacOS is BSD awk, not GNU awk, but you should install GNU awk as it doesn't have all the bugs/quirks that BSD awk does and it has a ton of extremely useful extensions.
If you just want to use MacOS's awk then it'd be something like:
find . -name 'fitting_file*.feedme' -exec sh -c "\
awk 'FNR==35{\$0=\"5) 1-4\"}1' \"\$1\" > \"\$1.bak\" &&
mv -- \"\$1.bak\" \"\$1\"
" sh {} \;
which is obviously getting kinda complicated - I'd probably put the awk+mv script in a file to execute from sh -c or just resort to a shell loop myself if faced with that alternative (or a similar quoting nightmare with xargs)!

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.

Redirect argument from a file to a Linux command

I searched the Internet, but maybe I used the wrong keyword, but I couldn't find the syntax to my very simple problem below:
How do I redirect a file as command line arguments to the Linux command "touch"? I want to create a file with "touch abc.txt", but the filename should come from the filename "file.txt" which contains "abc.txt", not manually typed-in.
[root#machine ~]# touch < file.txt
touch: missing file operand
Try `touch --help' for more information.
[root#machine ~]# cat file.txt
abc.txt
Try
$ touch $(< file.txt)
to expand the content of file.txt and give it as argument to touch
Alternatively, if you have multiple filenames stored in a file, you could use xargs, e.g.,
xargs touch <file.txt
(It would work for just one, but is more flexible than a simple "echo").

from Linux command line, find number of lines in which a string occurs

I have a file in the location /home/someuser/sometext.txt . I want to count the number of lines in which a particular string occurs. What's the way to do that from Linux command line?
grep with -c switch is what you need:
grep -c "pattern" /home/someuser/sometext.txt
Alternate solution using awk:
awk '/regex/{c++}END{print c+0}' /home/someuser/sometext.txt
You're looking for the grep command. Here's a basic tutorial. It's extremely useful for string searching in files. It also has support for regular expressions.
It looks like you'll do something like this:
grep -c "mystring" /home/someuser/sometext.txt
The -c argument is short for --count and tells grep to print out the number of lines that contain the string.

find string from the file in somewhere

I want to find a string from some file in subdirectory.
Like
we are in bundle/.
and in bundle/ there are multiple subdirectories and multiple txt files
I want to do something like
find . -type f -exec grep "\<F8\>" {} \;
want to get the file where it contain string < F8 >
this command does work, find the string, but never return filename
I hope anyone can give me a better solution to this, like display filename along with the line containing that string
grep -rl '<F8>' .
The -r option tells grep to search recursively through directories starting at .
The -l option tells it to show you just the filename that's matched, not the line itself.
Your output will look something like this:
./thisfile
./foo/bar/thatfile
If you want to limit this to only one file, append | head -1 to the end of the line.
If you want output like:
./thisfile:My text contains the <F8> string
./foo/bar/thatfile:didn't remember to press the <F8> key on the
then you can just leave off the -l option. Note that this output is not safe to parse, as filenames may contain colons, and colons in filenames are not escaped in grep's output.
You can use grep by itself.
grep -r '<F8>' .
This should list out all the files and line numbers that match:
grep -nR '<F8>' *
Personally I find it's easier just to use ack. (ack-grep is the name used in Ubuntu's repos to keep from confusing it with another software with the same name.) It's available in most major repositories.
The command would be ack -a "<F8>" (or ack-grep -a "<F8>" in Ubuntu). The -a option is to search all file types.
Example:
testfile
Line 1
Line 2
Line 3
<F8>
<F9>
<F10>
Line 4
Line 5
Line 6
Output:
$ ack -a "<F8>"
testfile
4:<F8>

Resources