Command Line 2

From FreekiWiki
Revision as of 06:12, 8 January 2006 by Jeff (talk | contribs)
Jump to navigation Jump to search

Introduction

You've taken the Command Line class. You've QC'd a couple of boxen, and now you've built one. In the meantime, you've forgotten everything you learned in Command Line, except (I hope), tab completion. It's time to learn how it all works, fits together, and how to make sense of it all. Then you can go on to all the other topics in From $ to #.

Lexing a command line

guest@freekbox:~$ ls -la --sort=size -r -w 100 /home/lab* /home/class*

Scared? It's simpler when you know how to lex. Lex, the verb, means to break a flow of information into meaningful symbols. It's a term specific to linguistics and computer science, so you may not know it, but you do it all the time. When reading this sentence you recognize spaces and turn the letters around the spaces into separate words. You could lex on commas, and,then,the,sentence,would,look,like,this. We lex in aural conversation as well, though the spaces aren't visible, they're something we have to be taught. This is why the last step of fluency in a foreign language is the most difficult: knowing when they're saying "heat it", as opposed to "He did."

Despite all the other strange characters in a regular command line (the /, the -, the *, etc), we lex command line arguments with spaces. So let's look at the different pieces of the above command

ls
-la
--sort=size
-r
-w
100
/home/lab/*
/home/class/*

The most important of these is always the first one. Here's why.

Programs are a field of machines

Imagine, for a moment, that you are outside the Matrix, looking out at the field of machines that runs Zion. It is overwhelming. They seem to all interact and interrelate in bizarre ways. People are going through bizarre motions as they tend to machines because this is the way they have been taught.

You discover a simpler way. It turns out that, though they seem to interrelate, each machine is its own entity, and when you are looking at the machine, it has its directions for how to use it printed on the side. Though now you realize you need to remember where all the machines are called, you understand that when you get to the machine you need, the instructions are in front of you.

There's a field of machines in Unix. They're called programs, executables, or binaries (all the same thing). Some coder put together a project in which he, she, it (he from now on) figured out what he wanted the program to do and wrote that. Then he compiled it into a binary and installed it somewhere. This binary can have its own system of rules and way of thinking, but that will be documented in the man page. Soon you'll know enough to start reading them.

Parsing a command, part 1

Imagine momentarily that we put the verb first in English grammar. Talk like Yoda, we would. Know, however, immediately, we would, meant to convey what action the sentence did.

Command line talks like Yoda. The first word is the command, the action. The binary, or the machine, in the above. So

guest@freekbox:~$ ls -la --sort=size -r -w 100 /home/lab* /home/class*

The only piece we care about immediately is ls. ls is a command to list files in a directory (we'll get there in a moment). To know anything else about the command line above we have to read man ls to find out how ls expects arguments. When we read ls, we find out that -l means one thing, -a means another, --sort=size means sort files by size, -r means recursive, -w gets passed the 100 and means format the output to 100 columns, and the last two pieces are the directories/files we want listed. The *s I'll explain in a bit, in Parsing Part 2.

Paths/Filesystem introduction

What the heck are those directories? Honestly, how does that all work?

Why is a tree with lots of branches better for climbing than a tree with few branches? It's not because it's easier to climb, it's because when you bring along a whole class full of students, you still have room for all the students to sit on their own branches. This is the way the Linux filesystem is too. At the bottom there is the /, appropriately called the root. It's hard to realize that / is two things in the context of a file location or directory name, it's the root, the bottom of the tree, and it's the lexical separator (like the space is for written English). So /var/lib has two different characters other than the letters: the / that is the root and the / that separates var from lib. They're different. One is the root at the bottom of the tree, the other is the place where the lib directory branches off from the var directory.

So / is a place. So is /var (or /var/ if you want). It's a directory called var located in / (the root). /home is a directory which holds directories for users, like /home/joe holds all the files for joe. And there's one other critically important location: your current location. It starts off, if you're joe, as /home/joe (there's a special way of referring to this, which is ~). But if you don't know where you are, the command pwd will tell you where you are. It's probably also in your prompt, which is that strange thing to the left of where you type commands.

(sadly, not all directory names start with /. You'll learn about why in Where is everything, but for now imagine that anytime you don't see a / at the beginning you add the location you're currently in before what you see, so if pwd: ~ then superfood/juice is actually /home/joe/superfood/juice).

Parsing, Part 2

Bash is a shell

A handful of commands

The pipe

How do you do this?