Living in Unix

The last post introduced the Unix environment and the basic ways of navigating this environment. Here let's talk about some ways of making your life in the shell a little easier.

Asking for help

You can, of course, always ask the class staff for any help. But there is also a lot of built-in resources for figuring things out in the Unix environment

--help

Most (though not all) Unix programs have built-in help that you can access by entering the command with a single argument, --help. For example, if we ask the "print working directory" command for its help with pwd --help, we get the following output:

pwd help string

That first line of the output indicates ways of calling the program. The square brackets indicate optional arguments (sometimes referred to as flags or switches. In this example, there are two flags, -L and -P. Though in most of our cases we'll never need either. Assuming that no flags need arguments themselves and are composed of single characters, we can often shorted a list of options, e.g., pwd -LP to select both options.

The help output for the ls command is significantly more complicated. Below is a truncated list of the help output.

ls help string

Note that here some options have both a short flag and a longer, more descriptive version. The most commonly used options for ls (in my own experience) are -a, -l, and -h. The -a (or --all) option lists hidden files. In Unix, files that start with a single dot (e.g., .bashrc) will normally be hidden from programs like ls or a GUI file manager. The -l (or --list) option will print out each listed entry with more information (file permissions, owner, file size, etc.). The -h (or --human-readable) option, when used in conjunction with the -l option, lists file sizes using SI prefixes (e.g., 1K, 234M referring to 1 kilobyte or 234 megabytes). Why this isn't the default setting is beyond me.

man

RTFM: Read The Friendly Manual (depsite what you may read on the internet)

Almost every Unix program comes with manual pages. These can be accessed with the man command. For example, man ls would bring up the manual page for ls. The resulting screen looks like a more verbose version of the help output we saw earlier. The man page can be navigated with the arrow keys to scroll up and down. Some terminals support using a mouse wheel too. To quit the man page, just press q.

Less (typing) is more

Here are some common ways of avoiding lots of typing.

Tab completion

This is a fantastically useful tool. When entering lines at the command prompt, hitting tab will cause bash (the shell program that manages the text input and output) to try and guess what you're trying to type. If you haven't typed anything, there's not much for it to guess. But if we were to type the letters pw and hit tab, we would see the list of available commands shown below, including the familiar pwd command. I see a command, pwmake, that I might be interested in. If I continue typing this command, adding m to the already typed pw, then I can simply hit tab to complete the rest of the characters, since the first three characters uniquely specify this option. As an aside, from reading its manual page, pwmake seems to be a tool for generating random passwords.

tab completing to find commands

This tab completion feature is context dependent. Say I'm in the middle of changing directories and I'm typing out the path to some distant directory. If I hit tab while writing the path, bash will list directories that match what I'm currently typing. And if I've uniquely specified some directory, it will autocomplete the rest of the path.

Command history

Most shells will keep track of the commands that you've recently entered into the terminal. You can access the most recent command by hitting the up arrow, and you can cycle back through older commands by continuing to hit the up arrow. For example, if we are currently editting some python file named some_long_name.py and we want to run python on this file, we can enter the command python some_long_name.py once, then hit the up arrow and enter whenever we want to run the command again.

Stop and Drop

If we run a command that's taking up a lot of time and we want to quit, we can send an interrupt signal by hitting Control-C.

From most shell environments, you can exit the shell by hitting Control-D.

social