Commandline or GUI

Ubuntu might be described as an operating system with a dual nature. For many administrative tasks you can use GUI programs that are usually provided on the the System -> Administration menu. Or you can do

2. The configuration files in your /home folder are owned by you, rather than root. This is because they usually relate to your personal settings, such as those for the GNOME desktop. These configuration files are usually hidden, which is to say, their filenames are preceded with a period (.). Most are stored in the .gnome2 folder.

One giant file system

Linux is one giant file system. If you attach a new piece of hardware to the system, it's made accessible as a virtual file in the /dev folder. The system "talks to you" by providing virtual files in the /proc folder containing information about what it's doing. As a user, even you manifest as a handful of files on the system.

Linux is a giant file system because it reduces everything to the same level so things can be accessed and manipulated in a logical and structured way. The concept is one of the fundaments of Unix, which Linux is based on.

That everything really is a file can be demonstrated by opening the file that the mouse is plumbed through to—/dev/psaux. Start by opening a terminal window—click Applications ^ Accessories ^ Terminal. Then type sudo cat /dev/psaux. You'll need to type your password when prompted. Then waggle the mouse around a little. The screen will fill-up with junk. The computer might beep too. The cat command you issued displays the contents of a file on the screen, and you told it to display the contents of the file that's magically plumbed-through to the mouse hardware. So waggling the mouse causes data to appear onscreen.

When you've finished, just close the program window.

exactly the same thing by typing at the command-prompt. For example, you can install the Epiphany web browser using the Synaptic Package Manager program. Or you can install it by typing sudo apt-get install epiphany-browser. This applies equally to trivial things such as file management. You can delete that file on your desktop by dragging it to the Trash, or you can type rm -/Desktop/filename.3

Which should you use? The choice is yours. The command line is often far more efficient, but can be arcane, especially for beginners. Yet it's where the real power lies. GUI tools make things simpler, but often at the expense of flexibility in the form of configuration options.

This book prefers to use the GUI tools whenever possible. It's only when

3. Note that the command-line doesn't have a trash facility. Once a file is deleted, it's gone forever. Tip 36, on page 93, describes a workaround for this.

something isn't possible via GUI software that we delve into command line tools. This is very much in keeping with the spirit of Ubuntu which is, after all, "Linux for human beings".

But that's no excuse for not having at least some command-line skills. So how does the command line work? I'm glad you asked...

How the command line works

Let's get one thing straight from the start: the command-line is just one more way to administer your system or run software. You don't have to use it if you don't want to, although there are a few tasks where it's unavoidable.

we'd better clear-up another misconception too: the command-line isn't some ethereal presence, always running in the background.4 This isn't like Windows 3.1, where everything "sat on top of' DOS, which was always there ready to offer a C:\ prompt. In Ubuntu, everything sits on top of a central program called the kernel. If the command-line isn't being used then the command-line isn't running.

One last thing—how to refer to the command-line in polite conversation. Some folks refer to it as the shell. I tend to refer to the command-prompt or command-line.

The software that provides the command-line is called the Bourne Again Shell but this is always abbreviated to bash. bash simply lets you enter commands, and manipulate files, and see the output once you've done so.

Let's start a command-line session and see what it looks like. There are two ways of doing this—either running a virtual console, or using GNOME Terminal (usually referred to simply as terminal). Both provide access to exactly the same thing—think of them as doors into the same room.

You can start a virtual console by holding down [Ctrl ]+[Alt]+[f2) (or F3, F4, F5 or F6—there are six in total, corresponding to the first six function keys, and all can be running at the same time, but the first—F1—is used for log/debug output and so is best avoided).

4. It isn't strictly true that the command-prompt isn't always running; the program that provides the command login, getty, is always running, and... What's that? Your head is about to explode? OK. I'll ease off the pedantic explanations.

Ubuntu 8.04 keir-desktop ttyE

keir-desktop login: keir Password:

Linux keir-desktop E.6.£4-19-gener ic #1 SMP Hed Jun 4 16:35:01 UTC EOOB i686

The programs included with the Ubuntu system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law.

To access official Ubuntu documentation, please visit:

http://help.ubuntu.com/

[email protected]:~$ Is

Examples [email protected]:_

Figure 2.3: The virtual console provides a command-prompt

You'll notice that the desktop and all signs of GUI-ness disappear. Don't worry. You can get it all back by hitting [Ctrl)+[Alt]+[F7. Give it a try. Then hit [Ctrl)+Al t)+[F2 to get back to the virtual console.

What you'll see will be something like this: Ubuntu 8.04.1 keir-desktop tty2

keir-desktop login:

You must type your username and then, when prompted, type your password. Then you'll see the command-line prompt, followed by the familiar cursor, as show in Figure 2.3.

As mentioned, you can also use the terminal program from within the GUI desktop to get a command-line. This is far more convenient,5 so quit your virtual console by typing exit. This will log you out (but only out of that virtual console—you'll still be logged into your desktop).

5. So when should you use a virtual console, and when should you use a GNOME Terminal window? This is answered by situation and circumstance—the only time you need to use a virtual console is when you have no choice. For example, if a program crashes and locks up the GUI, you can switch to a virtual console to fix things. However, for some Linux old-hands the virtual console is simply the first port of call when it comes to typing commands. Each to their own.

Then switch back to the GUI ([Ctrl ]+[ATt]+[F7)) and start GNOME Terminal by clicking Applications -> Accessories -> Terminal.

No login is needed this time around because you've already logged into the desktop, and the terminal program runs "on top" of that. However, what you see is exactly the same prompt you saw earlier on the virtual console, and you can do exactly the same things.

Let's take a closer look at the prompt. Here's what the one on my test system looks like:

It looks complicated but isn't. The first part, before the @ sign, is the username I'm logged in as. My username is keir. The part after the @ sign is the hostname, which is to say, how the computer refers to itself and is referred to by other computers on a network.

So if we "read" the prompt from left to right, it says that the user keir is logged in at (@) the computer called keir-desktop. If I logged in as the user called jane, the prompt would read [email protected]:~$.

Following this is a colon. That separates what we might call the "location" part of the prompt from the rest of it, which tells us where we currently are in the file system—what folder we're currently browsing. It appears we're browsing the ~ folder. What? Don't worry. The tilde symbol is command-line shorthand. It means that you're currently in your /home folder, which is where you'll always be dumped when you start a new command-line prompt. You can confirm the folder you're in by typing pwd, which stands for Print Working Directory (directory being another term for folder). Give it a try. Type pwd and hit [Enter). Here's what I see on my test PC: $ pwd

/home/kei r

Finally, at the end of the prompt line, is the dollar sign. This tells us we're logged in as a normal user. If we logged in as root, it would change to a hash (#) but, as mentioned, this is moot as far as we're concerned because Ubuntu doesn't allow root user login (with a notable exception: if you use Ubuntu's rescue mode, you're logged in as root automatically).

Throughout this book all commands I want you to type are listed with a dollar sign before them. Some will be preceded with a hash symbol too if the rescue mode is being used. This is just one of the conventions

Navigating text-mode menus

Not all command-line programs provide straight output. Some invoke simple text menus, like you might be used to in any GUI application but more primitive. You usually can't use the mouse to click on entries and instead must use the keyboard to navigate. The [Ta^ key moves the selection highlight from entry to entry, and [Space normally confirms the selection of that particular option. Alternatively you can use the cursor keys to move the selection highlight around. When you've finished selecting the options on any particular screen you should move the selection to the ok button and hit [Space] to select it. Alternatively, on some more simple menus, you can move the selection highlight to your choice in the list and just hit [Enter], which will both make the selection and hit the ok button. [EsC will usually quit the program without making any changes.

of computing literature. It doesn't mean you need to type the dollar or hash sign.

Back to the command-line tutorial. If you switch to the Desktop folder by typing the following:6

$ cd Desktop

.. .you'll see that the prompt changes to something like the following:

kei [email protected] r-desktop:~/Desktop$

So we're browsing in our Desktop folder in our personal /home folder (represented by a tilde symbol). Again, prove this if you want by typing pwd.

Some commands need what are called arguments—you need to tell the command what file or folder you want it to work with. We've already seen an example of this when we typed cd Desktop. cd is the command, while Desktop is the argument.

6. Capital letters matter under Ubuntu, unlike with DOS/Windows, where they're optional. If a filename, folder or command has a capital letter in then you must type it. You could feasibly have files called Filename.doc, filename.doc, FILEname.doc, and so on, all in the same folder. Virtually all commands are entirely lower case and should be typed as such. Adding capital letters will mean they won't be recognized.

File .Edit View Terminal Tabs Help [email protected]:~i Is

Desktop Documents tlxanoles ftisic Pictures Public TempLates Videos [email protected]:-$ Is -I total 28

drwxr-xr-x 2 keir keir 4096 2008-06-04 05:41

drwxr-xr-x 2 keir keir 4096 2008-05-21 11:30 Do nenl

Irwxrwxrwx 1 keir keir 26 2008-05-21 12:19 Examples -> A

ent drwxr-xr-x 2 keir keir 4096 2008-05-21 11:30

drwxr-xr-x 2 keir keir 4096 2008-05-21 11:30 :tures drwxr-xr-x 2 keir keir 4096 2008-05-21 11:30 alic drwxr-xr-x 2 keir keir 4096 2008-05-21 11:30 Template:

drwxr-xr-x 2 keir keir 4096 2008-05-21 11:30 deos keir<dkeir-desktop:~i

Figure 2.4: Using command options

Some commands also take options, which modify how the command works. For example, the Is command (ls being short for list) will give us a file and folder listing. Let's try this. First, switch back to your /home folder (cd ..—the two periods tells cd to switch back to the parent folder) and type ls. Here's what I saw on my test computer: $ ls

Desktop Documents Examples Music Pictures Public Templates Videos

There isn't any information about file permissions here, and we can make Is provide that by using the -I command option. Command options are usually inserted after the command, so you would type: $ ls -l

If you type that, you'll get a long list of filenames/directories on the right, with their permissions and ownership rules on the left. An example from my test PC is show in Figure 2.4. Often two or more command options are used together—the -a option tells Is to list hidden files too, so a command commonly typed by Linux users is Is -la.

You can usually get a list of a command's most popular options by typing --heIp after the command (that's two dashes before heIp). For example, to get a list of the Is command's options, you would type:

Figure 2.5, on the next page shows some typical commands. These make up the meat of day-to-day operations at the command prompt when it comes to handling files. There's lot more to each command, of course, and a good tip is to use the man (manual) command. This provides useful information about what a command does and how to use it. For example, to learn about the ls command, you would type the following:

More advanced command-line skills

It might sound obvious but all bash does is take input, usually in the form of commands you type, and then output something. It's like a production-line machine—input goes in at one end and output comes out at the other end.

Technically speaking, the input of the machine is called standard input (stdin) while the output is called standard output (stdout).7 Stdin is usually your keyboard and stdout your monitor but that doesn't have to be the case. The rather cool thing about bash is that it doesn't care what stdin and stdout actually are.

Why is this important? Well, the output and input a command can be redirected. A command can be fed the contents of a file instead of what you type. Alternatively, the output of a command can be redirected into a file, rather than sent to the screen. Angle brackets are used for the purpose of redirecting.

Let's say that you wanted to create a text file showing a long file listing. All you'd to is type ls -l > listing.txt. Imagine the angle bracket as a funnel—the ls -l command pours its output into the listing.txt file (after first creating the file, of course).

Let's also say that you have a shopping list and want to sort it into alphabetical order. bash includes a handy command that can do this— sort—but you have to make it take your file as input. The following will do the trick:

7. Alongside stdout there's actually a second output—standard error, or stderr. This is simply the error output of a command (if there is any). It is usually sent to the screen, like stdout, but it too can be redirected. To redirect stderr, use 2>, instead of a single right-facing angle-bracket.

ls List files and folders in current folder.

-l : Provide long listing, including all details of files -a : Show all files, including those that are hidden -h : Provide "human-readable" file sizes (KB, MB, GB) Example: ls -l cd Change folder; cd .. changes to parent folder. Example: cd /home/keir cp Copy file and/or folder. First specify the file and then the new location.

-r : Copy directories too (otherwise directories will be ignored). Example: cp /home/keir/file.doc /home/keir/Desktop/

mv Move file and/or folder. First specify the file and then the new location. By specifying a new filename, mv can also be used to rename. Unlike cp, no need for additional -r option for directories.

Example: mv /home/keir/myfile /home/keir/Desktop/ Example: mv oldfilename newfilename rm Delete file and/or folder.

-r : Delete directories too (otherwise directories will be ignored) -f : Don't prompt for confirmation Example: rm -rf Desktop/newfolder/

mkdir Create folder.

Example: mkdir newfolder less Display text file. Use up/down cursor keys to scroll and hit Esc to quit.

Example: less file.txt

Figure 2.5: Typical day-to-day file management commands bash: The silent type

A common complaint from Linux newbies is that a command hasn't worked when, actually, it has. They base this assumption on the fact that the command "didn't seem to do anything". For example, if I update the locate database as described in Tip 77, on page 134, here's what I'll see:

$ sudo updatedb $

In other words, all I'll see at the end of it is the command prompt again. Nothing else. No message saying things went OK. This is because most commands only give feedback to you if (a) something goes wrong; or (b) giving feedback is what they're designed to do (for example, the ls command, which is designed to list files).

If you don't see any output after running a command then don't panic. It's a good thing.

$ sort < shoppinglist.txt

This is slightly less intuitive because the command comes first, and then the file is "poured into it", courtesy of redirection of input.

If you were to try this, you'd find the shopping list would indeed be sorted but it would also appear on-screen. This is because sort sends its output to stdout, unless told otherwise. Many commands do this and it might seem stupid but it reflects the deliberate simplicity of bash. To get around it, you need to redirect the output of sort into a new file: $ sort < shoppinglist.txt > sortedlist.txt

What about if you wanted to create a text file that contained file listings of several different directories? You could create several text files using redirection and combine them manually but a better plan is to type something similar to the following:

$ ls -l > listing.txt $ ls -l /etc >> listing.txt $ ls -l /bin >> listing.txt ... etc.

Two angle brackets mean that the file is added to, rather than created anew. The first command above initially creates our listing.txt file, while the two following commands add their output to the end of it.

The output of one command can also be piped into another, which is to say, the output of one command can form the input of another. The pipe symbol (|) is used to do this. The most basic example is to pipe the output of the Is command into less, the text viewer:

In other words, rather than sending the file listing to the screen, it's piped into the less command, so is then displayed on-screen for leisurely scroll-through reading. This can be very useful when you're working at a virtual console, which lacks those handy scroll bars that GUI programs have.

Couldn't we just redirect the output into less—something like Is > less? No. Redirecting is about sending data into (or from) files. With piping the output is simply transferred from one command to another without the creation of a file. The ls > less command would create a new file called less which contains the file listing.

Piping is often used with the grep command, which is able to search through a file for text. For example, say you want to make a list of all the files belonging to a particular project you're working on. The files are in your Documents folder, along with hundreds of others (for the purposes of this demonstration you aren't very well organized). One thing is true of all the project files—they have the word "project" in their filename, but that could be at the end, or in the middle, or at the front. If you were an idiot you could type ls and the scroll through the list of results looking for relevant files. However, a better way is to get grep to search for you. We can do this by piping the output of ls straight into it: $ ls | grep -i "project"

What you'll see, as seen in Figure 2.6, on the next page, is the output of grep which has filtered the output of ls to show only the lines that contain the word "project" (the -i command option tells grep to ignore upper and lower case letters when searching).

Was this article helpful?

0 0

Post a comment