Tardis Beginner Tutorials/2
Tutorial 2: Basic BASH usage
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.
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 commandline 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:
This simply dumps the content of the file "motd" in the directory "/etc" to screen. Commandline 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 commandline flags available for a program by typing [programname] --help It is worth mentioning that the linux commandline 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 commandline 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 [programname]. Try it now - type:
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 accessable 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:
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.
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' homedirs. To return to your specific home directory, type cd [yourusername] 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. To return to your home directory again you can type cd ~ which is the special path character meaning homedir. You can navigate faster by using absolute paths. 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 dont use it, but tab completion puts it in. From here we can now go back to our homepage using absolute paths - type cd /home/[yourusername]
All this takes a little getting used to, and it's useful to have a command reference nearby - i recommend printing this handy tardis commandline cheatsheet.