• Aucun résultat trouvé

Command-line or GUI?

Dans le document Prepared exclusively for J.S. Ash (Page 25-35)

Ubuntu might be described as an operating system with a dual nature.

For many administrative tasks you can use GUI programs that are usu-ally provided on the the System -> Administration menu. Or you can do

2. The configuration files in your/homefolder 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.gnome2folder.

COMMAND-LINE ORGUI? 26

One giant file system

Linux is one giant file system. If you attach a new piece of hard-ware to the system, it’s made accessible as a virtual file in the /devfolder. The system “talks to you” by providing virtual files in the /proc folder containing information about what it’s doing.

As a user, evenyoumanifest 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 funda-ments 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 → Acces-sories→Terminal. Then typesudo 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. Thecatcommand you issued displays the tents of a file on the screen, and you told it to display the con-tents of the file that’s magically plumbed-through to the mouse hardware. So waggling the mouse causes data to appear on-screen.

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 Pack-age ManPack-ager program. Or you can install it by typingsudo apt-get install epiphany-browser. This applies equally to trivial things such as file man-agement. You can delete that file on your desktop by dragging it to the Trash, or you can typerm ~/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. Tip36, on page93, describes a workaround for this.

COMMAND-LINE ORGUI? 27

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 aC:\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 conversa-tion. Some folks refer to it as theshell. I tend to refer to the command-prompt orcommand-line.

The software that provides the command-line is called the Bourne Again Shell but this is always abbreviated tobash.bashsimply 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 asterminal). 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 func-tion 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’tstrictlytrue 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.

COMMAND-LINE ORGUI? 28

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+Alt+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 Figure2.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 typingexit. 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.

COMMAND-LINE ORGUI? 29

Then switch back to the GUI (Ctrl+Alt+F7) and start GNOME Termi-nal by clicking Applications -> Accessories -> TermiTermi-nal.

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:

keir@keir-desktop:~$

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 thehostname, 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 calledkeir-desktop. If I logged in as the user calledjane, the prompt would readjane@keir-desktop:~$.

Following this is a colon. That separates what we might call the “loca-tion” 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/keir

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 automati-cally).

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

COMMAND-LINE ORGUI? 30

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 nav-igate. The Tab key moves the selection highlight from entry to entry, and Space normally confirms the selection of that partic-ular option. Alternatively you can use the cursor keys to move the selection highlight around. When you’ve finished select-ing the options on any particular screen you should move the selection to theOKbutton and hit Space to select it. Alterna-tively, 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 theOKbutton. 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:

keir@keir-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 calledarguments—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 typedcd Desktop.cdis the command, whileDesktopis 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 calledFilename.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.

COMMAND-LINE ORGUI? 31

Figure 2.4: Using command options

Some commands also take options, which modify how the command works. For example, thelscommand (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 tellscdto switch back to the parent folder) and typels. 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 makelsprovide that by using the-lcommand 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 exam-ple from my test PC is show in Figure2.4. Often two or more command options are used together—the -aoption tellslsto list hidden files too, so a command commonly typed by Linux users isls -la.

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

COMMAND-LINE ORGUI? 32

$ ls --help

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 thelscommand, you would type the following:

$ man ls

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 calledstandard 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 list-ing. 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.bashincludes 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, orstderr. 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, use2>, instead of a single right-facing angle-bracket.

COMMAND-LINE ORGUI? 33

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 directo-ries.

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

COMMAND-LINE ORGUI? 34

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 locatedatabase as described in Tip77, on page134, 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 becausesortsends its output to stdout, unless told otherwise. Many commands do this and it might seem stupid but it reflects the deliberate simplicity ofbash. To get around it, you need to redirect the output ofsortinto 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 ourlisting.txt file, while

SOFTWARE INSTALLATION AND MANAGEMENT 35

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 thelscommand intoless, the text viewer:

$ ls | less

In other words, rather than sending the file listing to the screen, it’s piped into thelesscommand, 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 pro-grams have.

Couldn’t we just redirect the output into less—something like ls > 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. Thels > lesscommand would create a new file called

Couldn’t we just redirect the output into less—something like ls > 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. Thels > lesscommand would create a new file called

Dans le document Prepared exclusively for J.S. Ash (Page 25-35)