linux - Direct errors in find to /dev/null by default

07
2014-07
  • cwallenpoole

    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?
  • Answers
  • Parthian Shot

    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.


  • Related Question

    linux - Using find to find files
  • Phenom

    If I'm in the root directory, can I use find to search all the directories below it for a file?

    I tried find stdio.h but it didn't find anything. However I know that that file is in the filesystem somewhere. How do I find it?


  • Related Answers
  • Greg Bacon

    It's in /usr/include/stdio.h, but for future reference run

    $ locate stdio.h
    

    Don't forget to run updatedb first. On Cygwin, you'll want to run it as Administrator.

    To look in the current directory and its children:

    $ find . -name stdio.h
    

    To look everywhere beneath the root directory:

    $ find / -name stdio.h
    

    You tagged this as a Cygwin question, so to search the entire C drive, you could run

    $ find /cygdrive/c -name stdio.h
    

    but that's likely to be much slower than searching from an Explorer window.