| Fedora This forum is for the discussion of the Fedora Project. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-05-2007, 08:36 AM
|
#1
|
|
Member
Registered: May 2005
Distribution: Fedora & Debian
Posts: 56
Rep:
|
What does "2>" do when added to "find" command?
Utilizing: 2.6.20-*.fc6
Command:
find / -name rc.local 2>/dev/null
I understand that without 2>/dev/null, the find command would display every directory while searching for rc.local. With 2>/dev/null, only the directories containing rc.local would be displayed (which is my preferred method). What I do not understand is what the 2> does? Will someone explain to me?
|
|
|
|
08-05-2007, 08:40 AM
|
#2
|
|
Senior Member
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,893
Rep:
|
2> redirects 'standard error output'. Usually when a tool wants to has something as its 'official output' (like for piping) and something to be 'information for user' which is displayed even when output is piped, it will write output to descriptor 1 (stdout, redirected by 1> or just > ) and information to descriptor 2 (stderr, redirected by 2>).
|
|
|
|
08-05-2007, 02:01 PM
|
#3
|
|
LQ Newbie
Registered: Aug 2007
Posts: 14
Rep:
|
When you type a command at a terminal (command prompt, command line interface, whatever you call it) it's output is displayed to you.
This is not actually one output but rather a culmination of different 'channels' all together.
The standards are : std.in, std.out and std.err (in, out[1] and error[2]).
1 is used for output to the user. This contains results etc.
2 is used for errors, redirecting this in find is good for scripts, especially when it is just complaining about not being able to access directories.
3 and above are rarely used, but I have seen 3 and 4 used before.
The 2> redirects the output from this stream and pipes it into a location.. This can be another stream ( 2>&1 ) or (usually) a filesystem location... /dev/null to basically ignore the output from this stream, or /tmp/check_later which means you can open that file later and check the output.
|
|
|
|
08-05-2007, 02:13 PM
|
#4
|
|
Senior Member
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,893
Rep:
|
Well, if we are to go into these details, it is reasonable to mention also that the same 'channels' are used for file I/O and that program expects stdout and stderr to exist and somehow accept output. Talking about higher 'channel numbers' program usually assumes they do not exist on its start and creates them by identifying with some files or channels. Even if you redirect 'channel 3' by 3> it is highly probable that a program will either reassign it to whatever it wants (as it does not consider it to be pointing to anything reasonable), silently use next free number, or even fail - but not redirect what it would normally write to this 'channel'.
|
|
|
|
08-05-2007, 02:21 PM
|
#5
|
|
LQ Newbie
Registered: Aug 2007
Posts: 14
Rep:
|
Oh right. That's good to know.
Are they actually called channels? I can't remember, but something's bugging me about what they're called.
Seeing what you've written above, channels seems a more appropriate name to call them.
|
|
|
|
08-05-2007, 02:38 PM
|
#6
|
|
Senior Member
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,893
Rep:
|
No, they are really called 'file descriptors'. I just liked your description (which reflects their use), and decided to make additions to it. They are also one of a few types of objects that are usually wrapped in something called 'streams' in high-level languages. When you create a new pair of file descriptors just to serve to take data from one process and give it to another, it is called 'pipe'.
File descriptor is generally an index in the array of open file-like objects kept for this process by OS.
Usually programs create file descriptors (by opening files) before using them, but standard requires to have descriptors 1 and 2 already open when a program starts. File descriptor 0 is also open, and it is standard input.
|
|
|
|
08-05-2007, 04:04 PM
|
#7
|
|
LQ Newbie
Registered: Aug 2007
Posts: 14
Rep:
|
Sorry, I keep mixing descriptions all over the place. Channels was a throw-back to what I remember from basic and the good old sinclair QL.
My original reason for posting was that although you gave good info - I think it wasn't enough for that person (who I guess was looking in a script and saw that in a line) who may not have known what std.err or std.out were and what you meant exactly.
Thanks for expanding on the names.
|
|
|
|
08-05-2007, 04:22 PM
|
#8
|
|
Member
Registered: Sep 2004
Location: solihull.w-mids.uk
Distribution: Debian 5.0, CentOs, Solaris 8-10
Posts: 576
Rep:
|
To put it a different way, ...2>/dev/null is a way of preventing the results that you want being drowned in error messages.
I usually use it when searching as an ordinary user, as without root permissions, find will usually throw up a flood of "Permission denied" errors if searching through system directories.
Yours helpfully,
Rob
Edit: an example:
With:
Code:
bertie@mylinux:~$ find / -name bash 2>/dev/null
/bin/bash
/usr/share/doc/bash
/usr/share/menu/bash
/media/hdc4/bin/bash
/media/hdc4/usr/share/doc/packages/bash
bertie@mylinux:~$
... and without:
Code:
bertie@mylinux:~$ find / -name bash
/bin/bash
find: /etc/ssl/private: Permission denied
find: /etc/cups/ssl: Permission denied
find: /etc/firestarter/outbound: Permission denied
find: /etc/firestarter/inbound: Permission denied
find: /var/lib/gdm: Permission denied
find: /var/lib/slocate: Permission denied
find: /var/run/cups/certs: Permission denied
find: /var/tmp/kdecache-root: Permission denied
find: /var/cache/setup-tool-backends/debug: Permission denied
find: /var/cache/setup-tool-backends/backup: Permission denied
find: /var/spool/cron/atjobs: Permission denied
find: /var/spool/cron/atspool: Permission denied
...
227 lines in total!
Last edited by Robhogg; 08-05-2007 at 04:33 PM.
|
|
|
|
08-06-2007, 02:25 AM
|
#9
|
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 11,231
|
"man bash" - search for REDIRECTION
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:42 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|