Tardis Beginner Tutorials/2

From Tardis
Jump to: navigation, search

Tutorial 2: Basic navigation of the command line environment

When you log in via SSH, you are presented with the command line interface, by means of the shell. The current default is Bash. Bash stands for "Bourne Again SHell", and is one (but the most commonly used by far) of many shells available for Linux. A shell is is what interprets what you type into the prompt and makes things happen - different shells do things differently. Your default shell is bash, although you can change it if you wish, but this document will only give you a brief introduction to using the shell to run programs and use the filesystem.

Prevent your home directory from being globally accessible

Since Linux is a Unix-like system, it inherits many features from Unix's design. On multi-user systems, this includes having everyone's home directories (where you store your own files and program configurations) globally readable by any user. This may not be desirable to you, especially if you use your shell account for idling on IRC all day, and have a few private messages you might not want everyone to be able to see (however, note, do not have an expectation of privacy on insecure, multi-user systems).

In the shell, type chmod -R o-rwx ~

The tilde '~' is shorthand for your home directory, /home/USERNAME/. In our default, Bash (and most other shells), this is also referenced by the variable $HOME (variables are accessible in shell scripts and the shell more generally by appending $ to the start of them).

Running programs

To run a program, you need to know the name of that program. All you need to do is type that name, or if it is a specific script or program in a non-standard place, you will need to type the path of it as well (more on this later). A lot of the non-interactive Linux programs take command-line options which are additional words or filenames you type after the name of the program. For example, the Linux program cat (short for "concatenate") can display the contents of text files. Try typing:

cat /etc/motd

This simply dumps the content of the file "motd" in the directory "/etc" to screen. Command-line options also often have one-letter flags which tell the program what mode to run in, or how to display the output. For instance, ls lists the content of a directory, much like "dir" in a windows command prompt. Try typing ls / Now try typing ls / -l This simply lists the files and directories in the root of the filesystem (referred to simply as "/"), but the second one gives the "long" output. You can usually get a quick listing of the command-line flags available for a program by typing [program name] --help It is worth mentioning that the Linux command-line is case sensitive, unlike windows - this means that cat is different from CAT and from Cat etc. - only one will work, because there is no program called "CAT", but if there were you could call it by using the upper case. This applies to all filenames and command-line options etc.

Learning how to use programs

The best way to learn how a program works and how to use it is by reading the man page (manual page) built into the system about that program. To find out what a program does and how to use it, simply type man [program name]. Try it now - type:

man cat

You can scroll up and down using up and down arrows, page up and page down, and space and return. Don't worry if you don't understand everything written there, you're not expected to memorize all of a program's options - that's why they're so easily accessible in man pages! Press q to quit the man page reader.

I should also mention the other manual command, info which (most of the time) shows the same info as man, but uses Emacs to display it. However, certain programs (such as tar) have much more useful information in the info page than the man page, because certain GNU programmers are very awkward about these things. You don't really need to worry about this though.

Finding a program for your purpose

If you know what sort of program you want to run, but aren't sure of the name (or if such a program exists), you can use apropos to search for a program by function. For instance, say we want to find an IRC client but we don't know the names of any. We type:

apropos irc

But we get quite a lot of matches, most of which are no use to us. This is because the search has turned up a load of results where "irc" was part of another word, such as "circular". If we have a quick look at man apropos we find out that the -e flag searches for exact matches:

apropos -e irc

Yay, we've narrowed our matches down to what we wanted! Alternatively we could have tried apropos "irc client" which would have yielded the same results. However, typing apropos irc client without the quotes would have returned twice the unwanted results, as it would have searched for both "irc" and "client" and given you results for either. The quotes tell Bash to treat what you put in them as one continuous string.

Navigating the filesystem

Just like in windows, you can use cd to change directory. Why don't we use it in combination with ls to have a browse around the filesystem right now! To change to a directory, simply type cd and that directory's name. You'll notice the directory you're in is shown on your prompt, and can also be shown by typing pwd. Now let's take a spin around the filesystem and see what we can see! Type ls between each step to get a feel for where you are. To go up a step in the directory tree type cd .. Notice you are now in the /home/ directory with all the users' home directories. To return to your specific home directory, type cd [your username] To go to the root of the filesystem type cd / You can also use tab completion - this is where you press tab partway through typing the name of the directory (or filename) and the shell completes it for you (if there is only one possible match). Otherwise you will hear a beep. If you press tab a second time, it will list all the possible completions for what you've typed already. Type cd ~ to return to your home directory. You can navigate faster by using absolute paths. The current directory can be printed to the shell by typing pwd - this is useful to remember for scripts that you write for the shell. If you wanted to look at the source of my web page, you could type:

cd ..
cd ..
cd var
cd autofs
cd www
cd users
cd rain
cd pages

But it is infinitely more efficient to type cd /var/autofs/www/users/rain/pages/ all in one go. You could also type that without the first "/" but only if you were in the root directory - the first "/" means "from root". The last "/" is optional - Bash is intelligent enough to know something is a directory even if you don't use it, but tab completion puts it in. From here we can now go back to our homepage using absolute paths - type cd /home/[your username]

All this takes a little getting used to, and it's useful to have a command reference nearby - I recommend printing this handy Tardis command-line cheatsheet.

Changing your shell

If you decide you want to try another shell, you can type the name of it, e.g. zsh (Z Shell) to start it. However, to change it so that it is your login shell, the traditional chsh will not work. This is due to LDAP (the way that Tardis manages accounts). Please ask on our IRC channel if you want to change it.

Next: Mail