0

On my OSX command line, find directs errors to STDOUT. I find this very inconvenient, and I also find it annoying to add 2>/dev/null to the end of the command every time. This brings up several questions:

  • Is this a system wide setting (eg. do errors direct to STDOUT on OSX but to /dev/null in Linux) or is this specific to find?
  • Is there a way to make find take this behavior by default (without creating an alias)?
  • If so, would a second command be needed to restore the original behavior?
  • If not, is there a shortcut which is shorter than 2>/dev/null (eg. unzip -qq instead of unzip >/dev/null)?
  • How would you make this alteration on a Linux box?
1
  • 2> redirects STDERR, not STDOUT. You can run exec 2>/dev/null to redirect all STDERR to the bitbucket from that point on. Also, defining a function would be more appropriate than an alias.
    – choroba
    May 29, 2014 at 14:48

1 Answer 1

1

Though both are appearing on your terminal in the same way, STDERR is not being redirected to STDOUT.

To check whether I'm lying, try running whatever find command was generating errors (without any redirects) and putting > example.file at the end of it. If the generated errors appear on your screen, but not in the generated file called example.file, then you know STDERR is not being redirected, because if it were all the output would be in the generated file.

When you add 2>/dev/null what you're saying is "take all the output from STDERR and write it to the file /dev/null".

Is this a system wide setting (eg. do errors direct to STDOUT on OSX but to /dev/null in Linux) or is this specific to find?

No; by default when running programs from the terminal STDIN, STDOUT, and STDERR are all copied to the terminal, but they are handled differently internally (i.e. they are aliased to different file descriptors), as in Linux. By default, errors are always printed to the console when you run a program, so that if something goes wrong you know what, when, and why. /dev/null is like a black hole; once information goes in you can't get it back. Most people don't want error output to vanish mysteriously into the ether, so (no Linux distro I know of) does that by default.

Is there a way to make find take this behavior by default (without creating an alias)?

If you want to make find behave this way every time without using an alias (which... I mean an alias wouldn't work anyway because aliases only work when you want to replace the beginning of the command only, and redirects have to be at the end Belay that; I was wrong; redirects can be put between/before arguments without issue), one way would be to define a function in bash (I assume you use bash; my apologies if that is a mistake) as follows:

find() { command find "$@" 2>/dev/null }

If not, is there a shortcut which is shorter than 2>/dev/null (eg. unzip -qq instead of unzip >/dev/null)?

There isn't any configuration file, environment variable, or convenient option flag you can use, if that's what you mean.

How would you make this alteration on a Linux box?

Same as on OSX. If you don't want to redefine that function every time you login, add that line to the file ~/.bashrc. Although... Come to think of it, I do think there is a different in OSX, there. On OSX I'm pretty sure you actually have to add it to ~/.bash_profile instead to get it to work.

1
  • I've always used alias find='find 2>/dev/null', the shell doesn't care where redirections are in the parameter list and removes them as it acts on them, the program (find) never sees the redirection requests.
    – lornix
    May 29, 2014 at 20:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .