I'm running a command like:  parallel --spreadstdin --line-buffered 'some_command 2&gt; `mktemp --tmpdir /tmp/stderr`' | do_something   The trick is that parallel creates a lot of processes and they all get a stderr file, most of which is uninteresting because they're empty. How can I make my shell generate a stderr file only if any standard error output actually happened?
59943
How can I rewrite this command to only email if there is output from the mailq | grep?  mailq | egrep 'rejected|refused' -A 5 -B 5 | mail -s 'dd' email@email   Is this even possible on one line?  See  for a more general case than sending email.
99936
In Perl, I can type:  $|++;   and anything printed to STDOUT will be automatically fflush()ed.  Is there an equivalent in C? In other words, is there some way I can tell stdio to automatically fflush stdout after every printf(), the way it automatically flushes stderr?
83278
Edit: Since it appears that there's either no solution, or I'm doing something so non-standard that nobody knows - I'll revise my question to also ask: What is the best way to accomplish logging when a python app is making a lot of system calls?  My app has two modes.  In interactive mode, I want all output to go to the screen as well as to a log file, including output from any system calls.  In daemon mode, all output goes to the log.  Daemon mode works great using os.dup2().  I can't find a way to "tee" all output to a log in interactive mode, without modifying each and every system call.    In other words, I want the functionality of the command line 'tee' for any output generated by a python app, including system call output.  To clarify:   To redirect all output I do something like this, and it works great:  # open our log file so = se = open("%s.log" % self.name, 'w', 0)  # re-open stdout without buffering sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)  # redirect stdout and stderr to the log file opened above os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(se.fileno(), sys.stderr.fileno())   The nice thing about this is that it requires no special print calls from the rest of the code.  The code also runs some shell commands, so it's nice not having to deal with each of their output individually as well.  Simply, I want to do the same, except duplicating instead of redirecting.  At first thought, I thought that simply reversing the dup2's should work.  Why doesn't it?  Here's my test:   import os, sys  ### my broken solution: so = se = open("a.log", 'w', 0) sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)  os.dup2(sys.stdout.fileno(), so.fileno()) os.dup2(sys.stderr.fileno(), se.fileno()) ###  print("foo bar")  os.spawnve("P_WAIT", "/bin/ls", ["/bin/ls"], {}) os.execve("/bin/ls", ["/bin/ls"], os.environ)   The file "a.log" should be identical to what was displayed on the screen.
42393
On the command line, I can redirect or pipe output of a command to a file or another command using the &gt; or | operator after the command. I have come across a less standard situation that I would like to redirect the output to a file, but I don't seem to have the oppurtunity to redirect it:  When at a new terminal,  [chiliNUT ~]$   if I press Tab without typing anything first, I get asked   display all 1725 possibilities? (y or n)   and if I then type y, I get a nice long list of different commands. How can I redirect or pipe this output to a file? I don't seem to get the chance to type &gt; myfile.txt anywhere.  Using CentOS release 6.4 (Final).
50563
I am currently looking ways to suppress error command in Linux, in particular, the command cp.   I do:   root@ubuntu:~$ cp /srv/ftp/201*/wha*/*.jj ~/. cp: cannot stat `/srv/ftp/201*/wha*/*.jj': No such file or directory   How do I suppress the error message that gets printed on the screen? I.e., I don't want to see this error message in my monitor.
99996
I'm running into issues with redirection in tcsh.  Consider the following commands: vi --version and vi --xxx.  And let's assume this is on a machine where vi supports the --version option.  The option --xxx is invalid, and therefore vim should display something via stderr.  By that reasoning, using 2&gt; /dev/null with both of these commands should give output for the valid case and no output for the invalid case.  And that is what I see in bash, zsh, ksh, and dash.  $ vi --xxx 2&gt; /dev/null $ vi --version 2&gt; /dev/null VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Oct 20 2014 16:09:17) ...   However, when I try this in tcsh, it gives me no output in both cases.  $ vi --xxx 2&gt; /dev/null $ vi --version 2&gt; /dev/null (there is no output here)   What is going on here?  Am I redirecting stderr incorrectly?  Here is the output of tcsh --version:  tcsh 6.18.01 (Astron) 2012-02-14 (i686-intel-linux) options wide,nls,dl,al,kan,rh,nd,color,filec
46255
I have a command that I run in a folder that outputs MD5 hashes and filenames on the terminal:  ls |sort -nr | xargs md5sum   I need this output in a text file that I can download and compare to another folder on another customer's machine. How can I modify the command such that its output is stored in a file in say /tmp? I'm using Redhat 5.
101309
Suppose that   dd if=/dev/sda bs=100M | gzip -c &gt; somefile.img.gz    is done. If errors occur in dd, do I get to see error messages in terminal, or do they get ignored?  Also, suppose that dd encounters a read error. Will dd just skip the block, or will it print out error messages?
16488
Currently I have a shell script which logs messages to a log file like this:  log_file="/some/dir/log_file.log" echo "some text" &gt;&gt; $log_file do_some_command echo "more text" &gt;&gt; $log_file do_other_command   When executing this script, there is no output to screen, and, since I'm connecting to the server via putty, I have to open another connection and do "tail -f log_file_path.log", because I can't terminate the running script and I want to see the output in real time.  Obviously, what I want is that the text messages are printed on screen and into file, but I'd like to do it in one line, not two lines, one of which has no redirection to file.  How to achieve this?
123071
I have the following in a script  for server in ${servers[@]}; do     echo ${server}     ssh user@${server} "for i in /tmp/foo* ; do echo ${i}; done" done   But it doesn't work. Weird thing, I see $I amount of return lines. So if I have ten files, I see ten blank lines.
107966
So, I understand that by using 'tee' I can redirect the output of a command such as 'ping' to stdout as well as a file.  For example:  &gt; ping google.com | tee somefile   This would display the ping statistics on terminal and write them to 'somefile'.  Now, if I wish to modify the output from 'ping', I can use 'cut' in such a way:  &gt; ping google.com | cut -d' ' -f 1   But, if I wish to use all the three commands together, I get a no output on stdout and an empty file.  &gt; ping google.com | cut -d' ' -f 1 | tee somefile   What am I doing wrong? Is there a better way to do this? Somehow I feel I am not using 'tee' properly. Any insight would be appreciated.  I'm using bash shell, if this is relevant.
122811
Occasionally I need to specify a "path-equivalent" of one of the standard IO streams (stdin, stdout, stderr).  Since 99% of the time I work with Linux, I just prepend /dev/ to get /dev/stdin, etc., and this "seems to do the right thing".  But, for one thing, I've always been uneasy about such a rationale (because, of course, "it seems to work" until it doesn't).  Furthermore, I have no good sense for how portable this maneuver is.  So I have a few questions:   In the context of Linux, is it safe (yes/no) to equate stdin, stdout, and stderr with /dev/stdin, /dev/stdout, and /dev/stderr? More generally, is this equivalence "adequately portable"?   I could not find any POSIX references.
49191
I was trying to redirect both stdout and stderr to a file today, and I came across this:  &lt;command&gt; &gt; file.txt 2&gt;&amp;1   This apparently redirects stderr to stdout first, and then the resulting stdout is redirected to file.txt.  However, why isn't the order &lt;command&gt; 2&gt;&amp;1 &gt; file.txt? One would naturally read this as (assuming execution from left to right) the command being executed first, the stderr being redirected to stdout and then, the resulting stdout being written to file.txt. But the above only redirects stderr to the screen.  How does the shell interpret both the commands?
44884
The time(1) command allows you to time any command, by passing it as arguments to time.  I know it's possible to redirect stderr and stdout to a file via 2&gt;&amp;1, and there's also tee(1) which allows one to copy its input to both the screen and a file.  However, I do not know how to add timestamps to logs.   Is there a tool to redirect stderr and stdout to a file, AND append timestamps to it for logging purposes?
80577
I am trying to create directory in bulk using mkdir through shell script below is the script  #!bin/bash mkdir /tmp/report*/testfolder*/testdoank   when i run it i got an error  -bash: ./makefolder.sh: bin/bash: bad interpreter: No such file or directory   what is wrong?
50616
So, I thought I had a good understanding of this, but just ran a test (in response to a conversation where I disagreed with someone) and found that my understanding is flawed...  In as much detail as possible what exactly happens when I execute a file in my shell? What I mean is, if I type: ./somefile some arguments into my shell and press return (and somefile exists in the cwd, and I have read+execute permissions on somefile) then what happens under the hood?  I thought the answer was:   The shell make a syscall to exec, passing the path to somefile The kernel examines somefile and looks at  of the file to determine if it is a format the processor can handle If the magic number indicates that the file is in a format the processor can execute, then   a new process is created (with an entry in the process table) somefile is read/mapped to memory. A stack is created and execution jumps to the entry point of the code of somefile, with ARGV initialized to an array of the parameters (a char**, ["some","arguments"])  If the magic number is a  then exec() spawns a new process as above, but the executable used is the interpreter referenced by the shebang (e.g. /bin/bash or /bin/perl) and somefile is passed to STDIN If the file doesn't have a valid magic number, then an error like "invalid file (bad magic number): Exec format error" occurs   However someone told me that if the file is plain text, then the shell tries to execute the commands (as if I had typed bash somefile). I didn't believe this, but I just tried it, and it was correct. So I clearly have some misconceptions about what actually happens here, and would like to understand the mechanics.  What exactly happens when I execute a file in my shell? (in as much detail is reasonable...)
24776
I need to spawn a child process that is a console application, and capture its output.  I wrote up the following code for a method:  string retMessage = String.Empty; ProcessStartInfo startInfo = new ProcessStartInfo(); Process p = new Process();  startInfo.CreateNoWindow = true; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardInput = true;  startInfo.UseShellExecute = false; startInfo.Arguments = command; startInfo.FileName = exec;  p.StartInfo = startInfo; p.Start();  p.OutputDataReceived += new DataReceivedEventHandler (     delegate(object sender, DataReceivedEventArgs e)     {         using (StreamReader output = p.StandardOutput)         {             retMessage = output.ReadToEnd();         }     } );  p.WaitForExit();  return retMessage;   However, this does not return anything. I don't believe the OutputDataReceived event is being called back, or the WaitForExit() command may be blocking the thread so it will never callback.  Any advice?  EDIT: Looks like I was trying too hard with the callback. Doing:  return p.StandardOutput.ReadToEnd();    Appears to work fine.