LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Fedora
User Name
Password
Fedora This forum is for the discussion of the Fedora Project.

Notices



Reply
 
Search this Thread
Old 08-05-2007, 09:36 AM   #1
O(V)eGA_l2el)
Member
 
Registered: May 2005
Distribution: Fedora & Debian
Posts: 56

Rep: Reputation: 15
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?
 
Old 08-05-2007, 09:40 AM   #2
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,893

Rep: Reputation: 68
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>).
 
Old 08-05-2007, 03:01 PM   #3
paul_one
LQ Newbie
 
Registered: Aug 2007
Posts: 14

Rep: Reputation: 0
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.
 
Old 08-05-2007, 03:13 PM   #4
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,893

Rep: Reputation: 68
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'.
 
Old 08-05-2007, 03:21 PM   #5
paul_one
LQ Newbie
 
Registered: Aug 2007
Posts: 14

Rep: Reputation: 0
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.
 
Old 08-05-2007, 03:38 PM   #6
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,893

Rep: Reputation: 68
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.
 
Old 08-05-2007, 05:04 PM   #7
paul_one
LQ Newbie
 
Registered: Aug 2007
Posts: 14

Rep: Reputation: 0
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.
 
Old 08-05-2007, 05:22 PM   #8
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 85
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 05:33 PM.
 
Old 08-06-2007, 03:25 AM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,499

Rep: Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077
"man bash" - search for REDIRECTION
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 08:42 AM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 09:31 AM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 03:32 PM
Tiny Sofa 2.0 - I thought "halt", "reboot" were only root command ?? sorcerer Linux - Distributions 1 08-21-2004 04:28 PM
"segmentation error" when issuing "useradd" command through terminal with RH 9.0 kaihuang Linux - General 0 10-22-2003 12:47 AM


All times are GMT -5. The time now is 05:08 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration