How to Git Good at Unix.

Philip Thomas K.

02 April 20

Back

Its been a long time since my last post. Now, I’ve made the move over to void linux. That’s right, I’m living the easy runit life.

Apart from that, I think it’s time to give a proper PSA regarding the current state on how to get good at using unix systems. It’s very simple. Just use the manpages:

$ man [command]

It is not easy to get used to this. The more important thing is to know what exactly you are searching for. That is the hard bit that everyone needs to learn. I also have trouble getting this bit down. As with all things worth learning, you never really stop.

Before we tackle that more complicated and hairy bit, let’s address how to find what you are looking for. The default pager for man pages for most unix based operating systems is the less program. less is vi based. So to search for something, use the following:

/[search pattern]

Scroll forward through matches with n and backwards with N.

The unix philosophy comes in to play to learn the hairy bit. That indeed needs its own post. It comes in many forms, but here is the gist of it:

It is better for a program to have its own simple function and objective than for a program to be a compound of functions and objectives.

A bit of a mouth full but if it needs to be reread, reread it. So what does that entail? It means that for each task or configuration related task there is a designated tool that is written specifically to do said task. Not sure what tool is for what task? That is when you should search online.

When you search online, make sure to go up the chain of abstraction. You need to search for a program that sounds like it fits the bill. As with linux users for example, you search first to achieve the task on a unix system. If that does not yield satisfactorily, go down the chain of abstraction. Search the same thing and substitute unix for linux. Does not work? Substitute linux with the name of your distro. Does not work? Use synonyms of your search terms but don’t go full Joey Tribbiani with a thesaurus.

Once you come across the name of a program that sounds like it fits the bill, go straight back to the terminal. Then run man on the program. Parse through the manpage with the inbuilt search tools as stated above. When you force yourself to do this, you go through the full loop.

What else?

It is pretty obvious that to get good at using unix, you must know more deeply what goes on. By this, I mean that it is important to know how things work.

Unix is designed to basically treat everything like a file. There are text files, and binary files. That really is the level of simplification the fellas over at Bell Labs decided on. And it’s why things just work. Things are simple. Everything is pretty clear cut.

Most good unix programs have the ability to take standard input, process it, then spit it out to the standard output, or to a specific format for a certain task. This concept is very simple, but very important to grasp correctly.

Why? Because pipes. Pipes shove the standard input of one program to another program. It’s how a unix user can get a certain task done by using several programs without doing heavy labour. Because that is your computer’s job. The standard input or output can be either plaintext or binary. You need to make sure that the programs that are in your pipelines expect and spit out the correct type input or output.

If however, the output of your program needs to be the arguments of another program, you need to use xargs.

How do you use xargs? Use the method outlined above.

What if you need to run the standard output of a program? Then you need to use subshells:

$ $(STDOUT)

Combining all of these, you can pretty much do anything that you can manually do with all the programs you use individually in unix.

Streams

Now for the advanced Piper(pipist?). You need to use the concept of streams. So what is a stream?

A stream is basically the smallest unit of data that a process can be run on. To utilise this concept, you must know how much data is essential for your process to run with the desired outcome.

When you create a unix pipeline, you need to know when the computer is dealing with streams and how big the stream is. Then with this knowledge, you can create more efficient pipelines. The smaller the stream size, the more efficient the pipeline.

The smallest stream is a characterwise stream. Some programs parse through their standard input character by character, and use some sort of algorithm to perform some sort of process. In C, this is by usage of the the getchar() function.

After that comes the linewise stream. Same thing, but not as friendly with the memory.

Lastly, a program that needs to take the whole STDIN to perform its function will probably be where your pipeline takes it’s time. The unix sort function is one of these that by definition has to take everything in before it can sort it.

What are some things that are really worth learning? There are lots of unix utilities, grep sed and awk are some of the more popular and powerful text manipulation utilities. But it would do any unix user well to have a look through all the coreutils.

Best Regards,
Philip
Back