unix/linux redirection: empty files without typing /dev/null

Subtitled, Figurative and Literal Redirection, a post in which I alter my approach to redirection with explict shell redirection indicators.

When I  started working with Unix, I generally used ”cat /dev/null’ to empty a file quickly and cleanly, e.g.,:

foozbear% cat /dev/null > big_useless_log

foozbear% cat /dev/null > /var/mail/mymail

This, of course, made big_useless_log or mymail empty (zero length), and did so without getting rid of it, in case a data write was imminent. 

This week I was using a lot of temp files — actually using the same temp file over and over to hold data I’d scraped from Cisco switch output so I could point some ‘awk’ at it for parsing purposes (or, vice versa, really). For some reason, I decided to see what would happen if instead of removing and recreating the file (or more laboriously, typing cat /dev/null > temp), I used a redirection symbol, i.e.,
foozbear% > temp
Well, this worked as well as my previous steps, so I started alternating (using the command line up arrow) between my temp file resetting/editing
foozbear% > temp && vi temp
and my grep/awk line:
foozbear% egrep 'Gig|Fas' temp | awk '{print "interface "$2" "$3"n description "$1}'

This worked fantastically (note that I was using bash under Ubuntu here), and a good time was had by all (the Cisco switches were particularly pleased, having their cdp output turned into port descriptions so handily — but perhaps I’ll expand on that later).

The redirection symbol also can be used if you are too lazy to create an empty file using ‘touch’. Instead of
foozbear% touch newfile
just type
foozbear% > newfile
and you’re all set.

redirecting standard error (stderr) and standard output (stdout) to a file

Capturing both errors and normal list output from ls, with “long” and recursive options set,  to a file called /tmp/allout :

ls -lR > /tmp/allout 2>&1

A simple output filter for “show cdp neighbors” using a compound regular expression (Cisco IOS)

Here is a simple filter I’ve used on ’show cdp’ output, which lets me get information quickly.

MalbecMDF#show cdp neighbor detail | include (---|Device ID|IP address|Platform)

In practice, I generally cut the command down to:
sh cdp ne d | inc (blah|blah)
It is probably best to start with obvious match choices, before pairing them down, as you can find yourself surprised with the text that is grabbed from different types of devices if you’re basing your regular expression match on a small sample.

In any case, the output should come out something like this:

MalbecMDF#sh cdp ne d | inc (---|e ID|IP add|Plat)
Device ID: SummaC-6509
IP address:
Platform: cisco WS-C6509-E, Capabilities: Router Switch IGMP
Device ID: Malbec-AP10
IP address:
Platform: cisco AIR-AP1231G-A-K9 , Capabilities: Trans-Bridge
Device ID: Malbec-AP11
IP address:
Platform: cisco AIR-AP1231G-A-K9 , Capabilities: Trans-Bridge

upgrading an Ubuntu server from the command line

This is how I upgraded from 8.10 to 9.04 over the network:

sudo apt-get install update-manager-core
sudo do-release-upgrade

[reference : http://www.ubuntu.com/getubuntu/upgrading ]