How to redirect stdout and stderr to the same file - grep

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

Related

How can I list only directory names, with no trailing "/"?

by doing the following command in the folder
ls -d */ | cut -f1 -d'/'
I get entries like:
env1
env2
env3
env4
how I can use cat/grep or yq/jq or any other alternative command(s) instead of the above command?
for dir in */; do
echo "${dir%/}"
done
There are several options. You can use the tree command with the options:
# d: list only directories
# i: no print of indention line
# L: max display depth of the directory tree
tree -di -L 1 "$(pwd)"
Or you can also use the grep command to get the directories and the command awk:
# F: input field separator
# $9: print the ninth column of the output
ls -l | grep "^d" | awk -F" " '{print $9}'
Or you can use the sed command to remove the slash:
# structure: s|regexp|replacement|flags
# g: apply the replacement to all matches to the regexp, not just the first
ls -d */ | sed 's|[/]||g'
I found this solutions in this post.

Grep redirection is pulling more information that I want in log.txt

I want the output of the sed file edit to go into my log file name d_selinuxlog.txt. Currently, grep outputs the specified string as well as 3 other strings above and below in the edited file.
#!/bin/bash
{ getenforce;
sed -i s/SELINUX=enforcing/SELINUX=disabled /etc/selinux/config;
grep "SELINUX=*" /etc/selinux/config > /home/neb/scropts/logs/d_selinuxlog.txt;
setenforce 0;
getenforce; }
I want to be seeing just SELINUX=disabled in the log file
All the lines with the lines SELINUX are going to match, even the commented ones, so, you need to omit that ones, and the * from the match.
grep "SELINUX=" /etc/selinux/config | grep -v "#"
This is my output
17:52:07 alvaro#lykan /home/alvaro
$ grep "SELINUX=" /etc/selinux/config | grep -v "#"
SELINUX=disabled
17:52:22 alvaro#lykan /home/alvaro

Redirection of stdout and stderr to file not working on raspbian

I am trying to redirect STDOUT and STDERR to a log file on raspberry pi.
My .sh script contains this line
sudo ./main.py &> client.log &
The script runs correctly as it transfers data to and from my server, but client.log file remains empty. I tried &>; &>>; >> with 2>&1; and |&. None of them write any data to client.log.
sudo ./main.py
produces both stdout and stderr output. What am I doing wrong?
The syntax you are looking for is:
sudo ./main.py > client.log 2>&1 &
> client.log redirects standard output to the file client.log
2>&1 redirects stderr to stdout
& at the end of the line runs this in the background so you can continue working at the command prompt.
Note: if you log off while the background command is running, it will be killed. You can override this behavior by adding nohup to the beginning of the line. For more information google bash jobs
Edited to add additional information after comment below
revised syntax:
sudo stdbuf -o L -e L ./main.py > client.log 2>&1 &
stdbuf modifies the default linux output buffering
-o L Flushes stdout at the end of every line
-e L Flushes stderr at the end of every line
python -u test.py > output.txt &
Python will buffer your output by default, and simply killing the script doesn't immediately flush that standard output to disk

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

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.

Piping shasum to grep, but grep returning all lines of piped input, even ones that don't match

I'm trying to script the download of the node.js source and corresponding SHASUMS256.txt, checksum it, grep for OK, and return no results just exit code 0 on success using grep's -q flag:
wget http://nodejs.org/dist/latest/node-v0.10.26.tar.gz
wget http://nodejs.org/dist/latest/SHASUMS256.txt
sha256sum -c SHASUMS256.txt|grep -q OK
However, grep is returning a selection of the non-matching lines "no such file or directory" errors (though not all, confusingly):
> sha256sum -c SHASUMS256.txt|grep -q OK
sha256sum: node-v0.10.26-darwin-x64.tar.gz: No such file or directory
sha256sum: node-v0.10.26-darwin-x86.tar.gz: No such file or directory
sha256sum: node-v0.10.26-linux-x64.tar.gz: No such file or directory
sha256sum: node-v0.10.26-linux-x86.tar.gz: No such file or directory
sha256sum: node-v0.10.26-sunos-x64.tar.gz: No such file or directory
sha256sum: node-v0.10.26-sunos-x86.tar.gz: No such file or directory
sha256sum: node-v0.10.26-x86.msi: No such file or directory
sha256sum: node-v0.10.26.pkg: No such file or directory
sha256sum: node.exe: No such file or directory
Any idea what the problem is here? All I want from this script is return code 0 if the checksum succeeds (eg grep matches OK), or return code non-0 if it fails.
When you pipe the output of a command as input to other command, only stdout of first command is passed as stdin to the second command.
The lines you see are sent by the sha256sum program to stderr.
You can verify that by sending stderr of sha256sum command also to grep by
sha256sum -c SHASUMS256.txt 2>&1 |grep -q OK
Hope that helps.

Resources