The Terminal: I/O Redirection

If you thought what we’ve been doing with our Linux shells has been crazy, wait ’til you see this. We’re really just scratching the surface of all the time-and-energy saving things the terminal can do (with a little learning and ingenuity on our parts). So far, we’ve input commands with various flags and arguments, and saw saw the output on the screen. This is normal for most commands, but it’s not the only thing. It might seem trivial, or it might sound completely friggin’ awesome, but you don’t always have to write one command and see the results on the screen over and over and over again. Are you intrigued yet? This is where I/O redirection comes into play.

First, lets take a look and see just what Standard Input and Standard Output is. By default, standard input/output is when the commands begin with keyboards and ends up with the results printed on the screen. We type the command, it does it’s thing, and the results show up on our monitor, right? Well, it doesn’t always have to be this way. Let’s start with an example. Want to see what the /etc directory has for us?

andrew@server:~$ ls /etc
acpi gai.conf magic.mime rc3.d
adduser.conf groff mailcap rc4.d
adjtime group mailcap.order rc5.d
aliases group- mailname rc6.d
alternatives gshadow mail.rc rc.local
apparmor.d gshadow- manpath.config rcS.d
apt gssapi_mech.conf mime.types reportbug.conf
at.deny host.conf mke2fs.conf resolv.conf
bash.bashrc hostname modprobe.d rmt
bash_completion hosts modules rpc
bash_completion.d hosts.allow motd rsyslog.conf
bind hosts.deny motd.tail scsi_id.config
bindresvport.blacklist idmapd.conf mtab securetty
calendar inetd.conf Muttrc security
console init.d Muttrc.d services
console-tools initramfs-tools nanorc shadow
cron.d inittab network shadow-
cron.daily inputrc networks shells
cron.hourly iproute2 nsswitch.conf skel
cron.monthly issue openoffice ssh
crontab opt sysctl.conf
cron.weekly kernel-img.conf pam.conf sysctl.d
debconf.conf ldap pam.d syslog.conf
debian_version passwd terminfo
default passwd- texmf
deluser.conf perl timezone
dhcp3 locale.alias ppp ucf.conf
dictionaries-common locale.gen profile udev
dpkg localtime protocols ufw
emacs login.defs python updatedb.conf
email-addresses logrotate.conf python2.5 vim
environment logrotate.d rc0.d w3m
exim4 lsb-base rc1.d wgetrc
fstab magic rc2.d X11

Now let’s change it up a bit by changing the standard output of ls from our screen to a file.

andrew@server:~$ ls /etc > etc-contents

What we did here was took the content that would’ve been printed on the screen by default and put it into a text file in our current working directory (since we haven’t changed directories since logging on, we should still be in our home directory.). Use the cat command to see if it worked.

Let’s say you want to add more to that list, since creating a ton of files with command outputs would be wasteful in regards to space. While a single > will make a new file, you can use >> to append to a current file. (Keep in mind that you’d have to make sure you’re appending to the right file. Trying to append new contents to “etc_contents instead of the current “etc-contents” would make a new file altogether.)

So, that’s an example of redirecting standards output. To redirect the standard input, we could use the sort command on the same file, but first let’s take sort for a test drive.

andrew@server:~$ sort etc-contents

As you can see, the contents of our etc-contents file is sorted alphabetically on the screen. It’s a type of filter command, which I’ll talk more about later in the post. Right now, let’s continue redirection input/output. Now that we’re familiar with the sort command, and we’ve seen what the sort command does, let’s make a sorted contents file.

andrew@server:~$ sort sorted-etc-contents

See what we did here? our first time with sort, we sorted the standard output of our file. In this command, we now have a normal etc-contents file, and a sorted-etc-contents file, thanks to some I/O redirection and sort.

Seems spiffy, eh? So far, we’ve only been discussing simple I/O redirection. Now, let’s kick it up a notch, with pipes. By using pipes, we can literally daisy-chain commands together to get a lot done with very little work with our fingers. To be more specific, by piping commands to other commands, we’re taking the output of one command and making it the input to another.

When I’m working on my server, I’ll often not have the GUI (Graphical User Interface) loaded, so I can’t use nifty things like scroll and such. When I’m working with a file thats too big for my screen, it becomes difficult. Like, looking for a folder in the middle of /etc with ls (because of the smaller terminal window size, I can only see the last bit of content from ls). How do I get around this? I pipe the output of ls into the input of the less command (so I can go through the ls output like pages instead of staring at the end of it). It looks like this.

andrew@server:~$ ls -l /etc | less
total 832
drwxr-xr-x 3 root root 4096 2009-06-15 07:04 acpi
-rw-r–r– 1 root root 2986 2009-06-15 06:58 adduser.conf
-rw-r–r– 1 root root 45 2009-06-15 03:57 adjtime
-rw-r–r– 1 root root 198 2009-06-15 07:17 aliases
drwxr-xr-x 2 root root 4096 2009-06-15 07:17 alternatives
drwxr-xr-x 3 root root 4096 2009-06-28 04:00 apparmor.d
drwxr-xr-x 4 root root 4096 2009-06-15 07:22 apt
-rw-r—– 1 root daemon 144 2008-10-20 01:34 at.deny
-rw-r–r– 1 root root 1453 2008-05-12 15:02 bash.bashrc
-rw-r–r– 1 root root 226370 2008-07-05 09:55 bash_completion
drwxr-xr-x 2 root root 4096 2009-06-15 06:58 bash_completion.d
lrwxrwxrwx 1 root root 23 2009-06-22 10:13 bind -> /var/lib/named/etc/bind
-rw-r–r– 1 root root 332 2009-01-04 11:06 bindresvport.blacklist
drwxr-xr-x 2 root root 4096 2009-06-15 06:58 calendar
drwxr-xr-x 2 root root 4096 2009-06-15 06:58 console
drwxr-xr-x 2 root root 4096 2009-06-15 06:58 console-tools
drwxr-xr-x 2 root root 4096 2009-06-15 06:58 cron.d
drwxr-xr-x 2 root root 4096 2009-06-22 10:21 cron.daily
drwxr-xr-x 2 root root 4096 2009-06-15 06:58 cron.hourly
drwxr-xr-x 2 root root 4096 2009-06-15 06:58 cron.monthly
-rw-r–r– 1 root root 724 2008-09-28 05:33 crontab
drwxr-xr-x 2 root root 4096 2009-06-22 10:21 cron.weekly

… (you see what I mean).

Now, lets look at some filter commands. We’ve already talked about the sort command, but there are a few others that you should know.

uniq is a filter command that removes duplicate lines from a file.

fmt takes text from a standard input and turns it into formatted text for the standard output.

head and tail output the first and last 10 lines of a file, respectively.

pr splits text data into pages complete with page breaks, headers, and footers.

grep is probably the most often used filter you’ll use. It’s sort of a search filter for text. Let’s say you’re looking for a file with the word “cars” in it. Using an example, your command would look something like this.

andrew@server:~$ ls /filesImade | grep “cars”


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: