Quantcast
Channel: What are file descriptors, explained in simple terms? - Stack Overflow
Viewing all articles
Browse latest Browse all 15

Answer by Abhishek Kamal for What are file descriptors, explained in simple terms?

$
0
0

File Descriptors (FD) :

  • In Linux/Unix, everything is a file. Regular file, Directories, and even Devices are files. Every File has an associated number called File Descriptor (FD).
  • Your screen also has a File Descriptor. When a program is executed the output is sent to File Descriptor of the screen, and you see program output on your monitor. If the output is sent to File Descriptor of the printer, the program output would have been printed.

    Error Redirection :
    Whenever you execute a program/command at the terminal, 3 files are always open
    1. standard input
    2. standard output
    3. standard error.

    These files are always present whenever a program is run. As explained before a file descriptor, is associated with each of these files.
    File                                        File Descriptor
    Standard Input STDIN              0
    Standard Output STDOUT       1
    Standard Error STDERR          2

  • For instance, while searching for files, one typically gets permission denied errors or some other kind of errors. These errors can be saved to a particular file.
    Example 1

$ ls mydir 2>errorsfile.txt

The file descriptor for standard error is 2.
If there is no any directory named as mydir then the output of command will be save to file errorfile.txt
Using "2>" we re-direct the error output to a file named "errorfile.txt"
Thus, program output is not cluttered with errors.

I hope you got your answer.


Viewing all articles
Browse latest Browse all 15

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>