Unix Philosophy

3AM at TSMC Fab2

Imagine it’s the early 2000s. You’re an engineer at TSMC’s ICSD, responsible for maintaining hundreds of Microsoft Windows servers. At 3 AM, you’re urgently called to Fab2: some servers are down, and you suspect the server room’s air conditioner has failed. But which server? Which room? How do you check temperatures across hundreds of machines?

These are not your home PCs with graphical interfaces. Doing this manually would mean the nightmare of 500 mouse-clicking through GUI tools. Now imagine running a single command to log into all 500 servers, get their temperatures (with cat 🐱), and identify the hottest machines:

for i in server{1..500}; do  
    echo -n "$i: ";  
    ssh $i "cat /sys/class/thermal/thermal_zone0/temp";  
done | sort -n -k2 | tail -n2  

Learn these tools! Will change your life! Check out MIT Missing Semester: Shell Tools

This script shows two important Unix philosophies:

  1. Everything is a file
  2. Make each program do one thing well and let them work together

Read: 3.2.1 Do one thing well (Lampson 2021)

The Windows Dilemma

You might have seen Windows’s cmd.exe dark window, but anyone who has tried to program anything on it know that it’s like writing Tensorflow Python code in Notepad. But this is the fate of Windows ITs for a long time.

Before PowerShell, Microsoft’s product strategy is to offer every possible functionality the user could need as a GUI tool, built by Microsoft engineers. Scripting is available in Unix only: user can quickly assemble a desired program using many single-purpose binaries.

Fortunately, this has changed with in the mid-2000s through the creation of PowerShell. When Microsoft engineer Jeffrey Snover developed PowerShell in mid-2000s, his boss questioned why Windows needed a command-line tool when the company has so many amazing GUI tools. Snover argued that enterprise automation required scripting, not mouse clicks. He turned out to be right: PowerShell is critical to Microsoft’s founding of Azure in the 2010s, now one of the biggest cloud on earth.

But there is still a deep assumption in PowerShell. It assumes that everything passing around is a .NET object. However, Unix has a different philosophy:

Write programs to handle text streams, because that is a universal interface. - Peter H. Salus

In other words, PowerShell assumes structured API for IPC where bash/pipe pass around unstructured text.

Everything is a File

An UNIX operating system tries to present many of its internal states and connected devices through the same basic interface that you use to read a text document.

  • Hardware sensors (like CPU temperature) become readable files
  • Running processes appear as virtual files in /proc
  • Disks are listed in /dev
# Read CPU temperature  
cat /sys/class/thermal/thermal_zone0/temp  

# Discard output to a black hole  
echo "Junk data" > /dev/null  

# You can change directory to here and see what's going on in your system
cd /proc
# How much memory I have?
cat /proc/meminfo | grep "MemFree"  

This abstraction lets you use simple tools (cat, grep, sort) to debug complex systems on the fly.

Everything is a file: a beautiful explanation

Examples of Linux one-liner

cat /dev/urandom | tr -dc A-Za-z0-9 | head -c 32

Try this on Github Codespace. A URL will be automated generated, click that.

sudo apt install netcat-openbsd
while true; do { echo -ne "HTTP/1.1 200 OK\r\n\r\n"; ps aux; } | nc -l -p 8080; done
Back to top

References

Lampson, Butler. 2021. “Hints and Principles for Computer System Design.” https://arxiv.org/abs/2011.02455.