I am going to discuss two file descriptors of the common three. They are standard input and output. In my next post I will talk about standard error. These three file descriptors are the basis for the UNIX/Linux command line. Don’t worry if your confused, you will learn what a file descriptor is later on.

Let’s pretend we are a shut in and are only way of communicating with the outside world is the telephone. Standard input (stdin) would be when we are listening and standard output (stdout) would be when we are speaking. The inside of a computer program works in much the same manner. For now we.ll just assume that we only have one method of communicating out (stdout) to the user and the user only has one method of communicating in to the program (stdin).

A very common usage of file descriptors is called “piping.” This is when you send the output of one command to another command. For example, if we want to find out the process identifiers of all the processes a specific user is running we can use the ps command.Let’s pretend we are a shut in and are only way of communicating with the outside world is the telephone. Standard input (stdin) would be when we are listening and standard output (stdout) would be when we are speaking. The inside of a computer program works in much the same manner. For now we.ll just assume that we only have one method of communicating out (stdout) to the user and the user only has one method of communicating in to the program (stdin).

root@www:~ # ps -ef
UID PID PPID C STIME TTY TIME CMD
dhcp 341 1 0 Feb25 ? 00:00:00 dhclient3 -pf /var/run/dhclient.eth0.pid -lf ...blah blah
klog 440 1 0 Feb25 ? 00:00:00 /sbin/klogd -P /var/run/klogd/kmsg
mysql 503 502 0 Feb25 ? 00:00:01 /usr/sbin/mysqld --basedir=/usr ...blah blah
brock 670 667 0 Feb25 ? 00:00:12 sshd: brock@pts/0
brock 671 670 0 Feb25 pts/0 00:00:00 -bash
www-data 14983 32620 0 02:10 ? 00:01:07 /usr/sbin/apache2 -k start -DSSL
www-data 19521 32620 0 05:47 ? 00:00:50 /usr/sbin/apache2 -k start -DSSL
... Output removed for readability

The ps -ef command normally outputs a ton of information. Especially on busy systems. Suppose I think that user brock is sending spam and so I want to see all the commands running under his username. I can use a pipe to send the output of the ps -ef command to another command grep. grep is filtering program. It takes the input you give it then searches for your term in the each line. If it finds your term on a given line, that line is printed.

root@www:~ # ps -ef | grep brock
root 667 633 0 Feb25 ? 00:00:00 sshd: brock [priv]
brock 670 667 0 Feb25 ? 00:00:12 sshd: brock@pts/0
brock 671 670 0 Feb25 pts/0 00:00:00 -bash
brock 685 684 0 Feb25 pts/0 00:00:00 [sendmail]
root 16881 633 0 Feb25 ? 00:00:01 sshd: brock [priv]
brock 16884 16881 0 Feb25 ? 00:00:27 sshd: brock@pts/1
brock 16885 16884 0 Feb25 pts/1 00:00:00 -bash
brock 5255 5254 0 Feb26 pts/1 00:00:00 [sendmail]
root 1089 5256 0 16:17 pts/1 00:00:00 su - brock
brock 1090 1089 0 16:17 pts/1 00:00:00 -su
root 1906 1360 0 16:45 pts/1 00:00:00 grep brock

The pipe signified as the bar | is the magic connector that takes the stdout (file descriptor 1) of ps -ef and sends it to grep brock as stdin (file 0). A pipe or | is similar to real pipe. Only in a UNIX/Linux pipe the medium flowing through the pipe is data not liquid.

The term file descriptor is just a fancy term for stdout and stdin. Standard out is
file descriptor one and standard input is file descriptor zero. Standard output can also
be send to a file:

root@www:~ # ps -ef | grep brock > ls.out
root@www:~ # cat ls.out
root 667 633 0 Feb25 ? 00:00:00 sshd: brock [priv]
brock 670 667 0 Feb25 ? 00:00:12 sshd: brock@pts/0
brock 671 670 0 Feb25 pts/0 00:00:00 -bash
brock 685 684 0 Feb25 pts/0 00:00:00 [sendmail]
root 16881 633 0 Feb25 ? 00:00:01 sshd: brock [priv]
brock 16884 16881 0 Feb25 ? 00:00:27 sshd: brock@pts/1
brock 16885 16884 0 Feb25 pts/1 00:00:00 -bash
brock 5255 5254 0 Feb26 pts/1 00:00:00 [sendmail]
root 1089 5256 0 16:17 pts/1 00:00:00 su - brock
brock 1090 1089 0 16:17 pts/1 00:00:00 -su
root 1906 1360 0 16:45 pts/1 00:00:00 grep brock

Note: A single > writes to a file while >> appends.

Next up: Standard Error Basics

Leave a Reply

If Wordpress eats your comment (shell output, loops, ex..) email the text to me.