Experience Level: Beginner and up
Follow this tutorial with: “Using the LXTerminal and Bash”
A central tenet of Linux and other Unix-like operating systems is diversity. Many programs are similar at first glance, but have different ways of executing tasks. The purpose of this tutorial is to gently familiarize you not only about how LXTerminal and Bash work, but the nomenclature used in Linux.
Linux distributions come with many built-in tools, which we will begin using in this tutorial. LXTerminal mimics the time decades ago when computers had nothing but text-only consoles. We will use the terms “terminal emulator,” “terminal” and “console” interchangeably.
LXTerminal automatically accesses a program called
bash, the default shell for Linux. A “shell” program acts as a translator by accepting from you the commands it has been programmed to perform. It's your responsibility to be familiar with common (and some not-so-common) commands.
Shells are often referred to as “the interpreter” or the “command line interpreter” and commonly known by the three-letter acronym “cli” or “CLI,” pronounced see-ell-eye.
Many different shells exist. Never take it for granted that commands typed in one shell will perform exactly the same in another shell.
“Commands” are also often called “utilities” and “tools” in Linux documentation. This tutorial, “Using the LXTerminal and Bash” and “Beyond Bash Basics” will use all three words.
All Linux-compatible shells are capable of executing multiple commands sequentially. Typed on a single line without further interaction from you, these multiple commands enhance the flexibility of the utilities. There are limits to how some utilities can be used. You can read “Beyond Bash Basics” for details.
Bodhi Linux includes hundreds of
man pages (
man being an abbreviation for
manual). Detailed and densely written explanations about individual tools, they were designed as help files for programmers decades ago. Man pages include the various
flags (often also called
options) which extend the properties of a given command.
Bodhi Linux not only includes
man pages as a source of help, but also the GNU Project's
info pages, which are another interpretation of the man pages.
For the insatiably curious, you can read the tutorials “How Do I Use a Man Page?” and “How do I Use an Info Page?”
Commands typed inside of LXTerminal are specific to the directory you are currently inside. For example, typing
ls in your home directory will give you a different display than if you were inside of the
ls command after simply opening LXTerminal results in a simple list of all directories and files in your home directory.
ls stands for the word
list, as in: “list all of the directories and files in the directory I am currently inside.”
ls also has several
flags which extend the usefulness of
ls -l results in a different output, with
-l meaning “use a long listing (detailed) format.” With the
-l flag, you have information in seven different fields, beginning with permissions on a file or directory, to how many links exist for it, followed by the name of its owner and more. A complete explanation will be given in “Using the LXTerminal and Bash.”
Meanwhile, experience is the best teacher, so let's begin.
$ prompt, a cursor is waiting for input. Type:
ls and press the Enter key.
LXTerminal is set to color output by default in Bodhi. Directory names appear in blue to distinguish them from files. Files appearing in white text are readable and writable. Files in green text are readable, writeable and executable. Files in aqua represent a soft link. Files in white text set against a blue block are hard links.
A soft link is also referred to as a symlink, for “symbolic link.” You may be familiar with the term used by the Microsoft Windows family of operating systems, which is “shortcut.”
If the original file the soft link is set to is deleted or if it's file path is changed in any way, the soft link becomes an “orphan.” If you attempt to open the file, a message complaining “No such file or directory” appears. If you then run the ls command, the soft link itself will still appear, but only because the soft link itself still exists. It no longer points to the file it represents.
The details of hard links are more complex and are addressed in “Beyond Bash Basics.”
Other than ls, one of the most common commands you will use is the
cd stands for the term
You may already be asking why these commands are cryptic. The answer is that in the early 1970's, computer memory was extraordinarily expensive. Typing
ls used fewer bytes than
cd used fewer bytes than any other variant. One text editor was named
e, for example. Later versions of Unix and Unix-like systems continued with the laconic approach of naming both utilities and their options.
You may also have wondered about the use of “Unix-like” and “*nix.” Unix became a trademarked name. Operating system with Unix-like features can officially *use* Unix if the current trademark owners sell a license for its use. So work-arounds including Unix-like, unixy (as in “unixy goodness”) and *nix (using the wildcard character * in place of the “U”) are commonplace.
But back to the subject at hand. The only way to experience
cd is to use it.
$ prompt, type:
cd /usr and press the Enter key.
The terminal will respond with:
bin games include lib local sbin share src
Each entry is in blue text, so you already know these are directories.
$ prompt, type:
cd share and press the Enter key.
From this point forward, press the Enter key after typing in a command without being prompted to do so. Only if the key is not to be pressed will a reference be made to it.
$ prompt, type:
A surprising 182 directories will scroll through the terminal's window. You can use the scrollbar to move up and down the window, but there is another tool available to keep any output to the terminal available one-screen-at-a-time.
We are about to extend our use of
ls by combining it with another utility: the
less is a program that in ordinary use sends its result to the terminal. Just as
ls lists files,
less makes it possible to read the output of
ls (and any other text output) in a direct manner.
“Output” describes the term “standard output,” referring to the console: in this case, the terminal. Likewise, “standard input” refers to keystrokes as they are recognized by the computer. Mouse clicks, however, are described by the term “event driven” when you see that phrase.
The vertical symbol
| is called a
pipe. What you are about to do is to “send” the output of ls not to the terminal, but to the program
less, which will apply its default formatting to ls and then send the results to the terminal.
$ prompt, type:
ls | less
ls | less the same output will now appear in a single column. At the bottom of the terminal window, the symbol
: appears as the sole indicator that the output continues beyond the bottom of the window.
When the Enter key is pressed once, the next line at the bottom of the screen will appear and the topmost line will scroll up beyond the window.
Press the Enter key three times and three new lines will appear at the bottom of the window. Press the Enter key seven more times and seven new lines will appear, until the end of the file is reached when the last line to appear is:
At any time during the listing, or at the end of the file itself, you can press the
q key and return to the command prompt.
Another standard utility on your system is the
more is the predecessor to
less and performs much as
less does. Since
less was programmed with abilities not included in
more, the name
less is a play on words, with
more (in the sense of capabilities).
Much of Linux contains similar wordplay.
more for yourself, at the
$ prompt type:
ls | more
The only obvious difference is that instead of the
: symbol at the bottom of the page, the word
–More– appears. Press Enter until you reach the end of the file and you will notice your command prompt simply returns to you instead of having to explicity use the letter
q to quit the program.
Here is one more example of using a pipe before moving on.
Several paragraphs ago, I mentioned there were 182 directories in
/usr/share. I didn't count them, but used the program
wc, which means “word count.”
$ prompt, type:
ls | wc
The output reveals in my terminal the following cryptic line:
182 182 1748
If at the
$ prompt, you were to type:
man wc all is revealed through a quick read. The first field is the count of each newline. In this case, it is 182. The second field is the word count, which also happens to be 182, because each line has only one word. Even when a directory is named
libparse-debianchangelog-perl, it's a single word, with a dash to keep from a space being in the directory name. The final field is the byte count, which is superfluous for our purpose. If you use
wc to get the word count of a thesis or paper or manuscript, it is the second field you'll be interested in.
Speaking of which, typing at the prompt:
wc name-of-my-file is how you get the result for the word count of a text file. There's no pipe involved, but you do have to be in the same directory as the file you're wanting
wc to find. Which brings us to:
Other than the visual cue from LXTerminal:
showing the user
sam on a computer named
shire is in the
/usr/share directory, not all shells report this information. If you find yourself looking at nothing but:
how do you know where you are? The answer lies in the
pwd command, which stands for
print working directory. If sam issues pwd like so:
The terminal will reply:
Finally in this introduction to the terminal and bash, here is why you can have spaces in file names and directory names, but shouldn't.
Unix-like operating systems reserve special significance for some characters. One character is the space. Another is the backslash. The space is designated as a file seperator, so in Unix-speak,
means to the system: there are 2 files here –
If you do an
ls, it appears as if
file 1.txt is being reported as a single file. But at the command prompt if you use the command
find and type:
sam@shire$ find file 1.txt
the terminal will report:
find: `file': No such file or directory
find: `1.txt': No such file or directory
But if you surround your request with a double-quotation like this:
sam@shire$ find “file 1.txt” the terminal will report:
Common replacements for the space include using the dash, the underscore, and the period, as in:
With a few basics explained and a glimpse into the soul of the practical nature of *nix operating systems, you're ready for a whirlwind tour of “Using the LXTerminal and Bash.”