Bash Standard Error Redirect
Contents |
>20.3. Applications
Bash Pipe Standard Error
file. # Creates the file if not present, otherwise overwrites it. ls -lR > dir-tree.list # Creates a file containing a listing of the directory tree. : > filename # The > truncates file "filename" to zero length. # If file not present, creates zero-length file (same effect as 'touch'). # The : serves as a dummy placeholder, producing no output. > filename # The > truncates file "filename" to zero length. # If file not present, creates zero-length file (same effect as 'touch'). # (Same result as ": >", above, but this does not work with some shells.) COMMAND_OUTPUT >> # Redirect stdout to a file. # Creates the file if not present, otherwise appends to it. # Single-line redirection commands (affect only the line they are on): # -------------------------------------------------------------------- 1>filename # Redirect stdout to file "filename." 1>>filename # Redirect and append stdout to file "filename." 2>filename # Redirect stderr to file "filename." 2>>filename # Redirect and append stderr to file "filename." &>filename # Redirect both stdout and stderr to file "filename." # This operator is now functional, as of Bash 4, final release. M>N # "M" is a file descriptor, which defaults to 1, if not explicitly set. # "N" is
12, 2008 in BASH Shell, Linux, UNIXQ. How do I redirect stderr to stdout? How do I redirect stderr to a file? A. Bash and
Redirect Standard Error Csh
other modern shell provides I/O redirection facility. There are 3 default standard bash redirect stderr to dev null files (standard streams) open: [a] stdin - Use to get input (keyboard) i.e. data going into a program. bash redirect stderr and stdout to same file
[b] stdout - Use to write information (screen)[c] stderr - Use to write error message (screen)Understanding I/O streams numbersThe Unix / Linux standard I/O streams with numbers:HandleNameDescription0 stdin Standard input1 http://www.tldp.org/LDP/abs/html/io-redirection.html stdout Standard output2 stderr Standard errorRedirecting the standard error stream to a fileThe following will redirect program error message to a file called error.log: $ program-name 2> error.log$ command1 2> error.logRedirecting the standard error (stderr) and stdout to fileUse the following syntax: $ command-name &>file OR $ command > file-name 2>&1 Another useful example: # find /usr/home -name .profile http://www.cyberciti.biz/faq/redirecting-stderr-to-stdout/ 2>&1 | moreRedirect stderr to stdoutUse the command as follows: $ command-name 2>&1 Share this tutorial on:TwitterFacebookGoogle+Download PDF version Found an error/typo on this page?About the author: Vivek Gite is a seasoned sysadmin and a trainer for the Linux/Unix & shell scripting. Follow him on Twitter. OR read more like this:How do I save or redirect stdout and stderr into different files?Linux Redirect Error Output To FileBASH Shell Redirect Output and Errors To /dev/nullUnix and Linux: Redirect Error Output To null CommandPrinting output of c program to a file in LinuxUnix / Linux: Save Output To FilePython Run External Command And Get Output On Screen or In VariablePython Execute Unix / Linux Command ExamplesLinux / Unix Find Command: Avoid Permission Denied MessagesHow to gzip and keep original file on Unix or Linux command line{ 11 comments… add one } Sayed Ahmad February 12, 2012, 12:11 amWhat this mean? $ command > file-name 2>&1 Reply Link Hesham M January 22, 2014, 3:34 pmThis means redirect stdout to file-name, with that in mind redirect stderr t stdout. This will lead to both
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us http://stackoverflow.com/questions/7526971/how-to-redirect-both-stdout-and-stderr-to-a-file Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to redirect both stdout and stderr to a file up vote 158 down vote favorite 44 Hi I am running a bash script that creates a log file for the execution of the command I use the following Command1 >> log_file Command2 standard error >> log_file This only sends the standard output and not the standard error which appears on the terminal. Can I log both the stderr and stdout logged to a file? bash stdout stderr share|improve this question edited Sep 23 '11 at 10:11 asked Sep 23 '11 at 9:35 sdmythos_gr 1,59621321 2 possible duplicate of Redirect stderr and stdout in a bash script –Nathan Aug 19 '14 at 19:35 Possible duplicate of How can I redirect and append both stdout and stderr to redirect standard error a file with Bash? –That1Guy May 5 at 15:19 add a comment| 5 Answers 5 active oldest votes up vote 229 down vote accepted If you want to log to the same file: command1 >> log_file 2>&1 If you want different files: command1 >> log_file 2>> err_file share|improve this answer answered Sep 23 '11 at 9:37 Mat 135k20234273 72 And just to save someone else the frustration, note that the order is important: 2>&1 needs to occur after >> log_file. –Rufflewind Jan 2 '14 at 6:34 2 Why >> and not > ? –user3527975 Mar 16 at 21:38 3 >> appends to the file, > overwrites. Search for "shell redirection" for more details. –Mat Mar 17 at 5:04 add a comment| up vote 117 down vote The simplest syntax to redirect both is: command &> logfile If you want to append to the file instead of overwrite: command &>> logfile share|improve this answer answered Sep 23 '11 at 10:14 Costi Ciudatu 20.3k43365 2 Not sure when this operator was added but it may not be available in older versions of Bash. It does appear to be working on my machine which runs Gnu bash v3.2.48. –James Wald Apr 10 '14 at 7:32 5 @CostiCiudatu the &>> operator does not seem to work in Mac OS X; safer to use Mat's solution imo. –James Fennell May 24 '14 at 18:28 4 @JamesFennell You're right, I wasn't aware of that. I upvoted the accepted answer :) –Costi Ciudatu Ma