grep and don't show lines that containa a certain string - grep

I have the following command and output
$ find . -iname '*custom_layout_width*'
find: ‘./etc/apps/learned/local’: Permission denied
./etc/apps/search/appserver/static/custom_layout_width.js
find: ‘./etc/apps/search/local’: Permission denied
./etc/apps/simple_xml_examples/appserver/static/custom_layout_width.js
find: ‘./etc/apps/simple_xml_examples/local’: Permission denied
find: ‘./etc/apps/splunk_management_console/lookups’: Permission denied
I would like not to show lines that DO NOT contain the string Permission.
How can I do this?
I was thinking of using a grep, bu i am obviously not that good.
$ find . -iname '*custom_layout_width*' | grep '!Permission'
EDIT1 found this here but using -v does not seem to work for me

You can redirect the error to /dev/null.
find . -iname '*custom_layout_width*' 2>/dev/null
Following will not work ,
find . -iname '*custom_layout_width*' | grep '!Permission'
Because, pipe is get the input from stdout and pass it as input to the next command. But here Permission Denied will come in stderr. So piping will not work.
For that you can do like this,
find / -iname "hi" 2>&1 | grep -v "Permission"
2>&1 It is merging the stderr to stdout. Now the stderr content will be placed in the stdout.

Related

How to redirect stdout and stderr to the same file

How to redirect both standard output and standard error to the same file?
so if there is a command like grep "ABC" /etc/passwd > output
this only directs the standard output to this file
whereas grep "ABC" /etc/passwd 2> output redirects the standard error to output file
Is there a command that can take care of both stdout and stderr and direct it to the same file
To redirect both std error and std output, you can try any of these;
$ grep "ABC" /etc/passwd &> output
$ grep "ABC" /etc/passwd >& output
$ grep "ABC" /etc/passwd > output 2>&1

Filter lines using grep -v

I am trying to filter out lines that don't contain the file names as below using the following command but I am not sure why line with permission denied keeps coming in my result. It should be gone when I have used grep -v "total|denied".
wc -l *.* | egrep -v "total|denied" | sort -nr -k1,1
wc: host.save: Permission denied
33301 apache-maven-3.5.3-bin.tar.gz
14149 jenkins-cli.jar
240 examples.desktop
19 list.py
19 interview_GL.sh
17 lines.txt
7 number.py
Only stdout gets passed to the pipe into grep but those error messages are on stderr
You can either forward stderr to /dev/null or send them to stdout aswell
Send errors to /dev/null:
wc -l * 2>/dev/null
Redirect errors to stdout:
wc -l * 2>&1 | grep -v dir
You obviously aren't allowed to read host.save file's contents, therefore the error coming from the first command.
Have you tried muting the errors instead?
wc -l *.* 2>/dev/null | egrep -v "total|denied" | sort -nr -k1,1

How to grep for filenames found by find in other files?

How can I grep for the result of find within another pattern?
That's how I get all filenames with a certain pattern (in my case ending with "ext1")
find . -name *ext1 -printf "%f\n"
And then I want to grep for these filenames with another pattern (in my case ending on "ext2"):
grep -r '[filname]' *ext2
I tried with
find . -name *ext1 -printf "%f\n" | xargs grep -r *ext2
But this only makes grep tell me that it can not find the files found by find.
You would tell grep that the patterns are in a file with the -f option, and use the "stdin filename" -:
find ... | grep -r -f - *ext2

find grep exclude some file names for dos2unix

so far I have gotten this far:
prompt$ find path/to/project -type f | grep -v '*.ori|*.pte|*.uh|*.mna' | xargs dos2unix 2> log.txt
However, the files with extensions .ori, .pte, .uh and .mna still show up.
It is better to leave the excluding to find, see Birei's answer.
The problem with your grep pattern is that you have specified it as a shell glob. By default grep expects basic regular expressions (BRE) as its first argument. So if you replace your grep pattern with: .*\.\(ori\|pte\|uh\|mna\)$ it should work. Or if you would rather use extended regular expressions (ERE), you can enable them with -E. Then you can express the same exclusion like this: .*\.(ori|pte|uh|mna)$.
Full command-line:
find . -type f | grep -vE '.*\.(ori|pte|uh|mna)$'
One way:
find path/to/project *.* -type f ! \( -name '*.ori' -o -name '*.pte' -o -name '*.uh' -o -name '*.mna' \)
| xargs dos2unix 2> log.txt

find with grep not returning results

The first line returns dozens of finds. The second line returns nothing.
sudo find /var/www/ . -type 'f' -name index.php | grep "php"
sudo find /var/www/ . -type 'f' -name index.php | grep "require"
There are files called index.php that contain both text strings. Why doesn't the second line seem to work?
You want to be calling xargs before piping to grep. What you have is grep searching the results of find, not the contents of the files that find has found.
sudo find /var/www/ . -type 'f' -name index.php | xargs grep "php"
If you do lots of searching like this, you may want to look at ack. For instance, to search all the PHP files in /var/www for "require", you would use:
ack --php require /var/www

Resources