Introducing the Linux Shell - Covering the Basics - BeagleBone For Dummies (2015)

BeagleBone For Dummies (2015)

Part II

Covering the Basics

Visit www.dummies.com/extras/beaglebone for additional Dummies content on using a multimeter.

In this part …

· Exploring the Linux world and getting acquainted with the Linux Shell

· Discovering electricity: the equations, circuit diagrams, and various components

· Taking a closer look at your BeagleBone and expanding its functionalities

Chapter 4

Introducing the Linux Shell

In This Chapter

Getting acquainted with Linux’s superuser

Using the Linux Shell to manage files and directories

Installing and managing software on your BeagleBone

Personalizing your BeagleBone by changing its name and your password

Working efficiently with the shell

At a first glance, the Linux operating system (OS) may look like a weirder, more complex, and less pretty way to do the things that the beloved Windows and Mac OS X are capable of. Often, it’s said that Linux isn’t a user-friendly OS, which explains why a typical computer user who simply wants to browse the web, for example, prefers to use Windows or Mac OS X.

The added complexity of Linux, however, paves the way for heavy customization and versatility, as well as user efficiency. These perks, along with the fact that Linux is free, has led to the existence of a huge community of users who are keen on constantly improving the system. Ultimately, Linux has become the best option for embedded systems such as the BeagleBone.

This chapter shows you how you can use the command prompt to get around a computer in a way that you’re probably not used to: using only text. You can forget about using a graphical user interface (GUI) for a while. Although using the command line may seem odd at first, this approach has plenty of advantages, as you see in this chapter.

Examining the Prompt

To open the command-line prompt, start by connecting to your BeagleBone via SSH. You can do that by either connecting the BeagleBone to your computer via USB or by connecting the BeagleBone to your router using an Ethernet cable. If you have previously worked through Chapter 3, the steps presented here are similar to the steps in that chapter except that this time you are logged in as debian instead of root.

If you connected by USB, do one of the following:

· Windows: In Windows, while using PuTTY, choose SSH and type 192.168.7.2 at the Host Name (or IP address) dialog box. A login prompt should appear a few seconds later: Type debian as the username and temppwd as the password.

· Linux or Mac OS X: If you’re using Linux or Mac OS X, simply type sudo ssh debian@192.168.7.2 in the terminal window and then type temppwd as your password.

If you connected by Ethernet, do one of the following:

· Windows: In Windows, while using PuTTY, choose SSH and type beaglebone at the Host Name (or IP address) dialog box. A login prompt should appear a few seconds later: Type debian as the username and the password temppwd.

· Linux or Mac OS X: If you’re using Linux or Mac OS X, simply type sudo ssh debian@beaglebone.local in the terminal window and then type temppwd as your password.

You run the show from the terminal window. This is where you type commands to accomplish various tasks, from organizing your files and directories (by creating, removing, copying, or moving them) to compiling and running programs you’ve written.

Your BeagleBone’s prompt should look like Figure 4-1.

Figure 4-1: Logging in as Debian.

The last line on the screen shown in Figure 4-1 reads debian@beaglebone:~$. You need to understand a few things from that single line:

· The first part, debian, is the user you’re logged in as. Under other circumstances, that name could be Richard or OfficePC-4.

· The next part, beaglebone, is the hostname. If you’re connected in a network, that name is the name your BeagleBone displays to other computers. You find out how to alter that name in the “Changing the Hostname and Password” section later in this chapter.

· After the colon is the current working directory — the folder that you’re currently inside — so all commands that you type, unless you specify otherwise, refer to files inside that directory. In this case, you see a tilde (~) because that’s shorthand for the logged-in user’s home directory. When you’re logged in as debian, the home directory is /home/debian. Another very important user, called root, is the administrator of the system and is known as superuser in the Linux OS. We talk more about the root account throughout this chapter.

· The last part is the prompt for input, which is $ in this example because you’re logged in as a regular user. When you’re logged in as root, the prompt is #.

Introducing the root superuser

In the Linux community the administrator of the system is called the superuser. This section introduces you to the superuser.

In Chapter 3, you log in as root, which is the superuser of most Linux systems. root is used for privileged tasks, such as installing and updating software, messing around directories with restricted access, and controlling the BeagleBone’s input and output pins.

In this chapter, you don’t need to log in as root, but the root user shows up throughout this chapter and the remainder of this book. Actually, even when you’re not logged in as root, you run programming scripts and commands with root permission. As such it’s important that you know about what the root superuser is capable of.

To log in as a superuser, type the following command:

debian@beaglebone:~$ sudo su
root@beaglebone:/home/debian#

By default, no password is required to log in as root, but if you’ve defined one, you’re prompted to type it.

As superuser, you have the power to do practically whatever you want. Cool. But that also means the system won’t protect you from yourself: If you’re careless, you may make changes on your board that will be difficult to rectify! For that reason, it’s often considered to be hazardous to work as root. Generally, you should only log in as root when absolutely necessary.

As previously mentioned, for the remainder of this chapter you don’t need to be logged in as root — although some sections prompt you to execute commands with root access (read more on this later in the “Managing Software on Your BeagleBone” section). You should only exercise the superuser’s powers when whatever it is that you tried to do as a regular user didn’t work.

The default username and password are debian and temppwd, respectively. You find out how to change the username and password in the “Changing the Hostname and Password” section later in this chapter. Log in as a regular user by entering the following command:

root@beaglebone:~# login debian
Password:
Last login: Wed Apr 23 20:21:20 UTC 2014 on pts/0
Linux beaglebone 3.8.13-bone47 #1 SMP Fri Apr 11 01:36:09 UTC 2014 armv7l

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

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
debian@beaglebone:~$

The password text isn’t visible while you’re typing (someone may be looking!) so simply press Enter or Return when you’re done typing.

Exploring the Linux File System

It’s time to play around with the command line. For starters, type pwd, (which means print working directory):

debian@beaglebone:~$ pwd
/home/debian

The output is /home/debian. The first forward slash (/) is the root of the file system (which is not the same as the root user!). Forward slashes are always used to indicate folders and files within other folders. In this case, the current working directory is debian, which is inside home, which is inside the root of the file system. Here, debian is the username with which you are logged in.

Even though the directory home is one level above the directory debian, the so-called “home directory” represented by the tilde (~) is actually /home/debian.

The commands in Linux are case-sensitive, which means that PWD, PwD, pWd, and any other variations are completely different from pwd. The same holds true for all other commands and for any code written in the programming languages addressed in this book.

Listing files and directories

If you type the command ls, a list of the files and directories within your working directory is printed. Right now, you should be inside your home directory, which should have the directories Desktop and bin:

debian@beaglebone:~$ ls
bin Desktop

This directory is the same as the desktop on your general-use computer; it holds the files that appear on the desktop when you use the GUI. We cover this topic in more detail in Chapter 12.

In reality, the complete syntax for the ls command (and for a great many other commands) is

ls [OPTIONS] <filename>

Don’t be confused by the fact that we use <filename> when <directoryname> would make more sense. We use <filename> for two reasons:

· The Linux OS interprets everything that exists on your computer as a file, from your hard drive to a photograph you just uploaded to your hard drive to your keyboard. In this case, a container of files, also known as a directory, is itself considered to be a file.

· <filename> That is the general syntax for many commands that deal with files in general.

When typing commands, you can also use ~ as shorthand for /home/root. Also keep in mind that the first forward slash that appears when you type pwd (that is, the root of the file system) is, for all practical purposes, a folder like any other; thus, it can be accessed by its name just like any other folder.

By default, ls with no options and no directory specified prints a list of the files and directories of your current working directory. You can specify exactly which directory you want to be listed, such as Desktop (which doesn’t have anything in it at the moment) or the root of the file system:

debian@beaglebone:~$ ls Desktop

Here is the root:

debian@beaglebone:~$ ls /
bin dev home lost+found mnt proc run selinux sys usr
boot etc lib media opt root sbin srv tmp var

Many options are available for the ls command. A rundown of all of them isn’t necessary to get through the remainder of this book, but you should know about one useful option: -l, which makes the ls command print its list in a long format. Here’s an example:

debian@beaglebone:~$ ls -l
total 8
drwxr-xr-x 2 debian debian 4096 Apr 23 20:27 bin
drwxr-xr-x 2 debian debian 4096 Apr 23 20:21 Desktop

debian@beaglebone:~$ ls -l /
total 76
drwxr-xr-x 2 root root 4096 Apr 23 20:35 bin
drwxr-xr-x 3 root root 4096 Apr 23 20:36 boot
drwxr-xr-x 14 root root 3600 Apr 23 20:20 dev
drwxr-xr-x 103 root root 4096 Jun 30 22:31 etc
drwxr-xr-x 3 root root 4096 Apr 23 20:57 home
drwxr-xr-x 15 root root 4096 Apr 11 01:41 lib
drwx------ 2 root root 16384 Apr 23 20:21 lost+found
drwxr-xr-x 2 root root 4096 Apr 23 20:20 media
drwxr-xr-x 2 root root 4096 Feb 3 10:24 mnt
drwxr-xr-x 5 root root 4096 Apr 23 20:36 opt
dr-xr-xr-x 117 root root 0 Jan 1 1970 proc
drwx------ 3 root root 4096 Apr 23 21:02 root
drwxr-xr-x 23 root root 740 Apr 23 20:20 run
drwxr-xr-x 2 root root 4096 Apr 23 20:40 sbin
drwxr-xr-x 2 root root 4096 Jun 10 2012 selinux
drwxr-xr-x 2 root root 4096 Apr 23 20:14 srv
dr-xr-xr-x 12 root root 0 Jan 1 2000 sys
drwxrwxrwt 8 root root 4096 Jun 30 22:17 tmp
drwxr-xr-x 10 root root 4096 Feb 21 04:24 usr
drwxr-xr-x 12 root root 4096 Apr 23 20:34 var

Note that because these options are optional, they’re always preceded by a dash (as in -l) so that the command knows that l is an option, not a file.

For now, you don’t need to get too caught up on all the information that this so-called long listing provides. It’s covered in more detail later in this chapter, in the “Using long-listing format and permissions” section.

Understanding the directory tree

When talking about how the file system is organized within a computer, the analogy of a tree makes complete sense: Just as a tree has a trunk from which branches sprout, and other branches in turn sprout from those branches, the Linux file system is a root directory (the trunk) that holds a bunch of directories that include other directories, which hold files.

Figure 4-2 illustrates the directory tree on your BeagleBone (and generally in any Linux OS). Note: The directory tree isn’t complete because it doesn’t include all the directories that branch off root; neither does it include their subdirectories or files. You can see, however, where home is relative to the other directories, as well as how you can get to Desktop.

Figure 4-2: A partial directory tree.

Changing directories

In many situations, you want to change the working directory of your system. You accomplish this task with the cd command. Generally, you type cd [name of the directory you want to go to]. You can use a couple of variations, or shortcuts, with the command.

Using the general case

The general case is cd [directory], as shown here:

debian@beaglebone:~$ cd Desktop
debian@beaglebone:~/Desktop$

By using pwd, you can verify that you’re now inside Desktop:

debian@beaglebone:~/Desktop$ pwd
/home/debian/Desktop


Discovering the root directory

The directories that are inside the root make up the core of your BeagleBone, as they contain everything that’s necessary for it to run — from all the regular programs and files on your computer to various configuration files and files that represent the devices on the system (such as a mouse or a disk). Most likely, you won’t find yourself working inside any of them very often, if ever. In case you’re curious, though, here’s a brief description of most of them:

bin: Short for binaries; contains programs and commands for the user. (The program ls is contained inside this directory, for example.)

boot: Contains the files necessary at boot time.

dev: Short for devices; stores a list of files that represent the devices on the system.

etc: Contains various configuration files.

home: Contains a directory for each user.

lib: Contains system libraries and drivers that are used by different programs of the OS.

lost+found: Contains files that were saved during system failures.

media: Stores the details of removable storage devices such as USB flash drives and microSD cards.

mnt: Represents the mount point for external systems. If you plug in your cellphone by using a Micro USB cable, for example, you’ll find the phone’s data in this directory.

opt: Normally contains third-party software and/or extra software.

proc: Contains files that represent information about the system, providing insight into matters such as the CPU (central processing unit) and the memory. This information, however, isn’t trivial to interpret.

root: Represents the home directory for the root user.

sbin: Contains programs that usually are usable only by the root user.

sys: Contains files directly related to the OS itself.

tmp: Stores temporary files; whatever’s in here is removed upon reboot.

usr: Contains programs and files accessible by all users.

var: Contains files whose size vary, such as databases and system log files.


To go back to /home/debian, you use a similar approach:

debian@beaglebone:~/Desktop$ cd ~
debian@beaglebone:~$

Moving up one directory

The command cd .. places you inside the current directory’s parent, as shown here:

debian@beaglebone:~$ pwd
/home/debian
debian@beaglebone:~$ cd ..
debian@beaglebone:/home$ cd ..
debian@beaglebone:/$

Moving to the previous directory

When you type cd -, you move to the directory you were previously in. Unlike other shortcuts, cd - also prints on the terminal the folder that you jumped to:

debian@beaglebone:/$ cd ~
debian@beaglebone:~$ cd Desktop
debian@beaglebone:~/Desktop$ pwd
/home/debian/Desktop
debian@beaglebone:~/Desktop$ cd -
/home/debian
debian@beaglebone:~$

Returning to the home directory

When you use cd by itself, you return to the home directory. This command is the same as cd ~:

debian@beaglebone:~$ pwd
/home/debian
debian@beaglebone:~$ cd /
debian@beaglebone:/$ pwd
/
debian@beaglebone:/$ cd
debian@beaglebone:~$ pwd
/home/debian
debian@beaglebone:~$

Understanding relative and absolute paths

So far in this chapter, we’ve talked about how you can go inside directories that are directly above or below one another in the directory tree, similar to the way you usually navigate a computer with a GUI, clicking your way through directories until you arrive at your destination. Clickable interfaces are intuitive and easy to use, which is why they’re considered to be user-friendly.

Imagine, however, that you want to access one folder that’s buried deep in the darkest recesses of your directory tree. Even with a GUI, you may consider it to be quite bothersome to navigate that far through the file system. Now imagine doing it with the command prompt and having to type the cd command so many times!


Understanding command line, prompt, and shell

The terms command line, prompt, and shell are often used interchangeably. Command line and prompt mean pretty much the same thing, but there’s a slight difference between them and shell. The command line or prompt is where you type the commands that the shell carries out. The shell is the brains of the operation, whereas the command line or prompt is simply where you tell the shell what task you want it to perform.


Fortunately, the Linux shell offers a solution through relative and absolute paths. So even though the command prompt might seem confusing and not as intuitive as a GUI, it provides tools that enable you to do work much faster than you can with a GUI.

The best way to understand absolute and relative paths is to view an analogy:

· Relative paths: Imagine that you’re following the directions someone gave you to arrive at your destination — a directory or a file. In that sense, the destination is relative to where you are right now: your current working directory.

· Absolute paths: Imagine that you don’t need to follow someone’s directions to get to a destination because you have the specific address; you put this address in your GPS system and get there directly. The absolute path is the exact, complete address of the file you want to access.

Absolute paths are always measured from the root, so they always start with /, followed by the complete list of directories you have to go through to arrive at your destination. (You used absolute paths in the preceding section with the commands cd / and cd ~.) Here’s an example:

debian@beaglebone:~$ cd /home/debian/Desktop
debian@beaglebone:~/Desktop$

The tilde (~) is short for /home/debian, so in the preceding example, you could also use cd ~/Desktop to achieve the same results. This command always gets you to Desktop, regardless of your current working directory. That’s what differentiates it from using a relative path. The computer knows that you’re using a relative path from the fact that you never start with / when writing one.

To go to a subdirectory somewhere below your current directory, list the path through the subdirectories you have to go through, separated with slashes(/):

debian@beaglebone:~/Desktop$ cd ..
debian@beaglebone:~$ cd ..
debian@beaglebone:/home$ cd debian/Desktop
debian@beaglebone:~/Desktop$

If you ever get confused while using relative paths, think about the analogy of following directions to a destination. Imagine that you’re in the boot folder and want to go to Desktop (refer to Figure 4-2). The directions would be “You have to go up one directory, then down to home, down again to Debian, and then down again to Desktop.” The following example shows you how those directions are written as commands. You start by getting inside the boot folder by using an absolute path and then get to Desktop by using a relative path:

debian@beaglebone:~/Desktop$ cd /boot
debian@beaglebone:/boot$ cd ../home/debian/Desktop
debian@beaglebone:~/Desktop$

Always use cd .. to go “up” and the directory name to go “down.” Here’s one last example:

debian@beaglebone:~/Desktop$ cd ../../../etc
debian@beaglebone:/etc$

You use a relative or absolute path based on which is more convenient at the moment. Normally, you should use an absolute path if the folder you’re currently in is very far away from the one you want to access. In the preceding example, it probably would have made more sense to use an absolute path, because the command could have been shortened to cd /etc. Otherwise, using a relative path may be more convenient.

Paths are extremely helpful and convenient, because with paths, you can do anything from anywhere. You can use them with any command to accomplish tasks in different locations from your current working directory, such as when you want to list the contents of debian while you’re at the root:

debian@beaglebone:/$ ls home/debian
bin Desktop

The preceding example uses a relative path, but the absolute path /home/debian renders the same result. After all, the root is the starting point of everything.

Checking file types

The file command followed by a filename gives you a brief description of the file you requested; how detailed the information is depends on each file, but the file type (directory, special file, and so on) is always shown. Because there aren’t many files on your BeagleBone yet, the example in this section uses the files in the directory /dev. The following succession of commands starts by changing to the /dev directory, listing it, and using the file command on those files:

debian@beaglebone:~$ cd /dev
debian@beaglebone:/dev$ ls
alarm loop0 ram12 tty16 tty43 ttyS2
ashmem loop1 ram13 tty17 tty44 ttyS3
audio loop2 ram14 tty18 tty45 ubi_ctrl
autofs loop3 ram15 tty19 tty46 uinput
binder loop4 ram2 tty2 tty47 urandom
block loop5 ram3 tty20 tty48 usbmon0 (...)
debian@beaglebone:/dev$ file alarm block log
alarm: character special
block: directory
log: socket

The previous example demonstrates that you can use a command with more than one file. The command is carried out for all the files you type in.

Some of the files within the directories of the root directories are very important for the system to run; therefore, you rarely find yourself doing anything inside them. Not working as a superuser guarantees safety, however, so nosing as described with the preceding code is perfectly safe.

These files don’t have many details to show you. If you had an image on your BeagleBone, using file on it would output details such as the format and resolution:

debian@beaglebone:~$ file firstView.png
firstView.png: PNG image data, 1920 x 1080, 16-bit/color RGB, non-interlaced

Creating directories

You’ve probably felt the need to create directories (or folders) when working on your standard computer. In Linux, you can create directories by using the mkdir command followed by the name you want to give the directory, like so:

debian@beaglebone:~$ mkdir project

Then you can use the ls command to verify that a new folder has been created:

debian@beaglebone:~$ ls
bin Desktop project

You can get inside this new directory with the cd command. Also, you can create several folders in a single use of mkdir, as shown here:

debian@beaglebone:~$ cd project
debian@beaglebone:~/project$ mkdir ListOfMaterials theoretical_stuff circuitSchematic Code_for_the_project
debian@beaglebone:~/project$ ls
circuitSchematic Code_for_the_project ListOfMaterials theoretical_stuff

We recommend that you avoid using spaces between words whenever possible. The preceding command is a good example of the wisdom of that guideline: If you’d typed mkdir List of materials, you would have created three folders — List, of, and materials — rather than one. If absolutely necessary, you can say “Hey Shell, the name of the file I want to access starts and ends here, not at the space” by wrapping the file in quotation marks. When you type mkdir “List of Materials”, you create a folder named List of Materials.

By default, the command prompt uses the space between words to separate the various inputs of a command for all commands, not just mkdir. Even though we advise you to avoid using spaces when naming stuff, you may come across something created by someone who didn’t think about this issue. In that case, you have no choice but to use the quotation-marks technique.

As with most commands, you can specify options to use with the mkdir command. The most useful one is -p, which enables you to create multiple directories within directories in a single line of code. Also, the -v option displays some additional information about the command you used, such as when each step of the command is completed. The following example illustrates these two options in action:

debian@beaglebone:~/project$ mkdir -vp stuff/schematics/datasheets
mkdir: created directory 'stuff'
mkdir: created directory 'stuff/schematics'
mkdir: created directory 'stuff/schematics/datasheets'


Naming conventions

The best way to deal with the difficulty of multiword naming is to concatenate multiple words into a single word. Running words together can make them hard to read, however, as in this example: anamelikethisquicklybecomesconfusing. It’s useful to be able to distinguish each word in the name. Every programmer has a preferred method. Possible conventions include using uppercase letters or underscores, as in aNameLikeThisIsNoLongerConfusing or a_name_like_this_is_not_confusing_either. Use the technique that feels most comfortable to you. You can also use hyphens, but that’s a less-common convention among programmers.


You can add multiple options to a command by concatenating them, as you saw in the preceding example.

Without the -p option, the shell would attempt to create the directory datasheets inside the stuff/schematics directory. Because this directory wouldn’t exist yet, an error would have resulted.

Take a moment to appreciate this use of mkdir. In standard use of a computer with a GUI, you’d have to go through the trouble of creating a folder, going inside it, creating another, and so on. Using the command line, however, you create a folder and a subfolder by typing a single line command. In many other situations, you can accomplish what would normally be tedious tasks simply in this straightforward fashion. As mentioned at the beginning of this chapter, at first glance using the Linux command line may seem to be less user-friendly and more confusing than using a GUI, but you can’t deny the added velocity at which you can complete simple tasks when you know your way around and understand the commands.

Creating, editing, and viewing text files

You can create an empty file by using the touch command, as shown here:

debian@beaglebone:~/project$ touch hello.txt
debian@beaglebone:~/project$ ls
circuitSchematic hello.txt stuff
Code_for_the_project ListOfMaterials theoretical_stuff

After you create the empty file, you can open it by using a text editor such as nano:

debian@beaglebone:~/project$ nano hello.txt

As expected, hello.txt is empty — that is, it has no text in it. Naturally, you can write whatever text you want, but leave well enough alone for now. You’re about to find out how to add text to a file by using — you guessed it — the command line. For now, exit nano by pressing Ctrl+X.

To store text in a file, use the following command:

debian@beaglebone:~/project$ echo 'Hello World!' > hello.txt

The touch command may seem to be a bit redundant when you’re dealing with .txt files because both of the preceding commands — writing something on nano or using echo — create a file if it doesn’t exist already, as shown in the following example:

debian@beaglebone:~/project$ echo 'Hello World!!' > hello2.txt
debian@beaglebone:~/project$ nano hello3.txt
debian@beaglebone:~/project$ ls
circuitSchematic hello3.txt stuff
Code_for_the_project hello.txt theoretical_stuff
hello2.txt ListOfMaterials

Note that hello3.txt appears only if you write something in it before exiting nano. If you don’t write something in the file before exiting nano, the file isn’t created.

To look at the contents of a file rather than opening it with a text editor such as nano, you can use the cat command as follows:

debian@beaglebone:~/project$ cat hello.txt
Hello World!

To append text to the end of a file, use the echo command as shown here:

debian@beaglebone:~/project$ echo 'Pleased to meet you!' >> hello.txt
debian@beaglebone:~/project$ cat hello.txt
Hello World!
Pleased to meet you!

To break down these last few new commands, the echo command is used to display on the terminal the text that follows it. Had you simply used it with text following it, you’d get a result like the following:

debian@beaglebone:~/project$ echo 'Hello!'
Hello!

In this example, no file is specified for saving the text, so the output is sent to the standard output, which is the terminal. The greater-than sign (>) is used to redirect that output where you want, such as to the beginning of an existing file or a file you want to create. Using two greater-than signs (>>) means that you want to redirect the output to the end of the specified file.

Finally, you should know that the real use of the command cat is to concatenate files and print the output. If you concatenate a file with nothing, as we’ve done thus far, cat simply outputs that file.

The following examples show you how you can use the cat command for what it was born to do:

debian@beaglebone:~/project$ echo 'Good bye!' > bye.txt
debian@beaglebone:~/project$ cat hello.txt bye.txt
Hello World!
Pleased to meet you!
Good bye!

You can redirect the output of a command by using the > sign, as follows:

debian@beaglebone:~/project$ cat hello.txt bye.txt > helloAndBye.txt
debian@beaglebone:~/project$ cat helloAndBye.txt
Hello World!
Pleased to meet you!
Good bye!

As with mkdir, commands aren’t limited to one file. You can specify them to work with as many files as you want, and the files are processed in order, as shown in the next succession of commands.

It’s interesting to note that the concept of redirecting your output applies to all commands. That means that any command that prints words in the terminal (the standard output) can be saved in a .txt file with the use of the > sign:

debian@beaglebone:~/project$ echo 'Hello!' 'Everything ok?' 'Bye!'
Hello! Everything ok? Bye!
debian@beaglebone:~/project$ echo 'Hello!' > hello2.txt 'How are you?' > hello3.txt 'See you later!' > bye2.txt
debian@beaglebone:~/project$ cat hello2.txt hello3.txt bye2.txt
Hello! How are you? See you later!
debian@beaglebone:~/project$ ls
bye2.txt hello3.txt stuff
bye.txt helloAndBye.txt teste2.txt
circuitSchematic hello.txt teste.txt
Code_for_the_project ListOfMaterials theoretical_stuff
hello2.txt
debian@beaglebone:~/project$ ls > list.txt
debian@beaglebone:~/project$ cat list.txt
bye2.txt
bye.txt
circuitSchematic
Code_for_the_project
hello2.txt
hello3.txt
helloAndBye.txt
hello.txt
ListOfMaterials
list.txt
stuff
teste2.txt
teste.txt
theoretical_stuff

Using the commands covered in this section, you can manipulate and work with files fairly easily. Sometimes, though, you may want to open the file and use a more direct approach to edit it; that’s what text editors are for. Try using nano again to view the contents of a text file (see Figure 4-3).

debian@beaglebone:~/project$ nano helloAndBye.txt

Figure 4-3: Editing a file by using nano.

Use the arrow keys to move around the file and make whatever alterations you want. You can save by pressing Ctrl+O or exit by pressing Ctrl+X. If you attempt to exit without saving, nano asks you whether you want to save; type Y for yes or N for no and then press Enter or Return. There are plenty of other tools within nano, but we don’t go into them all here. Press Ctrl+G to access the Get Help information.


Try experimenting

Play around! Each time you read about a new command and discover a new concept, feel free to try the new technique with one of the older commands, such as using ls with redirection. If something that you’ve read gives you an idea about something that may work, don’t hesitate to try it. If it doesn’t work, you’ll probably understand why later, and if it does work, you’ve taught yourself something new! Trial and error is a very effective way to find your way around a computer, and the fact that you aren’t working as superuser should guarantee safety.


Removing files and directories

To remove files, you can use the rm command:

debian@beaglebone:~/project$ ls
bye2.txt Code_for_the_project helloAndBye.txt list.txt
bye.txt hello2.txt hello.txt stuff
CircuitSchematic hello3.txt ListOfMaterials TheoreticalStuff
debian@beaglebone:~/project$ rm hello.txt
debian@beaglebone:~/project$ ls
bye2.txt hello3.txt stuff
bye.txt helloAndBye.txt teste2.txt
circuitSchematic hello.txt teste.txt
Code_for_the_project ListOfMaterials theoretical_stuff
hello2.txt list.txt

There’s no way to undelete files and directories when you delete them this way, so be cautious! The rm command also works for directories, but special care is required when you use it for that purpose; directories are special files, after all. If you attempt to remove a directory through the normal use of rm, this message would be the result:

debian@beaglebone:~/project$ rm ListOfMaterials
rm: cannot remove ‛ListOfMaterials': Is a directory

This message means that you need to use rm with some of the available options to remove directories. You can use three options with the rm command:

· -r, which recursively (hence the name) removes the contents of directories from bottom to top to ensure that no files are left without a directory to reside in

· -i, which prompts the user to confirm each deletion

· -f, which forces its way down, overriding any confirmation prompts that may occur for some specific files

Use the following command remove the stuff folder created in earlier examples in this chapter:

debian@beaglebone:~/project$ rm -rfi stuff
rm: descend into directory ‛stuff'? y
rm: descend into directory ‛stuff/schematics'? y
rm: remove directory ‛stuff/schematics/datasheets'? y
rm: remove directory ‛stuff/schematics'? y
rm: remove directory ‛stuff'? y

The -i and -f options are sort of contradictory, and the one that comes last is the one that’s dominant. In other words, rm -rif would be quite different from rm -rfi, which is used in the preceding example. The-i option would be ignored, and everything would be removed right away. Most of the time, using either rm -rf or -ri makes more sense.

Naturally, you can use each of these options independently. If you don’t use -i, everything is deleted without asking for permission; if you don’t use -f, you’d probably see no difference, because there are no files in that folder you need to force to be deleted; and if you don’t use -r, you get the error message shown earlier in this section.

Another way to remove a directory safely is to use the rmdir command, which forbids you from deleting folders that still have content. The following example illustrate how this command works in a new folder:

debian@beaglebone:~/project$ mkdir toBeDeleted
debian@beaglebone:~/project$ cd toBeDeleted
debian@beaglebone:~/project/toBeDeleted$ touch file1 file2 file3
debian@beaglebone:~/project/toBeDeleted$ ls
file1 file2 file3
debian@beaglebone:~/project/toBeDeleted$ cd ..
debian@beaglebone:~/project$ rmdir toBeDeleted
rmdir: failed to remove ‛toBeDeleted': Directory not empty
debian@beaglebone:~/project$ cd toBeDeleted
debian@beaglebone:~/project/toBeDeleted$ rm file1 file2 file3
debian@beaglebone:~/project/toBeDeleted$ ls
debian@beaglebone:~/project/toBeDeleted$ cd ..
debian@beaglebone:~/project$ rmdir toBeDeleted
debian@beaglebone:~/project$ ls
bye2.txt hello3.txt teste2.txt
bye.txt helloAndBye.txt teste.txt
circuitSchematic hello.txt theoretical_stuff
Code_for_the_project ListOfMaterials
hello2.txt list.txt

Note that we didn’t specify a file type, such as .txt, with touch. The truth is, for simple text files, specifying a file type doesn’t matter. By definition, an empty file or blank file is a file with the size of 0 bytes, which is what an empty .txt file is.

You can delete a directory while you’re still inside it. When you do that, the prompt still tells you that you’re inside that directory, but when you leave it, you realize that the directory has ceased to exist:

debian@beaglebone:~/project$ mkdir toBeDeleted
debian@beaglebone:~/project$ cd toBeDeleted
debian@beaglebone:~/project/toBeDeleted$ rmdir ../toBeDeleted
debian@beaglebone:~/project/toBeDeleted$ cd ..
debian@beaglebone:~/project$ ls
bye2.txt hello3.txt teste2.txt
bye.txt helloAndBye.txt teste.txt
circuitSchematic hello.txt theoretical_stuff
Code_for_the_project ListOfMaterials
hello2.txt list.txt

When you type a command without specifying a path, the shell assumes that you’re talking about a file inside your current directory. In the preceding example, there’s no toBeDeleted directory inside toBeDeleted, which is why you need to use rmdir with the relative path ../toBeDeleted.

Copying and renaming files

Copying and renaming files are common tasks, no matter what OS you use. In Linux, you can perform these tasks by using pretty straightforward commands. For copying, you use the following command:

cp [OPTIONS] <copy_from><copy_to>

copy_from is the name of the file from which you want to copy a file, and copy_to is where you want to save the copied file. The following code illustrates this command, starting by creating a <copy_to> folder:

debian@beaglebone:~/project$ mkdir importantFiles
debian@beaglebone:~/project$ cp hello2.txt importantFiles

The syntax for moving a file with the mv command is the same as the syntax for the cp command:

debian@beaglebone:~/project$ mv bye.txt importantFiles

If the importantFiles directory didn’t exist, the mv command would rename the file bye.txt, making it importantFiles. Although this renaming may lead to confusion and unexpected errors, use the following technique to rename files from the command prompt:

debian@beaglebone:~/project$ cd importantFiles
debian@beaglebone:~/project/importantFiles mv hello2.txt greetings.txt
debian@beaglebone:~/project/importantFiles cd ..

Now confirm the results of what you just typed :

debian@beaglebone:~/project$ ls importantFiles
bye.txt greetings.txt

The mv command means either “move a file” or “rename a file,” depending on whether its second input is a directory that already exists.

If the destination folder already has a file with the same name as the file you’re copying, that file will be overwritten!

You can use several options with these commands, two of which are quite useful and probably familiar to you. One option is-i, which protects you from overwriting files in the same way that you’re protected from deleting files with rm; you’re prompted to confirm before the file is overwritten. The other option is-v, which describes what’s going on, just like when it’s used with mkdir.

Selecting multiple files in Linux

Sometimes, you need to select multiple files, but without a GUI, dragging the mouse won’t do. When you use the command line, you can select multiple files by using wildcards.

The name does them justice, as wildcards are special characters that can be used to represent any character in different ways. Rather than providing a filename for your command to work with, you provide a pattern. The best way to explain this concept is to provide examples.

Start by creating a new folder (for matters of organization) and filling it with files of a similar name:

debian@beaglebone:~/project$ mkdir Hellos
debian@beaglebone:~/project$ cd Hellos
debian@beaglebone:~/project/Hellos$ touch hello1.txt hello2.txt hello3.txt hello4.txt
debian@beaglebone:~/project/Hellos$ ls
hello1.txt hello2.txt hello3.txt hello4.txt

Naturally, the patterns here are hello and .txt. You can use three wildcards.

The question-mark (?) wildcard replaces one single character, so hello?.txt refers to all those hellos. Try it with the file command:

debian@beaglebone:~/project/Hellos file hello?.txt
hello1.txt: empty
hello2.txt: empty
hello3.txt: empty
hello4.txt: empty

Note, however, that the ? wildcard replaces just one character. If you had more than ten hellos, this code would select only the first nine:

debian@beaglebone:~/project/Hellos touch hello12.txt
debian@beaglebone:~/project/Hellos file hello?.txt
hello1.txt: empty
hello2.txt: empty
hello3.txt: empty
hello4.txt: empty

This example is where the asterisk (*) wildcard comes in. Rather than replacing just one single character, it replaces any number of characters. hello* selects everything that starts with hello; *.txt selects everything that ends in .txt; and *hello* selects everything that has the word hello in it:

debian@beaglebone:~/project/Hellos touch oh_hello.txt helloBuddy.txt
debian@beaglebone:~/project/Hellos file hello*
hello12.txt: empty
hello1.txt: empty
hello2.txt: empty
hello3.txt: empty
hello4.txt: empty
helloBuddy.txt: empty

debian@beaglebone:~/project/Hellos file *.txt
hello12.txt: empty
hello1.txt: empty
hello2.txt: empty
hello3.txt: empty
hello4.txt: empty
helloBuddy.txt: empty
oh_hello.txt: empty

debian@beaglebone:~/project/Hellos file *hello*
hello12.txt: empty
hello1.txt: empty
hello2.txt: empty
hello3.txt: empty
hello4.txt: empty
helloBuddy.txt: empty
oh_hello.txt: empty

As expected, file hello* won’t return information about the oh_hello.txt file, and file *hello* and file *.txt won’t miss any file. Note, however, that if you had files with the names bye.txt and hello.jpg in there, these two commands would yield different results: file *hello* would ignore bye.txt, and file *.txt would ignore hello.jpg.

The last wildcard is square brackets ([ ]), which replaces a character with a set of specific letters. To select all files whose names start with a, b, or c, for example, you can use [abc]*:

debian@beaglebone:~/project/Hellos touch awesome beaglebone can do extremely fantastic gigs
debian@beaglebone:~/project/Hellos file [abc]*
awesome: empty
beaglebone: empty
can: empty

You can also use [ ] the other way around, to select files that don’t not start with the specified characters, by using the ^ character:

debian@beaglebone:~/project/Hellos file [^abc]*
do: empty
extremely: empty
fantastic: empty
gigs: empty
hello12.txt: empty
hello1.txt: empty
hello2.txt: empty
hello3.txt: empty
hello4.txt: empty
helloBuddy.txt: empty
oh_hello.txt: empty

Finally, you can specify a range by using [ ] and a hyphen, like so:

debian@beaglebone:~/project/Hellos file [b-e]*
beaglebone: empty
can: empty
do: empty
extremely: empty

Wildcards may be a bit confusing, but all you need to remember is the rule of thumb that a wildcard replaces one or more characters, allowing you to narrow or broaden the number of files you want to select. Playing around with wildcards is the best way to get a feel for how to use them. Following are some more examples, the first two of which deserve special attention and explanation:

debian@beaglebone:~/project/Hellos touch hello23.txt
debian@beaglebone:~/project/Hellos file hello[23].txt
hello2.txt: empty
hello3.txt: empty

hello23.txt doesn’t appear when you use file hello[23].txt. Weird, huh? This result is due to the fact that what’s inside the brackets is a list of one possible character to substitute for one character immediately next to hello and before .txt. file hello[23][23].txt or hello[32][32].txt or any other combination, would display hello23.txt but not hello2.txt or hello3.txt because it’s looking for twocharacters before .txt:

debian@beaglebone:~/project/Hellos touch A
debian@beaglebone:~/project/Hellos file *a*
awesome: empty
beaglebone: empty
can: empty
fantastic: empty

In this example, you’re selecting all files whose names contain a. The file A doesn’t appear due to the fact that, as stated earlier in this chapter, Linux is case-sensitive. The letter a and the letter A are completely different characters.

Here are some more examples to help you get familiar with how wildcards work:

debian@beaglebone:~/project/Hellos file hello[2-4].txt
hello2.txt: empty
hello3.txt: empty
hello4.txt: empty
debian@beaglebone:~/project/Hellos file hello??.txt
hello12.txt: empty
debian@beaglebone:~/project/Hellos file hello[^23]*
hello12.txt: empty
hello1.txt: empty
hello4.txt: empty
helloBuddy.txt: empty
debian@beaglebone:~/project/Hellos file ?
A: empty
debian@beaglebone:~/project/Hellos file *
A: empty
awesome: empty
beaglebone: empty
can: empty
do: empty
extremely: empty
fantastic: empty
gigs: empty
hello12.txt: empty
hello1.txt: empty
hello23.txt: empty
hello2.txt: empty
hello3.txt: empty
hello4.txt: empty
helloBuddy.txt: empty
oh_hello.txt: empty

You can use wildcards with any other commands, just as you’d use the name of a file. As proof, bid adieu to the Hellos directory as follows:

debian@beaglebone:~/project/Hellos rm -v *hello*
removed ‛hello12.txt'
removed ‛hello1.txt'
removed ‛hello2.txt'
removed ‛hello3.txt'
removed ‛hello4.txt'
removed ‛helloBuddy.txt'
removed ‛oh_hello.txt'
debian@beaglebone:~/project/Hellos ls
A awesome beaglebone can do extremely fantastic gigs

You could simply have deleted everything right away by using the * wildcard because it replaces any number of characters. The following code does that:

debian@beaglebone:~/project/Hellos$ rm -v *
removed ‛A'
removed ‛awesome'
removed ‛beaglebone'
removed ‛can'
removed ‛do'
removed ‛extremely'
removed ‛fantastic'
removed ‛gigs'
debian@beaglebone:~/project/Hellos$ ls
debian@beaglebone:~/project/Hellos$ cd ..
debian@beaglebone:~/project$ rmdir Hellos

The computer uses spaces to separate the inputs of commands! That said, when you’re using asterisks, an accidental space could lead to catastrophic results. Suppose that you want to delete all the .jpg files you have inside a folder. To do that, you could use

rm *.jpg

Had you unwittingly placed an extra space in the command, as in

rm * .jpg

the shell would interpret this command as a direction to remove * — which means remove everything — and then attempt to remove a file named .jpg. You may unwittingly delete a very important file in that directory, and there’s no “undelete” option. One solution would be to use the -i option whenever you decide to delete many files, but having to confirm each deletion would be tedious and would defeat the purpose of using wildcards. The best solution is simply to be very cautious whenever you’re deleting files — and to double that caution if you’re doing so by resorting to wildcards.

You should always be cautious when deleting files. If, for any specific project you may encounter in the future, you find yourself working as a superuser, that caution should be much greater. Nothing can hold you back from deleting everything on the computer, leading to a system crash due to the deletion of a crucial file, directory, or whatever. The command that causes this problem is rm -rf /* issued by someone who’s logged in as superuser. We’re exposing you to this command because someone, somewhere may claim that it’s the solution to the problem you’re having. Now you’re prepared for this claim, and you can ignore it. You can also laugh and say, “Ah, almost got me there!”

Using long-listing format and permissions

Earlier in this chapter, in the section “Listing files and directories,” we introduce the long-listing format of the ls command, which is displayed when you use it with the -l option. This format offers quite a bit of information about the files in the directory you list:

debian@beaglebone:~/project$ ls -l
total 32
-rw-r--r-- 1 debian debian 32 Jul 1 00:11 bye2.txt
drwxr-xr-x 2 debian debian 4096 Jun 30 23:58 CircuitSchematic
drwxr-xr-x 2 debian debian 4096 Jun 30 23:58 Code_for_the_project
-rw-r--r-- 1 debian debian 0 Jul 1 00:11 hello2.txt

-rw-r--r-- 1 debian debian 0 Jul 1 00:11 hello3.txt
-rw-r--r-- 1 debian debian 43 Jul 1 00:09 helloAndBye.txt
drwxr-xr-x 2 debian debian 4096 Jul 1 00:43 importantFiles
drwxr-xr-x 2 debian debian 4096 Jun 30 23:58 ListOfMaterials
-rw-r--r-- 1 debian debian 18 Jul 1 00:15 list.txt
drwxr-xr-x 2 debian debian 4096 Jun 30 23:58 TheoreticalStuff

You should dissect these lines for the information they contain. Starting from the right (because those commands are the most intuitive and the easiest to explain), you have the following:

· The filename (bye2.txt): Remember that the names of directories are also filenames.

· The time and date of the last modification (Jul 1 00:11): If you had an older file, this format would change slightly. Rather than showing the time when you last altered the file, the year in which you modified it would be displayed.

· The size of the file, measured in bytes (32 or 4096): One important thing to note is that for the directories, this number is the size of the file that represents the directory, not the sum of the contents of the directory. Thus, all directories display a size of 4096 bytes, regardless of whether they are empty. To have the sizes of the contents printed in units that make more sense (such as 1K, 120M, and 2G), use the -h (human-readable) option.

· The owner of the file and the group that owns it (debian debian): The two columns that feature debian represent, from left to right, the owner of the file and the group that owns it. Thus, the second column could be something like LabPCs.

· The number of hard links to the file (1 or 2): The number before debian represents the number of hard links to the file. In the case of a directory, that number is the number of immediate subdirectories it contains, as well as the directory itself and the parent directory. Therefore, a directory with no subdirectories has at least two hard links to it.

The remaining columns consist of information regarding permissions; they provide insight into who is able to use the files and in which ways. You can do three things with a file: read it (r), write in it (w), and execute it (x). Also, there are three groups of people, from the point of view of your computer: the owner, the group, and the world.

That information is provided in the leftmost part of each line and is always a combination of ten characters. To understand this code, you can break it into four parts: one for the size of one character and three for the sizes of three characters:

· The first character tells you what kind of file you’re dealing with: a regular file (-) or a directory (d).

· Next are the permissions for the owner (the first three characters), the group (the next three characters), and everyone else (the last three characters). For a specific file, someone who has read permission can open the contents of the file or list a directory; someone who has write permission can change the contents of the file and of a directory (such as by creating or deleting files); and someone who has execute permission for a regular file can use it as a program and run it, or enter a directory.

Typically, you see -rw-r--r-- in a file. This code means that the owner can read it and write in it, and everyone else (the group and the world) can read it but not make any changes. The code is always rwx, so if you see a hyphen in the place of one of these letters, the file doesn’t give that specific permission to that group of people.

For a directory, you often see drwxr-xr-x. Here’s the letter-by-letter translation:

· It’s a directory (d).

· The owner can read, write, and execute it (rwx). Because this item is a directory, the owner can see its contents with the ls command or when using a GUI (read permission), add files to or delete files from it (write permission), and get inside the directory (execute permission).

· The group can read and execute it (r-x) but not write in it, meaning that they can list its contents (read permission) and also get inside it (execute permission), but they can’t create or delete any files (no write permission).

· Everyone else has the same permissions as the group — that is, they can check the contents without messing around with them.

Two small details are worth noting:

· Even if a file gives you write permission, it still answers to the directory that contains it. Even though you can alter the file itself, you can rename or delete it only if you have write permission for its directory.

· In reality, you can’t do anything in a directory if you don’t have the execute permission, so a three-character indicator such as rw- doesn’t allow you to do anything. Most things that you try to do result in a Permission denied error.

The chmod command enables you to change a file’s permissions. You have many ways to use this command, but perhaps the most intuitive way is to use the sum (+) and subtraction (-) operators, along with the people or groups to which you want to give or remove permissions: the user (the owner of the file), the group, the world (other users), or all of these by using u, g, o, or a, respectively.

In the following example a command removes write permission from hello2.txt for the user, gives write permission to the group for bye2.txt, and grants execute permission to the world for hello3.txt:

debian@beaglebone:~/project$ ls -l
total 32
-rw-r--r-- 1 debian debian 32 Jul 1 00:11 bye2.txt
drwxr-xr-x 2 debian debian 4096 Jun 30 23:58 CircuitSchematic
drwxr-xr-x 2 debian debian 4096 Jun 30 23:58 Code_for_the_project
-rw-r--r-- 1 debian debian 0 Jul 1 00:11 hello2.txt
-rw-r--r-- 1 debian debian 0 Jul 1 00:11 hello3.txt
-rw-r--r-- 1 debian debian 43 Jul 1 00:09 helloAndBye.txt
drwxr-xr-x 2 debian debian 4096 Jul 1 00:43 importantFiles
drwxr-xr-x 2 debian debian 4096 Jun 30 23:58 ListOfMaterials
-rw-r--r-- 1 debian debian 18 Jul 1 00:15 list.txt
drwxr-xr-x 2 debian root 4096 Jun 30 23:58 TheoreticalStuff
debian@beaglebone:~/project$ chmod u-w hello2.txt
debian@beaglebone:~/project$ chmod g+w bye2.txt
debian@beaglebone:~/project$ chmod o+x hello3.txt
debian@beaglebone:~/project$ ls -l
total 44
drwxr-xr-x 2 debian debian 4096 Apr 23 2014 Code_for_the_project
drwxr-xr-x 2 debian debian 4096 Apr 23 2014 ListOfMaterials
-rw-r--r-- 1 debian debian 10 Apr 23 2014 bye.txt
-r--rw-r-- 1 debian debian 35 Apr 23 2014 bye2.txt
drwxr-xr-x 2 debian debian 4096 Apr 23 2014 circuitSchematic
-rw-r--r-- 1 debian debian 33 Apr 23 2014 hello.txt
-r--r--r-- 1 debian debian 0 Apr 23 2014 hello2.txt
-r--r--r-x 1 debian debian 0 Apr 23 2014 hello3.txt
-rw-r--r-- 1 debian debian 43 Apr 23 2014 helloAndBye.txt
-rw-r--r-- 1 debian debian 173 Apr 23 2014 list.txt
-rw-r--r-- 1 debian debian 9 Apr 23 2014 teste.txt
-rw-r--r-- 1 debian debian 9 Apr 23 2014 teste2.txt
drwxr-xr-x 2 debian debian 4096 Apr 23 2014 theoretical_stuff

As you can verify, the user lost his or her permission to write in the file hello2.txt. To confirm, use the echo command:

debian@beaglebone:~/project$ echo 'This will not work' > hell
o2.txt
-bash: hello2.txt: Permission denied

For bye2.txt, even though you can’t readily test it, you can check the list to see that the group was given write permission for it. The same applies to hello3.txt, which now shows an x that wasn’t there earlier.

Managing Software on Your BeagleBone

When you know your way around the command line, downloading and installing new software on a computer or device running the Linux OS is quite easy and straightforward. The software comes in what are called packages— software programs that can be downloaded from the Internet and installed simply by typing a command in the prompt.

To download and install these packages, you normally use a package manager, which downloads and installs not only the software you requested, but also all other required software, known as dependencies. The Debian distribution uses a package manager called apt.

If you read other literature about the BeagleBone, you may find that you should use the opkg utility as the package manager. As mentioned in Chapter 2, up until recently, the standard distribution used by the BeagleBone was Ångström. The examples in this book use the Debian distribution, so apt is the way to go.

To manage your software, you need the authorization of the administrator, whom you already know as the superuser. Being logged in as root is often considered to be unsafe, as the computer becomes vulnerable not only to its user (who may unwittingly make undesired changes in the file system), but also to malicious software that may have gotten inside. With that in mind, you can carry out a command with the authorization of the root user without being logged in as such. To do so, type sudo (superuser do) before a command.

In any other situation, if you get an error message telling you that the command you typed can be executed only with the authorization of root, try using sudo before it. Be cautious, though. If the command is telling you that it needs the authorization of root, it’s probably because something serious is involved!

First and foremost, you have to update the list of available package versions that your package manager is aware of. (The package manager keeps such a list in the BeagleBone’s file system.) Type the following command:

sudo apt-get update

You need to be connected to the Internet for this command to work.

Text scrolls by after you type the command, giving information about the newest listings.

Next, you should update the software, which you can achieve by commanding apt to upgrade. This command upgrades all the packages you’ve installed to their most recent versions:

sudo apt-get upgrade

In terms of wording, the difference between updating and upgrading is subtle, but what they do is quite different (even though they’re usually done together). sudo apt-get update updates the list of available package versions but doesn’t install or upgrade any of them, whereas sudo apt-get upgrade updates the packages themselves, checking the list to do so. For that reason, you should always run update before upgrade.

Installing software

To install a package for which you already know the name, you have to type the following command:

sudo apt-get install <desired application>

To see how this process works, use the following command to install the Midnight Commander application, which is a visual file manager:

sudo apt-get install mc

This command downloads the package from the Internet and installs it, as well as any dependencies it requires to work properly.

Always run sudo apt-get update before installing software.

Running software

To run programs directly from the prompt, simply type their names, as shown in the following command and in Figure 4-4:

debian@beaglebone:~$ mc

Figure 4-4: Running Midnight Commander through PuTTY.

Updating software

You can update the latest versions of your software by typing the upgrade command:

sudo apt-get upgrade

Generally, though, you want to update the list of available package versions before you upgrade to ensure that apt gets you the most recent updates for your installed software.

In the “Installing software” section, we show you how to get updates and upgrades by writing the commands separately, but you can write them both in a single line as follows:

sudo apt-get update && sudo apt-get upgrade

The && is a binary operator that means AND. The AND operator is commonly used in programming to test for multiple conditions. For now, keep in mind that its use ensures that the second command executes only if the first succeeds. If your update fails for some reason (maybe because you lack an Internet connection), the system won’t even attempt to upgrade.

This process (specifically, the upgrading part) can take a very long time, which can be troublesome if you want to update a single application. Fortunately, you can do so by typing the install command again, remembering to update the list of available package versions first:

sudo apt-get update && sudo apt-get install mc

This command doesn’t install the software all over again. Instead, the package manager first checks for updates and installs them. If updates aren’t available, the package manager displays a message that the software is already up to date.

Removing software

To remove software from your BeagleBone, you resort once more to the apt package manager. Here’s an example:

sudo apt-get remove mc

This command, however, leaves behind files that are somehow related to the software, such as configuration files and logs. If you don’t intend to use those files in any way, you can remove everything by using purge:

sudo apt-get purge mc

You can also direct the system to check for unnecessary packages and/or files and remove them automatically.

The package manager downloads and installs not only the requested package, but also any other packages that it may depend on. Thus, if you delete some software, its dependencies may stay behind. The apt package manager deems the dependencies unnecessary and deletes them automatically when you issue the following command:

sudo apt-get autoremove

Don’t fret about giving so much power to the package manager. Before deletion, you see a list of the packages that will be removed, and you’re prompted to press Y to confirm that you do want those packages removed. You also see how much space will be freed.

To remove all files that are undoubtedly unnecessary, you can type the following command:

sudo apt-get clean

These files are usually installation files. They remain on your computer after the installation of a program, even though they’re no longer needed. Removing them isn’t an issue. If you decide to reinstall a package, you can simply do so as previously described in the “Installing software” section. The installation file downloads again.

We highly recommend not removing any package that you didn’t install yourself unless you’re absolutely certain that you know what it’s for. It may be a necessary package that comes with the Linux OS, and removing it may lead to a system crash.

Seeing what’s installed on your BeagleBone

To see a list of installed packages on your BeagleBone, type the following command:

dpkg --list

Note that this command doesn’t require root authorization. After all, you aren’t messing around with the software; you’re just listing it. Consequently, it’s not necessary to use sudo.

To see whether a specific package is installed, as well as a more detailed description about it, you can use the following command:

dpkg --status <nameOfThePackage>

Changing the Hostname and Password

Currently, the hostname of your BeagleBone is, rather boringly, beaglebone. You may want to change it to something more personal. Doing so is also useful when you start to use more than one BeagleBone on the same network, because the hostname is the name that’s displayed to all the other users on that network.

If you want to change the hostname, start by typing the following command:

debian@beaglebone:~$ sudo echo 'newHostName' > /etc/hostname

Your hostname is changed to the one that you prefer. The BeagleBone won’t recognize this “host,” however, so you need to edit the hosts file. To open and edit it, follow these steps:

1. Type the following in a text editor such as nano:

debian@beaglebone:~$ sudo nano /etc/hosts

2. On the line that reads 127.0.0.1 beaglebone, change beaglebone to your new hostname

3. Save the file, and quit nano.

4. Reboot your BeagleBone.

Your new hostname should appear during your next login.

Use the arrow keys to navigate nano, and press Ctrl+O to save and Ctrl+X to exit. If you attempt to exit without saving, you’re prompted to press Y if you want to save or N if you don’t.

The default debian user password is temppwd. If you want to change it, simply type the command passwd and follow the steps printed in the terminal:

debian@beaglebone:~$ passwd
Changing password for debian.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
debian@beaglebone:~$

The text is hidden while you write the passwords.

Shutting Down and Rebooting

There are better ways to shut down and reboot your BeagleBone than simply unplugging it. Unceremoniously unplugging your BeagleBone is the equivalent of shutting down your computer by pressing the power button or even removing its power source, which sometimes leads to complications such as file corruption.

To shut down your BeagleBone, simply type this command on the command line:

debian@beaglebone:~$ sudo shutdown -h now

You see the following information after you use the shutdown command:

Broadcast message from root@beaglebone (pts/0) (Thu Jul 3 18:50:09 2014):
The system is going down for system halt NOW!
debian@beaglebone:~$

To reboot, type this:

debian@beaglebone:~$ sudo reboot

This is the result:

Broadcast message from root@beaglebone (pts/0) (Thu Jul 3 18:44:29 2014):
The system is going down for reboot NOW!

You need to log in again through SSH after rebooting.

Commanding the Prompt Like a Jedi Master

This section provides a few tips and tricks that maximize the efficiency with which you use the Linux OS. This material won’t help you save the galaxy or show you how to fetch the TV remote with telekinesis, but it should help you navigate the prompt much faster than before — and with a lot more style.

Recalling previous commands

You can easily recall the commands you’ve used previously by pressing the up- and down-arrow keys. Imagine that you listed the contents of a file by using ls -l; then you created a directory and a file inside this new directory. You want to see the list again to see the default permissions of this new directory. Rather than keying in ls -l again, you can simply press the up-arrow key twice.

Next, imagine that you want to list the contents of this new folder to see the file inside it, but you want to add the -h option. Simply press the up-arrow key to bring ls -l onscreen once more; you can use the left and right arrow keys to navigate through that line and then add the -h option and the name of the new directory. You can tell that this method saves some work.

Now imagine repeating the entire process for someplace other than your working directory, a place with a very long absolute path, such as this:

ls -l /home/debian/Desktop/project/aFolder/aDeeperFolder/anEvenDeeperFolder/theDeepestFolder

In fact, create this folder now for the sake of further exemplification:

debian@beaglebone:~/Desktop$ mkdir -p aFolder/aDeeperFolder/anEvenDeeperFolder/theDeepestFolder

Autocompleting commands

After writing the first letters of a line, press Tab to command the shell to try to guess what you meant to write and to autocomplete it. Naturally, this feature is most useful when you want to access long filenames. If it fails, write a little more to give the shell a better hint. You can try it by typing the following at the Desktop and then pressing the Tab key:

debian@beaglebone:~/Desktop$ cd aF

The shell completes the line with cd aFolder/.

Note the slash at the end. When you used cd to access a directory in the previous sections, you never placed a slash at the end of its name. Placing or not placing a slash is exactly the same. Why does the autocompletion add a slash, then? That’s because it’s ready to continue guessing the names of files within that directory. If you keep pressing the Tab key, you eventually end up with the following command:

debian@beaglebone:~/Desktop$ cd aFolder/aDeeperFolder/anEvenDeeperFolder/theDeepestFolder

Because nothing else was inside any of those folders, it was quite easy for the shell to autocomplete your command even without having hints; there was only one option, after all. Otherwise, the prompt prints a list of possible files, asking you to write a few more letters. The following example illustrates that:

debian@beaglebone:~/Desktop/aFolder/aDeeperFolder/anEvenDeeperFolder/theDeepestFolder$ touch heyJohn heyMarie heyCarl

If you type file and then press Tab repeatedly, you see the following:

debian@beaglebone:~/Desktop/aFolder/aDeeperFolder/anEvenDeeperFolder/theDeepestFolder$ file hey

Continue pressing Tab. The prompt suggests all the possibilities:

debian@beaglebone:~/Desktop/aFolder/aDeeperFolder/anEvenDeeperFolder/theDeepestFolder$ file hey
heyCarl heyJohn heyMarie
debian@beaglebone:~/Desktop/aFolder/aDeeperFolder/anEvenDeeperFolder/theDeepestFolder$ file hey

If you give the prompt a better hint, such as this,

debian@beaglebone:~/Desktop/aFolder/aDeeperFolder/anEvenDeeperFolder/theDeepestFolder$ file heyJ

when you press Tab again, you see the result:

debian@beaglebone:~/Desktop/aFolder/aDeeperFolder/anEvenDeeperFolder/theDeepestFolder$ file heyJohn

Using keyboard shortcuts

You have ways to navigate the command line other than pressing the left- and right-arrow keys, which sometimes seem to take forever to get you somewhere. Following are some useful keyboard shortcuts:

· Ctrl+A and Ctrl+E do the same thing as the Home and End keys — that is, they bring you to the beginning and the end of the current command, respectively.

· Ctrl+Left and Ctrl+Right jump between inputs (arguments) in a command; the cursor jumps from a space or a slash to the next.

· Ctrl+U clears the entire line.

· Ctrl+K deletes everything from the position of the cursor to the end of the line.

· Ctrl+W deletes a single word before the cursor.

· Ctrl+R allows you to search your command history. If you press Ctrl+R and then type cd, for example, your most recent command involving cd appears on the command line. Pressing Ctrl+R again shows you the cdcommand before that, and so on. Press the left- or right-arrow key when you find the one you want.

This search finds characters, not commands. If you had a command such as mkdir photos_from_cd, you could use Ctrl+R with cd to find it.

Using these Ctrl shortcuts helps you jump around quite fast. Suppose that you want to copy a file from the Desktop to the anEvenDeeperFolder directory. First, create the file by using the touch command:

debian@beaglebone:~/Desktop$ touch hello.txt

To copy the file, you would have to type the following cumbersome command:

debian@beaglebone:~/Desktop$ cp hello.txt /home/debian/Desktop/aFolder/aDeeperFolder/anEvenDeeperFolder

What a chore! Instead, you can achieve the same result by following these steps:

1. Press Ctrl+R and then type mkdir:

(reverse-i-search)‛mkdir': [m]kdir -p aFolder/aDeeperFolder/anEvenDeeperFolder/theDeepestFolder

The preceding example and those that follow are all about showing you how to have your cursor jumping around in a line by using keyboard shortcuts. In the examples, the terminal cursor is represented with closed brackets ([]). If your cursor is somewhere else and you press the keyboard shortcuts you’ll get different results.

2. Press Ctrl+right arrow until your cursor is right before the directory you want to eliminate from the path:

debian@beaglebone:~/Desktop$ mkdir -p aFolder/aDeeperFolder/anEvenDeeperFolder[/]theDeepestFolder

3. Press Ctrl+K to get rid of theDeepestFolder/:

debian@beaglebone:~/Desktop$ mkdir -p aFolder/aDeeperFolder/anEvenDeeperFolder[]

4. Press Ctrl+A to go to the beginning of the line:

debian@beaglebone:~/Desktop$ [m]kdir -p aFolder/aDeeperFolder/anEvenDeeperFolder

5. Press Ctrl+right arrow twice.

Your cursor is now after-p:

debian@beaglebone:~/Desktop$ mkdir -p[]aFolder/aDeeperFolder/anEvenDeeperFolder

6. Press Ctrl+W twice to erase mkdir -p,and then type cp hello.txt:

debian@beaglebone:~/Desktop$ []aFolder/aDeeperFolder/anEvenDeeperFolder

Alternatively, you can type something like cp he and then press Tab:

debian@beaglebone:~/Desktop$ cp he[]aFolder/aDeeperFolder/anEvenDeeperFolder[

7. Press Tab:

debian@beaglebone:~/Desktop$ cp hello.txt[]aFolder/aDeeperFolder/anEvenDeeperFolder

Keeping everything clean

Typing the command clear or pressing Ctrl+L quickly clears the screen, but it doesn’t clear the terminal window. In fact, the result is like pressing Enter or Return many times: If you scroll up, you still see the commands you used previously. If you truly want to clear everything, you can use the reset command.

The reset command reinitializes the terminal window, so that it appears to have just started (and, thus, has nothing in it). The command doesn’t reinitiate the shell, however, so it doesn’t alter the shell’s state. In layman’s terms, the shell’s state is the same, so you can still access your command history.


Taking things further

The Linux OS offers a world of possibilities. It allows you to do plenty of things and gives you a degree of freedom, versatility, and customizability that is rare in any OS. This chapter should be enough to get you started using Linux on the BeagleBone, but if you’re interested in finding out more, you can grab a copy of Linux For Dummies, 9th Edition, by Richard Blum (John Wiley & Sons, Inc.).

Also, Linux is open-source software, so consulting the web is often a good way to find out more. You can find many friendly Linux users who are more than happy to share some of their knowledge and resources. If you search for the specific problem that you encounter, there is bound to be someone who has had the exact same problem — and someone who has explained how to solve it.

Additionally, www.linux.com/learn, http://linuxsurvival.com, and http://elinux.org are great resources to find out more about Linux. Elinux is a website destined specifically for embedded systems that run Linux, such as the BeagleBone.