Home > Back-end >  How to output data to file but also suppress output on the terminal?
How to output data to file but also suppress output on the terminal?

Time:11-12

I'm trying to execute a system command in python but I am using > /dev/null 2>&1 to hide all the output. I'm trying to just display very specific output, but I am also trying to output the data to a file > grep.txt so that I can grep the specific data out. However it seems to be going to /dev/null as nothing appears in grep.txt when I use /dev/null in the command.

I have tried the following:

#command > grep.txt > /dev/null 2>&1
#command > grep.txt | > /dev/null 2>&1
#command > grep.txt & > /dev/null 2>&1
#command > grep.txt & /dev/null 2>&1

but nothing seems to work. It's either one or the other. I just want to save the results to the grep.txt file but also hide the output on the terminal.

I have even tried just using a variable to store the results of the command whilst using > /dev/null but the variable is always empty! So I can only assume that it's going to /dev/null.

Please help! xD

Sorry for the stupid question!

CodePudding user response:

/dev/null is equivalent to writing to nothing. It has a file interface, but does not record anything. If you want to retrieve the data, write it to a file:

command > grep.txt 2>&1

Now grep.txt has all the data you were looking for. There will be no output printed to the terminal. You read redirects from left to right: stdout goes to file, stderr goes to wherever stdout is currently going. 2>&1 grep.txt would read as stderr goes to stdout (terminal), stdout goes to file, so you will see error output but normal output would go to the file.

  • Related