LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-04-2017, 08:38 AM   #1
sigint-ninja
Member
 
Registered: Feb 2011
Location: Republic Of Ireland
Distribution: Debian,Centos,Slackware
Posts: 508

Rep: Reputation: 29
redirecting both stdout and stderr


hi guys,

why doesnt

ls /etc /root 2>&1 redirectfile1

not work

is this the correct way,it is working:
ls /etc /root > redirectfile1 2>&1

and to append its
ls /etc /root >> redirectfile1 2>&1

but i always thought 2>&1 always worked on its own
is the above the right way to do it? or is there a better way?

also how would you write stdout to a file and stderr to a different file using one command?

thanks

Last edited by sigint-ninja; 08-04-2017 at 08:40 AM.
 
Old 08-04-2017, 08:47 AM   #2
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Redirect both to same file:

Code:
ls /etc /root &> redirectfile1
Redirect both to different files:

Code:
ls /etc /root 2> errorfile 1> outputfile
To append to one or more of the files above rather than overwrite them, replace the relevant > with >>.

Last edited by hydrurga; 08-04-2017 at 08:55 AM. Reason: Appending
 
2 members found this post helpful.
Old 08-04-2017, 09:08 AM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
The order is important. The redirections are processed left-to-right, and "2>&1" means "send stderr to the place stdout is now going". Subsequent redirections of stdout will not affect stderr.
Code:
ls /etc /root 2>&1 >redirectfile1
  1. First, redirect stderr to the place stdout is now going, which is probably the terminal. Since stderr was probably already going to the terminal, this is pretty much a no-op.
  2. Then, redirect stdout to file "redirectfile1". This has no effect on stderr.
I have to use words like "probably" and "pretty much" because this command might be in a script invoked with stderr and/or stdout already redirected.
Code:
ls /etc /root >redirectfile1 2>&1
  1. First, redirect stdout to file "redirectfile1".
  2. Then, redirect stderr to the place stdout is now going, i.e., the file.
Note that I did not have to use the word "probably" in the second example. Nothing there is dependent on how stderr or stdout might have been previously redirected.
 
3 members found this post helpful.
Old 08-04-2017, 09:10 AM   #4
sigint-ninja
Member
 
Registered: Feb 2011
Location: Republic Of Ireland
Distribution: Debian,Centos,Slackware
Posts: 508

Original Poster
Rep: Reputation: 29
very efficient and easy to remember some other forums showed nasty ways of accomplishing this

thanks very much kudos!!!

Last edited by sigint-ninja; 08-04-2017 at 09:58 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Weird thing in redirecting stdout and stderr brubizu Linux - Software 2 07-01-2010 11:09 AM
example for redirecting stderr,stdout in seperate files using two way pipes concept nagendrar Programming 0 07-06-2009 06:08 AM
Redirecting stdout, stderr to pty0? Rostfrei Linux - General 4 03-20-2007 03:15 AM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 02:36 AM
redirecting stdout and stderr to a file Avatar33 Programming 4 03-12-2005 07:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration