Home IoT & Hardware BeagleBone: Creative Projects for Hobbyists

BeagleBone: Creative Projects for Hobbyists

By Rodolfo Giometti , Charles A. Hamilton , Richard Grimmett
books-svg-icon Book
Subscription $15.99 $10 p/m for three months
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
Subscription $15.99 $10 p/m for three months
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
About this book
BeagleBone is a microboard PC that runs Linux. It can connect to the Internet and run OSes such as Android and Ubuntu. You can transform this tiny device into a brain for an embedded application or an endless variety of electronic inventions and prototypes. This Learning Path starts off by teaching you how to program the BeagleBone. You will create introductory projects to get yourselves acquainted with all the nitty gritty. Then we’ll focus on a series of projects that are aimed at hobbyists like you and encompass the areas of home automation and robotics. With each project, we’ll teach you how to connect several sensors and an actuator to the BeagleBone Black. We’ll also create robots for land, sea, and water. Yes, really! The books used in this Learning Path are: 1. BeagleBone Black Cookbook 2. BeagleBone Home Automation Blueprints 3. Mastering BeagleBone Robotics
Publication date:
July 2017
Publisher
Packt
ISBN
9781788395656

 

Part 1. Module 1

BeagleBone Black Cookbook

Over 60 recipes and solutions for inventors, makers, and budding engineers to create projects using the BeagleBone Black

 

Chapter 1. Setting Up for the First Time

In this chapter, we will focus on the following topics:

  • First boot up
  • Power requirements – basic guidelines
  • Display
  • Essential GNU/Linux commands – the command shell
  • Essential GNU/Linux commands – typical commands
  • Edit a text file from the command shell
  • Connectivity
  • Package management – Installing packages using apt-get
  • SSH / Remote access
  • System images

Introduction

Getting off to a running start with your BeagleBone Black requires diving into a number of fundamental recipes first. After all, you have to learn to chop and dice before making a proper soup.

As you probably already know, creating the right development environment saves time, reduces errors, and creates a good workflow. To this end, after we get our display hardware hooked up, it's critical to learn a bit about the Linux commands that power our projects. Then, we will move on to getting your board both connected online and controlled remotely over a network. Next, we will learn the basics of installing and managing applications (packages). Finally, we will explore how to get an OS onto your board, whether it's with the internal solid-state drive (eMMC) or via an SD card.

First boot up

BeagleBone Black (BBB) designers have worked hard to simplify the user's first out-of-the-box experience. Although things get more challenging later, which is natural, it is pretty close to Plug and Play at the outset.

You do not even need to load any software as the platform arrives preloaded with a custom distribution of Linux Debian on the internal solid-state drive (eMMC).

Getting ready

Here's what you need:

  • Desktop client PC: As noted in the introduction, we will principally be using a Mac box as our client machine. However, the following steps apply equally to a Windows or Linux machine.
  • USB cable: A mini USB-B to USB cable is required, which is typically supplied with your board.
  • BeagleBone Black Rev. C: To reiterate a point from this book's introduction, our recipes use Rev. C, which is the currently shipping version of the board. If you have an earlier board, you may run into occasional conflicts with the recipes.

How to do it…

Perform the following steps:

  1. To power up, plug the USB cable into the BBB and your client box.
  2. Wait for the three blue LEDs to light up; each of them functions as a useful indicator of the board's health:
    • Power: Once power is supplied to the board, the LED next to the 5V barrel jack will remain on without flashing
    • USR0: Next to the tiny reset button (S1), this light maintains a continuous, solid "heartbeat" blink when powered
    • USR2: Two down from the Ethernet jack, this LED flutters subtly during CPU access
    How to do it…

    Note

    Unless otherwise noted, all images are copyright Charles A. Hamilton.

  3. Within a few moments on your client machine, the BEAGLE_BONE drive should appear as a visible volume on your desktop. Browse the folder and open up the START.htm file.
  4. Once opened, you will find a clear set of quick start steps, including how to install drivers (if needed).
  5. Browse your board—BeagleBone Black's boot system includes a handy web server that allows immediate connectivity. Clicking on the link will take you to the local IP address of the board at 192.168.7.2. Once there, you will find a very helpful presentation on the board.

    There is no reason to recapitulate all the material covered in the START.htm presentation. Instead, review it as a good jumping off point for the upcoming recipes in this chapter.

Getting ready

Power down: Before proceeding, remove all cables and power (USB, Ethernet, DC cord, HDMI, and so on).

How to do it...

Perform the following steps:

  1. Connect the USB cable: With a USB 2.0 A to Mini-B cable, connect the BBB to a USB port on your desktop PC.
  2. Look for status lights: After plugging in the USB cable, you will see the board's first LED light up, followed subsequently by the three other LEDs. If all goes well, the BEAGLE_BONE device will appear on your desktop.
  3. Install drivers: I won't belabor this step. It suffices to say that your desktop—whether it's a Windows, Mac, or Linux machine—requires certain drivers to recognize and run the BBB via USB properly. If your desktop OS doesn't come with the drivers already installed (as some do), install them via the link, http://beagleboard.org/Getting-Started.
  4. Once your drivers are installed, browse to the BASIC_START.htm file on the BEAGLE_BONE device and open it.

If the planets are aligned and the drivers are installed properly, you will see the following at the top of the browser window:

Your board is connected! BeagleBone Black rev 0A5C S/N xxxxxxxxxxxx running BoneScript 1.2.3 at 192.168.7.2.

At this point, you're ready to fly; at least at low altitude. With a co-pilot…on an emulator.

Getting ready

Here's what you need:

  • Desktop client PC: As noted in the introduction, we will principally be using a Mac box as our client machine. However, the following steps apply equally to a Windows or Linux machine.
  • USB cable: A mini USB-B to USB cable is required, which is typically supplied with your board.
  • BeagleBone Black Rev. C: To reiterate a point from this book's introduction, our recipes use Rev. C, which is the currently shipping version of the board. If you have an earlier board, you may run into occasional conflicts with the recipes.

How to do it…

Perform the following steps:

  1. To power up, plug the USB cable into the BBB and your client box.
  2. Wait for the three blue LEDs to light up; each of them functions as a useful indicator of the board's health:
    • Power: Once power is supplied to the board, the LED next to the 5V barrel jack will remain on without flashing
    • USR0: Next to the tiny reset button (S1), this light maintains a continuous, solid "heartbeat" blink when powered
    • USR2: Two down from the Ethernet jack, this LED flutters subtly during CPU access
    How to do it…

    Note

    Unless otherwise noted, all images are copyright Charles A. Hamilton.

  3. Within a few moments on your client machine, the BEAGLE_BONE drive should appear as a visible volume on your desktop. Browse the folder and open up the START.htm file.
  4. Once opened, you will find a clear set of quick start steps, including how to install drivers (if needed).
  5. Browse your board—BeagleBone Black's boot system includes a handy web server that allows immediate connectivity. Clicking on the link will take you to the local IP address of the board at 192.168.7.2. Once there, you will find a very helpful presentation on the board.

    There is no reason to recapitulate all the material covered in the START.htm presentation. Instead, review it as a good jumping off point for the upcoming recipes in this chapter.

Getting ready

Power down: Before proceeding, remove all cables and power (USB, Ethernet, DC cord, HDMI, and so on).

How to do it...

Perform the following steps:

  1. Connect the USB cable: With a USB 2.0 A to Mini-B cable, connect the BBB to a USB port on your desktop PC.
  2. Look for status lights: After plugging in the USB cable, you will see the board's first LED light up, followed subsequently by the three other LEDs. If all goes well, the BEAGLE_BONE device will appear on your desktop.
  3. Install drivers: I won't belabor this step. It suffices to say that your desktop—whether it's a Windows, Mac, or Linux machine—requires certain drivers to recognize and run the BBB via USB properly. If your desktop OS doesn't come with the drivers already installed (as some do), install them via the link, http://beagleboard.org/Getting-Started.
  4. Once your drivers are installed, browse to the BASIC_START.htm file on the BEAGLE_BONE device and open it.

If the planets are aligned and the drivers are installed properly, you will see the following at the top of the browser window:

Your board is connected! BeagleBone Black rev 0A5C S/N xxxxxxxxxxxx running BoneScript 1.2.3 at 192.168.7.2.

At this point, you're ready to fly; at least at low altitude. With a co-pilot…on an emulator.

How to do it…

Perform the following steps:

  1. To power up, plug the USB cable into the BBB and your client box.
  2. Wait for the three blue LEDs to light up; each of them functions as a useful indicator of the board's health:
    • Power: Once power is supplied to the board, the LED next to the 5V barrel jack will remain on without flashing
    • USR0: Next to the tiny reset button (S1), this light maintains a continuous, solid "heartbeat" blink when powered
    • USR2: Two down from the Ethernet jack, this LED flutters subtly during CPU access
    How to do it…

    Note

    Unless otherwise noted, all images are copyright Charles A. Hamilton.

  3. Within a few moments on your client machine, the BEAGLE_BONE drive should appear as a visible volume on your desktop. Browse the folder and open up the START.htm file.
  4. Once opened, you will find a clear set of quick start steps, including how to install drivers (if needed).
  5. Browse your board—BeagleBone Black's boot system includes a handy web server that allows immediate connectivity. Clicking on the link will take you to the local IP address of the board at 192.168.7.2. Once there, you will find a very helpful presentation on the board.

    There is no reason to recapitulate all the material covered in the START.htm presentation. Instead, review it as a good jumping off point for the upcoming recipes in this chapter.

Getting ready

Power down: Before proceeding, remove all cables and power (USB, Ethernet, DC cord, HDMI, and so on).

How to do it...

Perform the following steps:

  1. Connect the USB cable: With a USB 2.0 A to Mini-B cable, connect the BBB to a USB port on your desktop PC.
  2. Look for status lights: After plugging in the USB cable, you will see the board's first LED light up, followed subsequently by the three other LEDs. If all goes well, the BEAGLE_BONE device will appear on your desktop.
  3. Install drivers: I won't belabor this step. It suffices to say that your desktop—whether it's a Windows, Mac, or Linux machine—requires certain drivers to recognize and run the BBB via USB properly. If your desktop OS doesn't come with the drivers already installed (as some do), install them via the link, http://beagleboard.org/Getting-Started.
  4. Once your drivers are installed, browse to the BASIC_START.htm file on the BEAGLE_BONE device and open it.

If the planets are aligned and the drivers are installed properly, you will see the following at the top of the browser window:

Your board is connected! BeagleBone Black rev 0A5C S/N xxxxxxxxxxxx running BoneScript 1.2.3 at 192.168.7.2.

At this point, you're ready to fly; at least at low altitude. With a co-pilot…on an emulator.

Getting ready

Power down: Before proceeding, remove all cables and power (USB, Ethernet, DC cord, HDMI, and so on).

How to do it...

Perform the following steps:

  1. Connect the USB cable: With a USB 2.0 A to Mini-B cable, connect the BBB to a USB port on your desktop PC.
  2. Look for status lights: After plugging in the USB cable, you will see the board's first LED light up, followed subsequently by the three other LEDs. If all goes well, the BEAGLE_BONE device will appear on your desktop.
  3. Install drivers: I won't belabor this step. It suffices to say that your desktop—whether it's a Windows, Mac, or Linux machine—requires certain drivers to recognize and run the BBB via USB properly. If your desktop OS doesn't come with the drivers already installed (as some do), install them via the link, http://beagleboard.org/Getting-Started.
  4. Once your drivers are installed, browse to the BASIC_START.htm file on the BEAGLE_BONE device and open it.

If the planets are aligned and the drivers are installed properly, you will see the following at the top of the browser window:

Your board is connected! BeagleBone Black rev 0A5C S/N xxxxxxxxxxxx running BoneScript 1.2.3 at 192.168.7.2.

At this point, you're ready to fly; at least at low altitude. With a co-pilot…on an emulator.

How to do it...

Perform the following steps:

  1. Connect the USB cable: With a USB 2.0 A to Mini-B cable, connect the BBB to a USB port on your desktop PC.
  2. Look for status lights: After plugging in the USB cable, you will see the board's first LED light up, followed subsequently by the three other LEDs. If all goes well, the BEAGLE_BONE device will appear on your desktop.
  3. Install drivers: I won't belabor this step. It suffices to say that your desktop—whether it's a Windows, Mac, or Linux machine—requires certain drivers to recognize and run the BBB via USB properly. If your desktop OS doesn't come with the drivers already installed (as some do), install them via the link, http://beagleboard.org/Getting-Started.
  4. Once your drivers are installed, browse to the BASIC_START.htm file on the BEAGLE_BONE device and open it.

If the planets are aligned and the drivers are installed properly, you will see the following at the top of the browser window:

Your board is connected! BeagleBone Black rev 0A5C S/N xxxxxxxxxxxx running BoneScript 1.2.3 at 192.168.7.2.

At this point, you're ready to fly; at least at low altitude. With a co-pilot…on an emulator.

Power requirements – basic guidelines

Giving your BBB the proper amount of love and power is crucial to having a happy board. Following these basic guidelines will ensure that your BBB operates reliably and predictably.

The board's user manual recommends that you supply current to at least 1.2A (or 6W). However, if you connect up anything over the USB, at least 2A (or 10W) is recommended. Here are some additional rules of thumb:

  • You can power the BBB via the USB tether. However, if you attach additional USB devices, such as webcams, Wi-Fi or Bluetooth dongles, keyboards and mice, and then power them through the USB tether, it may lead to unreliable connections and power fluctuations. We were successful in powering some USB dongles directly via the BBB's USB port, whereas some devices, such as USB webcams, were not reliably powered.
  • If you connect USB devices, it is best to power them from a powered USB hub and then supply power to the BBB via a 5 VDC power supply of at least 1A.
  • 5V DC power—most users of the board will invariably recommend that you always power your board using a 5V DC supply.
  • You can also use a 5V wall charger power adapter with a USB port on it as long as it can deliver 1A.

Tip

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

In many cases, you will also be downloading code from the author's github site at https://github.com/HudsonWerks.

Display

The BBB's on board micro HDMI port provides a relatively easy way to attach a display device. With the new Debian distribution, compatibility, and ease of installation, the process is greatly simplified. We will take a look at two types of devices you may be using: an HDMI monitor and a small form-factor LCD (without HDMI).

Note

Using a display is not a requirement to operate the board and is merely optional. You will learn how to control the BBB without a display later in this chapter.

Connecting an HDMI LCD

Running your board with an HDMI monitor is the default method to use your BBB.

How to do it...

  1. Check the compatibility list for your HDMI LCD monitor at http://elinux.org/Beagleboard:BeagleBone_Black_Accessories#Monitors_and_Resolutions.

    Given the large universe of LCDs out there, this is not a definitive list. However, it does lend guidance on displays that will work with the BBB. You will also be pleasantly surprised that most consumer-grade HDTVs work pretty well out of the box with your board.

  2. Check your resolution at http://elinux.org/Beagleboard:BeagleBoneBlack_HDMI.
  3. Plug in one end of your HDMI cable to your monitor and the other end, which is the micro HDMI end, into your powered-down BBB.
  4. Fulfill the power requirements; power up your board with a 5V adapter.

    Note

    Since HDMI displays can draw a lot of power, don't try to run off your desktop's USB slot. Otherwise, you will get flaky results at best and, more likely, no picture at all.

  5. You should get a straightforward boot up into the default desktop "beagle" screen.

Connecting a non-HDMI LCD or cape

There's no obligation to use only an HDMI display on BeagleBone Black. For example, with the right HDMI to VGA converter cable, you can actually attach a standard VGA monitor to the BBB. Compatible converters can be found at http://elinux.org/Beagleboard:BeagleBone_Black_Accessories#HDMI_Adapters.

You may also develop an embedded prototype device that only needs a small LCD and no HDMI. Here's one recipe for a scenario using a 3.5-inch screen from Circuitco (http://elinux.org/CircuitCo:BeagleBone_LCD3) that I've used in my own projects. The display is in the category of add-ons to the BBB called capes, which are akin to the shields on an Arduino board. Capes are typically robust daughterboards and, unfortunately, tend to be much more expensive than Arduino add-ons.

Connecting a non-HDMI LCD or cape

CircuitCo LCD3 cape

How to do it...

Perform the following steps:

  1. Power down your board.
  2. Examine the header pins at the bottom of the LCD3 and note the pin layout: one short row and one longer row. Also, note the Power (PWR) button at one end.
    How to do it...

    The pin line up at the bottom of the PCB (printed circuit board)

    How to do it...
  3. Position the long row of pins on the LCD on top of the P9 pin slots on the BBB and the short pin row on the P8 row. Don't push the pins in yet. The PWR button should rest between the Ethernet port and the 5V barrel jack, as in the following image:
    How to do it...
  4. Next, slowly push the pins into the BBB headers. All pins should go in more or less simultaneously. It's a little tricky as you also have to squish the PWR button in place at the same time so that it lodges snugly between the 5V barrel and the Ethernet jack, as in the following image:
    How to do it...
  5. All pins on both sides of the LCD should be firmly pushed into place on the board, and the PWR button should be securely squeezed in place.
  6. Power up your board. Most distributions—including the Debian firmware, of course—have compatible drivers for the display, so you should boot right into your desktop within a few moments.

There's more…

For more information on the following topics, refer to the respective websites:

Connecting an HDMI LCD

Running your board with an HDMI monitor is the default method to use your BBB.

How to do it...

  1. Check the compatibility list for your HDMI LCD monitor at http://elinux.org/Beagleboard:BeagleBone_Black_Accessories#Monitors_and_Resolutions.

    Given the large universe of LCDs out there, this is not a definitive list. However, it does lend guidance on displays that will work with the BBB. You will also be pleasantly surprised that most consumer-grade HDTVs work pretty well out of the box with your board.

  2. Check your resolution at http://elinux.org/Beagleboard:BeagleBoneBlack_HDMI.
  3. Plug in one end of your HDMI cable to your monitor and the other end, which is the micro HDMI end, into your powered-down BBB.
  4. Fulfill the power requirements; power up your board with a 5V adapter.

    Note

    Since HDMI displays can draw a lot of power, don't try to run off your desktop's USB slot. Otherwise, you will get flaky results at best and, more likely, no picture at all.

  5. You should get a straightforward boot up into the default desktop "beagle" screen.

Connecting a non-HDMI LCD or cape

There's no obligation to use only an HDMI display on BeagleBone Black. For example, with the right HDMI to VGA converter cable, you can actually attach a standard VGA monitor to the BBB. Compatible converters can be found at http://elinux.org/Beagleboard:BeagleBone_Black_Accessories#HDMI_Adapters.

You may also develop an embedded prototype device that only needs a small LCD and no HDMI. Here's one recipe for a scenario using a 3.5-inch screen from Circuitco (http://elinux.org/CircuitCo:BeagleBone_LCD3) that I've used in my own projects. The display is in the category of add-ons to the BBB called capes, which are akin to the shields on an Arduino board. Capes are typically robust daughterboards and, unfortunately, tend to be much more expensive than Arduino add-ons.

Connecting a non-HDMI LCD or cape

CircuitCo LCD3 cape

How to do it...

Perform the following steps:

  1. Power down your board.
  2. Examine the header pins at the bottom of the LCD3 and note the pin layout: one short row and one longer row. Also, note the Power (PWR) button at one end.
    How to do it...

    The pin line up at the bottom of the PCB (printed circuit board)

    How to do it...
  3. Position the long row of pins on the LCD on top of the P9 pin slots on the BBB and the short pin row on the P8 row. Don't push the pins in yet. The PWR button should rest between the Ethernet port and the 5V barrel jack, as in the following image:
    How to do it...
  4. Next, slowly push the pins into the BBB headers. All pins should go in more or less simultaneously. It's a little tricky as you also have to squish the PWR button in place at the same time so that it lodges snugly between the 5V barrel and the Ethernet jack, as in the following image:
    How to do it...
  5. All pins on both sides of the LCD should be firmly pushed into place on the board, and the PWR button should be securely squeezed in place.
  6. Power up your board. Most distributions—including the Debian firmware, of course—have compatible drivers for the display, so you should boot right into your desktop within a few moments.

There's more…

For more information on the following topics, refer to the respective websites:

How to do it...

  1. Check the compatibility list for your HDMI LCD monitor at http://elinux.org/Beagleboard:BeagleBone_Black_Accessories#Monitors_and_Resolutions.

    Given the large universe of LCDs out there, this is not a definitive list. However, it does lend guidance on displays that will work with the BBB. You will also be pleasantly surprised that most consumer-grade HDTVs work pretty well out of the box with your board.

  2. Check your resolution at http://elinux.org/Beagleboard:BeagleBoneBlack_HDMI.
  3. Plug in one end of your HDMI cable to your monitor and the other end, which is the micro HDMI end, into your powered-down BBB.
  4. Fulfill the power requirements; power up your board with a 5V adapter.

    Note

    Since HDMI displays can draw a lot of power, don't try to run off your desktop's USB slot. Otherwise, you will get flaky results at best and, more likely, no picture at all.

  5. You should get a straightforward boot up into the default desktop "beagle" screen.

Connecting a non-HDMI LCD or cape

There's no obligation to use only an HDMI display on BeagleBone Black. For example, with the right HDMI to VGA converter cable, you can actually attach a standard VGA monitor to the BBB. Compatible converters can be found at http://elinux.org/Beagleboard:BeagleBone_Black_Accessories#HDMI_Adapters.

You may also develop an embedded prototype device that only needs a small LCD and no HDMI. Here's one recipe for a scenario using a 3.5-inch screen from Circuitco (http://elinux.org/CircuitCo:BeagleBone_LCD3) that I've used in my own projects. The display is in the category of add-ons to the BBB called capes, which are akin to the shields on an Arduino board. Capes are typically robust daughterboards and, unfortunately, tend to be much more expensive than Arduino add-ons.

Connecting a non-HDMI LCD or cape

CircuitCo LCD3 cape

How to do it...

Perform the following steps:

  1. Power down your board.
  2. Examine the header pins at the bottom of the LCD3 and note the pin layout: one short row and one longer row. Also, note the Power (PWR) button at one end.
    How to do it...

    The pin line up at the bottom of the PCB (printed circuit board)

    How to do it...
  3. Position the long row of pins on the LCD on top of the P9 pin slots on the BBB and the short pin row on the P8 row. Don't push the pins in yet. The PWR button should rest between the Ethernet port and the 5V barrel jack, as in the following image:
    How to do it...
  4. Next, slowly push the pins into the BBB headers. All pins should go in more or less simultaneously. It's a little tricky as you also have to squish the PWR button in place at the same time so that it lodges snugly between the 5V barrel and the Ethernet jack, as in the following image:
    How to do it...
  5. All pins on both sides of the LCD should be firmly pushed into place on the board, and the PWR button should be securely squeezed in place.
  6. Power up your board. Most distributions—including the Debian firmware, of course—have compatible drivers for the display, so you should boot right into your desktop within a few moments.

There's more…

For more information on the following topics, refer to the respective websites:

Connecting a non-HDMI LCD or cape

There's no obligation to use only an HDMI display on BeagleBone Black. For example, with the right HDMI to VGA converter cable, you can actually attach a standard VGA monitor to the BBB. Compatible converters can be found at http://elinux.org/Beagleboard:BeagleBone_Black_Accessories#HDMI_Adapters.

You may also develop an embedded prototype device that only needs a small LCD and no HDMI. Here's one recipe for a scenario using a 3.5-inch screen from Circuitco (http://elinux.org/CircuitCo:BeagleBone_LCD3) that I've used in my own projects. The display is in the category of add-ons to the BBB called capes, which are akin to the shields on an Arduino board. Capes are typically robust daughterboards and, unfortunately, tend to be much more expensive than Arduino add-ons.

Connecting a non-HDMI LCD or cape

CircuitCo LCD3 cape

How to do it...

Perform the following steps:

  1. Power down your board.
  2. Examine the header pins at the bottom of the LCD3 and note the pin layout: one short row and one longer row. Also, note the Power (PWR) button at one end.
    How to do it...

    The pin line up at the bottom of the PCB (printed circuit board)

    How to do it...
  3. Position the long row of pins on the LCD on top of the P9 pin slots on the BBB and the short pin row on the P8 row. Don't push the pins in yet. The PWR button should rest between the Ethernet port and the 5V barrel jack, as in the following image:
    How to do it...
  4. Next, slowly push the pins into the BBB headers. All pins should go in more or less simultaneously. It's a little tricky as you also have to squish the PWR button in place at the same time so that it lodges snugly between the 5V barrel and the Ethernet jack, as in the following image:
    How to do it...
  5. All pins on both sides of the LCD should be firmly pushed into place on the board, and the PWR button should be securely squeezed in place.
  6. Power up your board. Most distributions—including the Debian firmware, of course—have compatible drivers for the display, so you should boot right into your desktop within a few moments.

There's more…

For more information on the following topics, refer to the respective websites:

How to do it...

Perform the following steps:

  1. Power down your board.
  2. Examine the header pins at the bottom of the LCD3 and note the pin layout: one short row and one longer row. Also, note the Power (PWR) button at one end.
    How to do it...

    The pin line up at the bottom of the PCB (printed circuit board)

    How to do it...
  3. Position the long row of pins on the LCD on top of the P9 pin slots on the BBB and the short pin row on the P8 row. Don't push the pins in yet. The PWR button should rest between the Ethernet port and the 5V barrel jack, as in the following image:
    How to do it...
  4. Next, slowly push the pins into the BBB headers. All pins should go in more or less simultaneously. It's a little tricky as you also have to squish the PWR button in place at the same time so that it lodges snugly between the 5V barrel and the Ethernet jack, as in the following image:
    How to do it...
  5. All pins on both sides of the LCD should be firmly pushed into place on the board, and the PWR button should be securely squeezed in place.
  6. Power up your board. Most distributions—including the Debian firmware, of course—have compatible drivers for the display, so you should boot right into your desktop within a few moments.

There's more…

For more information on the following topics, refer to the respective websites:

There's more…

For more information on the following topics, refer to the respective websites:

Essential GNU/Linux commands – the command shell

Fancy GUIs are nice, but on BeagleBone Black, you will be mainly working with the command shell and its accompanying command-line tools to provide access to the operating system. First, let's take a look at some terminology: command shell, command-line shell, command line, or simply shell will all be used somewhat interchangeably as they all basically refer to the same thing—a means to provide access to the operating system with the command line and without a GUI. Bash is the default shell on Linux.

Terminal is yet another typical way to refer to the shell. The term is derived from its preceding technologies where any interaction with a computer required using a text-based terminal interface.

Often intimidating to new users, command shell is the starting point for any serious embedded system project. The Debian distribution for BeagleBone Black comes preloaded with two command-line tools to use a command shell: LXTerminal and Root Terminal. Although there are others with more robust and colorful interfaces that you can install, such as xterm and eterm, for simplicity's sake, we will use LXTerminal. In the next section, you will learn about the command syntax required within the tool. Finally, we will create, edit, and save a text file from the command line.

Getting ready

Ensure that your BeagleBone Black is set up and running in the manner described in the Introduction section. (Note that here, we will show the two different methods of running the board: powered via USB and powered via 5V).

How to do it...

  1. Begin by opening up LXTerminal, which you will find in the Accessories application on the lower left-hand side of the screen:
    How to do it...
  2. You will now see a command prompt that looks similar to this on your screen:
    How to do it...
  3. Just for fun, let's type in the following at the command prompt:
    debian@beaglebone:~$  echo Can you hear me Major Tom?
    

    This will show the following output:

    How to do it...

Congratulations! You've just sent your first command to BeagleBone Black (and it wasn't Hello World). But we're getting ahead of ourselves. Commands are for the next section.

How it works...

Let's take a quick look at the various parts of what LXTerminal shows us. Firstly, we know that Linux likes to keep the current user name at the front and center, in this case debian. This way, you always have a quick reference point to know whether you are a root user, which gives you total control over the system, or just another user with limited privileges.

Then comes the address of the device you're working on: beaglebone. You can actually customize the name, but we will save that for the appendix. Lastly, consider the $ sign, which denotes that you're logged in as a regular user. If you were a root user, the symbol would be the # sign. You will learn more about users and root users in a later section.

How it works...

Getting ready

Ensure that your BeagleBone Black is set up and running in the manner described in the Introduction section. (Note that here, we will show the two different methods of running the board: powered via USB and powered via 5V).

How to do it...

  1. Begin by opening up LXTerminal, which you will find in the Accessories application on the lower left-hand side of the screen:
    How to do it...
  2. You will now see a command prompt that looks similar to this on your screen:
    How to do it...
  3. Just for fun, let's type in the following at the command prompt:
    debian@beaglebone:~$  echo Can you hear me Major Tom?
    

    This will show the following output:

    How to do it...

Congratulations! You've just sent your first command to BeagleBone Black (and it wasn't Hello World). But we're getting ahead of ourselves. Commands are for the next section.

How it works...

Let's take a quick look at the various parts of what LXTerminal shows us. Firstly, we know that Linux likes to keep the current user name at the front and center, in this case debian. This way, you always have a quick reference point to know whether you are a root user, which gives you total control over the system, or just another user with limited privileges.

Then comes the address of the device you're working on: beaglebone. You can actually customize the name, but we will save that for the appendix. Lastly, consider the $ sign, which denotes that you're logged in as a regular user. If you were a root user, the symbol would be the # sign. You will learn more about users and root users in a later section.

How it works...

How to do it...

  1. Begin by opening up LXTerminal, which you will find in the Accessories application on the lower left-hand side of the screen:
    How to do it...
  2. You will now see a command prompt that looks similar to this on your screen:
    How to do it...
  3. Just for fun, let's type in the following at the command prompt:
    debian@beaglebone:~$  echo Can you hear me Major Tom?
    

    This will show the following output:

    How to do it...

Congratulations! You've just sent your first command to BeagleBone Black (and it wasn't Hello World). But we're getting ahead of ourselves. Commands are for the next section.

How it works...

Let's take a quick look at the various parts of what LXTerminal shows us. Firstly, we know that Linux likes to keep the current user name at the front and center, in this case debian. This way, you always have a quick reference point to know whether you are a root user, which gives you total control over the system, or just another user with limited privileges.

Then comes the address of the device you're working on: beaglebone. You can actually customize the name, but we will save that for the appendix. Lastly, consider the $ sign, which denotes that you're logged in as a regular user. If you were a root user, the symbol would be the # sign. You will learn more about users and root users in a later section.

How it works...

How it works...

Let's take a quick look at the various parts of what LXTerminal shows us. Firstly, we know that Linux likes to keep the current user name at the front and center, in this case debian. This way, you always have a quick reference point to know whether you are a root user, which gives you total control over the system, or just another user with limited privileges.

Then comes the address of the device you're working on: beaglebone. You can actually customize the name, but we will save that for the appendix. Lastly, consider the $ sign, which denotes that you're logged in as a regular user. If you were a root user, the symbol would be the # sign. You will learn more about users and root users in a later section.

How it works...

Essential GNU/Linux commands – typical commands

The command line is your friend.

For advanced users, this is an obvious statement; for the beginner, not so friendly. Where do you start with that darn blinking cursor?

How many Linux commands are there? A whole bunch. Thousands. Tens of thousands depending on dependencies and packages. But don't worry, you don't have to learn them all.

Saying or writing something original about Linux commands is a bit difficult: far smarter, folks than I have written about, compiled, and battle-tested the multiple combinations of commands available to the Linux user. And this book is not intended to be about Linux commands. So instead, I will give you a handful of basic commands that I find myself consistently using on the BBB and also provide you with some of my favorite reference materials and cheat sheets that will give you a more comprehensive list of important commands.

Getting ready

Of course, we assume that if you intend to use some command line interface (CLI) commands, you have to have your command shell window open and gain root access:

sudo

Using sudo as part of your command syntax is a requirement that often bedevils new Linux users. It is important to understand that many Linux commands demand what are known as "superuser" privileges, which grant you total control over the board and its software. Having full admin rights within a command prevents any casual user from coming along and destroying your hard work, by design or accident.

The "open sesame" of Debian Linux, the magic sudo command (pronounced "soo-doo"), means "Super User do x-command" when dissected. If you don't append your command at the beginning with sudo, your mkdir command, for example, will return a "bad command" error.

sudo -i and root

Adding the -i option (the sudo -i command) provides even more control over the environment: the root user access. When running commands as a root user, there are ample opportunities to completely mangle your system, so exercise extreme caution in its use.

However, not all commands require superuser privileges. For instance, when you're really just poking around the system and not modifying files, directories, or the system itself, sudo isn't necessary.

You should keep in mind several things when working with the command line:

  • Lowercase: Using lowercase syntax is the shell's required expectation, with the exception of some options and arguments that are uppercase.
  • Arguments and options: Most commands have multiple options or arguments that can be used to fine-tune your command. We will only be able to touch on a few instances of how and when these options are used.
  • Help:
    • Help (no hyphens): Typing help before a command outputs information about this command. An example of this is $ help cd.
    • Help (hyphens): Many executable commands have more help and information about their options or arguments. An example of this is $ mkdir --help.
    • Man pages: Short for "manual pages", adding man to the beginning of many Linux commands opens up a manual for these commands. It can often yield too much information, but it can be helpful if you're looking for a deeper understanding of a command. An example of this is $ man chmod.
  • Tab command: This lists recent commands. In order to use this, you need to do the following:
    • Type a letter (or a string of letters) and then press the Tab button. All commands that begin with this letter or string will be shown. This will prevent a vast majority of typing mistakes.
    • Press Tab twice quickly, and it will show all the commands that are currently in your path.

Conventions

Early in the book, we will frequently use the entire line from the command line, including the user name, address, symbols connoting user type, directory, and so on. So, for example, you will see a line that looks similar to the following:

debian@beagle_bone:~$ mkdir

However, as we become more familiar with the shell, we will mostly shorten the command instruction to only include the command itself:

~$ mkdir

Alternatively, you will see a slight variation on the preceding command when we refer to a root user. Note the # sign in place of the $ sign in the following command:

~# mkdir

Now, let's move on to the commands. A super-duper subset of commands, anyway. We've organized them into System navigational commands, Device and system insight, and Modify content/data commands.

System navigational command – change your directory – cd

To move from one directory to another, there are numerous variations on how to do it.

How to do it…

Perform the following steps:

  1. To change from your current default directory to the Desktop directory, type in these commands:
    debian@beaglebone:~$ cd Desktop
    debian@beaglebone:~/Desktop$
    
  2. To go back one directory level, use the following command lines:
    debian@beaglebone:~/Desktop$ cd ..
    debian@beaglebone:~$
    
  3. Go to the Home directory using the following command:
    debian@beaglebone:~/Desktop$ cd ~
    debian@beaglebone:~$
    
  4. Go back to the previous directory you were in by typing in the following commands:
    debian@beaglebone:~$ cd -
    /home/debian/Desktop
    debian@beaglebone:~/Desktop$
    

There's more…

In addition to the preceding commands, you should be familiar with the four special directory symbols:

  • The current directory (.)
  • The parent directory (..)
  • The root directory (/)
  • The home directory (~)

System navigational command – show your current directory – pwd

This stands for print working directory, a means to show the user where in the system they are currently working or the absolute path relative to the current directory.

How to do it…

Use the following command to show your current directory:

debian@beaglebone:~$ pwd
/home/Debian

System navigational command – find a file – find

Looking for a file? One way to search for it is with the find function. Just be sure to add the -name option so that the output shows the directory location. Add sudo as well so that you do not get directory access errors.

How to do it…

Use the following command to find a file:

$ sudo find / -name <filename>

Device and system insight – shows what's inside a directory – ls

The ls command lists the contents of your current directory.

How to do it…

Use the following command to see the list of contents in the current directory:

debian@beaglebone:~$ ls
Desktop

At the moment, the only thing you will see is the Desktop directory. As we continue adding content in subsequent chapters, there will be more that would be seen upon using ls.

Adding the -lah option at the end of the command gives more detailed information on the files, as follows:

debian@beaglebone:~$ ls -lah
total 112K
drwxr-xr-x 19 debian debian 4.0K May  4 23:37 .
drwxr-xr-x  3 root   root   4.0K May  5 00:05 ..
-rw-------  1 debian debian   55 May  4 23:37 .Xauthority
-rw-------  1 debian debian 2.4K May  5 01:41 .bash_history
-rw-r--r--  1 debian debian  220 Jan  1  2015 .bash_logout
-rw-r--r--  1 debian debian 3.4K Jan  1  2015 .bashrc
drwxr-xr-x  6 debian debian 4.0K Mar  8  2015 .cache
drwxr-xr-x  9 debian debian 4.0K Mar  4 23:51 .config
drwx------  3 debian debian 4.0K Mar  4 23:37 .dbus
-rw-r--r--  1 debian debian   35 Mar  4 23:37 .dmrc
drwxr-xr-x  2 debian debian 4.0K Mar  8  2015 .fontconfig
drwxr-xr-x 24 debian debian 4.0K Mar  8  2015 .gimp-2.8
drwx------  3 debian debian 4.0K Mar  8  2015 .local

The options in the command now reveal a variety of things about the file: their permission status (column 1), owner and group (columns 3 and 4), file sizes (column 5) , and modification date (column 6). The l option outputs in the list form, adding the a forces the command to show any hidden files, and the h option turns it all into a human readable format.

Device and system insight – find out what USB devices are connected – lsusb

This captures and lists all USB devices currently connected to the BBB while showing basic information about the device.

How to do it…

Use the following command to find out about the USB devices connected:

debian@beaglebone:~$ lsusb
Bus 001 Device 002: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Note

Note that the first device here shows a connected USB audio dongle.

Device and system insight – get information about connected devices – cat/proc/bus/input/devices

Where lusb leaves off, this command lists more detailed information about any device connected to the BBB. Note the difference in output for the same USB audio device (C-media) shown in our preceding recipe using lsusb.

How to do it…

Use the following command to get information about connected devices:

debian@beaglebone:~$ cat /proc/bus/input/devices

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="tps65217_pwr_but"
P: Phys=
S: Sysfs=/devices/ocp.3/44e0b000.i2c/i2c-0/0-0024/input/input0
U: Uniq=
H: Handlers=kbd event0
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0003 Vendor=0d8c Product=013c Version=0100
N: Name="C-Media Electronics Inc.       USB PnP Sound Device"
P: Phys=usb-musb-hdrc.1.auto-1/input3
S: Sysfs=/devices/ocp.3/47400000.usb/musb-hdrc.1.auto/usb1/1-1/1-1:1.3/input/input1
U: Uniq=
H: Handlers=kbd event1
B: PROP=0
B: EV=13
B: KEY=78 0 e0000 0 0 0
B: MSC=10

There's more…

The stem of this command is cat, which is one of the most popular commands to quickly read files without writing or modifying them:

$  cat file4

You can try it with any file to see how the screen output looks.

Device and system insight – get your version of Linux

You can find out which version and distribution of Linux you are running through several methods.

How to do it…

To find out your version of Linux, use the following command:

debian@beaglebone:~$ lsb_release -a

The screen output will look similar to this:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.6 (wheezy)
Release: 7.6
Codename: wheezy

The preceding command is a good complement to the more typical way we determine the actual kernel version, which is as follows:

$ uname -r
3.8.13-boneXX

Device and system insight – find out running processes – ps

In Linux, applications are referred to as processes, and each is given a unique ID number or PID. The ps (that is, process status) command provides information about the currently running tasks or processes. The output includes the PIDs.

How to do it…

This command takes a snapshot of your board. You can run the command unadulterated and with no options, as follows:

$ ps

But the output is a thin gruel:

debian@beaglebone:~$ ps
PID    TTY    TIME    CMD
1623    pts/0    00:00:00    bash
2035    pts/0    00:00:00    ps

So, it is often better to modify the output in order to get more insight into the running processes and display this information in a more orderly fashion, as in the following command:

$ ps aux

The following is the output:

How to do it…

The aux series of options complement the basic command by doing the following:

  • The a option shows the processes for all users. Historically, ps requires different syntax depending on our flavor of Linux or Unix. This option simplifies the method required to add options.
  • Using the u option tells the command to display the user or owner of the process in the output. The reason you want to do this is that there are processes typically running at the root level and other processes running at a non-root-user level. However, we frequently want to see all processes running regardless of user, so this option is important.
  • The x option ensures that processes that are not currently running in a terminal window—which form the majority of running processes—are also in the output that we want to see.

Device and system insight – find out the resources used by running processes – top and htop

This command takes it a bit further than the ps command as it dynamically updates not only the processes running but also the CPU resources used by the processes. The status is continuous and in real time.

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

Getting ready

Of course, we assume that if you intend to use some command line interface (CLI) commands, you have to have your command shell window open and gain root access:

sudo

Using sudo as part of your command syntax is a requirement that often bedevils new Linux users. It is important to understand that many Linux commands demand what are known as "superuser" privileges, which grant you total control over the board and its software. Having full admin rights within a command prevents any casual user from coming along and destroying your hard work, by design or accident.

The "open sesame" of Debian Linux, the magic sudo command (pronounced "soo-doo"), means "Super User do x-command" when dissected. If you don't append your command at the beginning with sudo, your mkdir command, for example, will return a "bad command" error.

sudo -i and root

Adding the -i option (the sudo -i command) provides even more control over the environment: the root user access. When running commands as a root user, there are ample opportunities to completely mangle your system, so exercise extreme caution in its use.

However, not all commands require superuser privileges. For instance, when you're really just poking around the system and not modifying files, directories, or the system itself, sudo isn't necessary.

You should keep in mind several things when working with the command line:

  • Lowercase: Using lowercase syntax is the shell's required expectation, with the exception of some options and arguments that are uppercase.
  • Arguments and options: Most commands have multiple options or arguments that can be used to fine-tune your command. We will only be able to touch on a few instances of how and when these options are used.
  • Help:
    • Help (no hyphens): Typing help before a command outputs information about this command. An example of this is $ help cd.
    • Help (hyphens): Many executable commands have more help and information about their options or arguments. An example of this is $ mkdir --help.
    • Man pages: Short for "manual pages", adding man to the beginning of many Linux commands opens up a manual for these commands. It can often yield too much information, but it can be helpful if you're looking for a deeper understanding of a command. An example of this is $ man chmod.
  • Tab command: This lists recent commands. In order to use this, you need to do the following:
    • Type a letter (or a string of letters) and then press the Tab button. All commands that begin with this letter or string will be shown. This will prevent a vast majority of typing mistakes.
    • Press Tab twice quickly, and it will show all the commands that are currently in your path.

Conventions

Early in the book, we will frequently use the entire line from the command line, including the user name, address, symbols connoting user type, directory, and so on. So, for example, you will see a line that looks similar to the following:

debian@beagle_bone:~$ mkdir

However, as we become more familiar with the shell, we will mostly shorten the command instruction to only include the command itself:

~$ mkdir

Alternatively, you will see a slight variation on the preceding command when we refer to a root user. Note the # sign in place of the $ sign in the following command:

~# mkdir

Now, let's move on to the commands. A super-duper subset of commands, anyway. We've organized them into System navigational commands, Device and system insight, and Modify content/data commands.

System navigational command – change your directory – cd

To move from one directory to another, there are numerous variations on how to do it.

How to do it…

Perform the following steps:

  1. To change from your current default directory to the Desktop directory, type in these commands:
    debian@beaglebone:~$ cd Desktop
    debian@beaglebone:~/Desktop$
    
  2. To go back one directory level, use the following command lines:
    debian@beaglebone:~/Desktop$ cd ..
    debian@beaglebone:~$
    
  3. Go to the Home directory using the following command:
    debian@beaglebone:~/Desktop$ cd ~
    debian@beaglebone:~$
    
  4. Go back to the previous directory you were in by typing in the following commands:
    debian@beaglebone:~$ cd -
    /home/debian/Desktop
    debian@beaglebone:~/Desktop$
    

There's more…

In addition to the preceding commands, you should be familiar with the four special directory symbols:

  • The current directory (.)
  • The parent directory (..)
  • The root directory (/)
  • The home directory (~)

System navigational command – show your current directory – pwd

This stands for print working directory, a means to show the user where in the system they are currently working or the absolute path relative to the current directory.

How to do it…

Use the following command to show your current directory:

debian@beaglebone:~$ pwd
/home/Debian

System navigational command – find a file – find

Looking for a file? One way to search for it is with the find function. Just be sure to add the -name option so that the output shows the directory location. Add sudo as well so that you do not get directory access errors.

How to do it…

Use the following command to find a file:

$ sudo find / -name <filename>

Device and system insight – shows what's inside a directory – ls

The ls command lists the contents of your current directory.

How to do it…

Use the following command to see the list of contents in the current directory:

debian@beaglebone:~$ ls
Desktop

At the moment, the only thing you will see is the Desktop directory. As we continue adding content in subsequent chapters, there will be more that would be seen upon using ls.

Adding the -lah option at the end of the command gives more detailed information on the files, as follows:

debian@beaglebone:~$ ls -lah
total 112K
drwxr-xr-x 19 debian debian 4.0K May  4 23:37 .
drwxr-xr-x  3 root   root   4.0K May  5 00:05 ..
-rw-------  1 debian debian   55 May  4 23:37 .Xauthority
-rw-------  1 debian debian 2.4K May  5 01:41 .bash_history
-rw-r--r--  1 debian debian  220 Jan  1  2015 .bash_logout
-rw-r--r--  1 debian debian 3.4K Jan  1  2015 .bashrc
drwxr-xr-x  6 debian debian 4.0K Mar  8  2015 .cache
drwxr-xr-x  9 debian debian 4.0K Mar  4 23:51 .config
drwx------  3 debian debian 4.0K Mar  4 23:37 .dbus
-rw-r--r--  1 debian debian   35 Mar  4 23:37 .dmrc
drwxr-xr-x  2 debian debian 4.0K Mar  8  2015 .fontconfig
drwxr-xr-x 24 debian debian 4.0K Mar  8  2015 .gimp-2.8
drwx------  3 debian debian 4.0K Mar  8  2015 .local

The options in the command now reveal a variety of things about the file: their permission status (column 1), owner and group (columns 3 and 4), file sizes (column 5) , and modification date (column 6). The l option outputs in the list form, adding the a forces the command to show any hidden files, and the h option turns it all into a human readable format.

Device and system insight – find out what USB devices are connected – lsusb

This captures and lists all USB devices currently connected to the BBB while showing basic information about the device.

How to do it…

Use the following command to find out about the USB devices connected:

debian@beaglebone:~$ lsusb
Bus 001 Device 002: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Note

Note that the first device here shows a connected USB audio dongle.

Device and system insight – get information about connected devices – cat/proc/bus/input/devices

Where lusb leaves off, this command lists more detailed information about any device connected to the BBB. Note the difference in output for the same USB audio device (C-media) shown in our preceding recipe using lsusb.

How to do it…

Use the following command to get information about connected devices:

debian@beaglebone:~$ cat /proc/bus/input/devices

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="tps65217_pwr_but"
P: Phys=
S: Sysfs=/devices/ocp.3/44e0b000.i2c/i2c-0/0-0024/input/input0
U: Uniq=
H: Handlers=kbd event0
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0003 Vendor=0d8c Product=013c Version=0100
N: Name="C-Media Electronics Inc.       USB PnP Sound Device"
P: Phys=usb-musb-hdrc.1.auto-1/input3
S: Sysfs=/devices/ocp.3/47400000.usb/musb-hdrc.1.auto/usb1/1-1/1-1:1.3/input/input1
U: Uniq=
H: Handlers=kbd event1
B: PROP=0
B: EV=13
B: KEY=78 0 e0000 0 0 0
B: MSC=10

There's more…

The stem of this command is cat, which is one of the most popular commands to quickly read files without writing or modifying them:

$  cat file4

You can try it with any file to see how the screen output looks.

Device and system insight – get your version of Linux

You can find out which version and distribution of Linux you are running through several methods.

How to do it…

To find out your version of Linux, use the following command:

debian@beaglebone:~$ lsb_release -a

The screen output will look similar to this:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.6 (wheezy)
Release: 7.6
Codename: wheezy

The preceding command is a good complement to the more typical way we determine the actual kernel version, which is as follows:

$ uname -r
3.8.13-boneXX

Device and system insight – find out running processes – ps

In Linux, applications are referred to as processes, and each is given a unique ID number or PID. The ps (that is, process status) command provides information about the currently running tasks or processes. The output includes the PIDs.

How to do it…

This command takes a snapshot of your board. You can run the command unadulterated and with no options, as follows:

$ ps

But the output is a thin gruel:

debian@beaglebone:~$ ps
PID    TTY    TIME    CMD
1623    pts/0    00:00:00    bash
2035    pts/0    00:00:00    ps

So, it is often better to modify the output in order to get more insight into the running processes and display this information in a more orderly fashion, as in the following command:

$ ps aux

The following is the output:

How to do it…

The aux series of options complement the basic command by doing the following:

  • The a option shows the processes for all users. Historically, ps requires different syntax depending on our flavor of Linux or Unix. This option simplifies the method required to add options.
  • Using the u option tells the command to display the user or owner of the process in the output. The reason you want to do this is that there are processes typically running at the root level and other processes running at a non-root-user level. However, we frequently want to see all processes running regardless of user, so this option is important.
  • The x option ensures that processes that are not currently running in a terminal window—which form the majority of running processes—are also in the output that we want to see.

Device and system insight – find out the resources used by running processes – top and htop

This command takes it a bit further than the ps command as it dynamically updates not only the processes running but also the CPU resources used by the processes. The status is continuous and in real time.

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

System navigational command – change your directory – cd

To move from one directory to another, there are numerous variations on how to do it.

How to do it…

Perform the following steps:

  1. To change from your current default directory to the Desktop directory, type in these commands:
    debian@beaglebone:~$ cd Desktop
    debian@beaglebone:~/Desktop$
    
  2. To go back one directory level, use the following command lines:
    debian@beaglebone:~/Desktop$ cd ..
    debian@beaglebone:~$
    
  3. Go to the Home directory using the following command:
    debian@beaglebone:~/Desktop$ cd ~
    debian@beaglebone:~$
    
  4. Go back to the previous directory you were in by typing in the following commands:
    debian@beaglebone:~$ cd -
    /home/debian/Desktop
    debian@beaglebone:~/Desktop$
    

There's more…

In addition to the preceding commands, you should be familiar with the four special directory symbols:

  • The current directory (.)
  • The parent directory (..)
  • The root directory (/)
  • The home directory (~)

System navigational command – show your current directory – pwd

This stands for print working directory, a means to show the user where in the system they are currently working or the absolute path relative to the current directory.

How to do it…

Use the following command to show your current directory:

debian@beaglebone:~$ pwd
/home/Debian

System navigational command – find a file – find

Looking for a file? One way to search for it is with the find function. Just be sure to add the -name option so that the output shows the directory location. Add sudo as well so that you do not get directory access errors.

How to do it…

Use the following command to find a file:

$ sudo find / -name <filename>

Device and system insight – shows what's inside a directory – ls

The ls command lists the contents of your current directory.

How to do it…

Use the following command to see the list of contents in the current directory:

debian@beaglebone:~$ ls
Desktop

At the moment, the only thing you will see is the Desktop directory. As we continue adding content in subsequent chapters, there will be more that would be seen upon using ls.

Adding the -lah option at the end of the command gives more detailed information on the files, as follows:

debian@beaglebone:~$ ls -lah
total 112K
drwxr-xr-x 19 debian debian 4.0K May  4 23:37 .
drwxr-xr-x  3 root   root   4.0K May  5 00:05 ..
-rw-------  1 debian debian   55 May  4 23:37 .Xauthority
-rw-------  1 debian debian 2.4K May  5 01:41 .bash_history
-rw-r--r--  1 debian debian  220 Jan  1  2015 .bash_logout
-rw-r--r--  1 debian debian 3.4K Jan  1  2015 .bashrc
drwxr-xr-x  6 debian debian 4.0K Mar  8  2015 .cache
drwxr-xr-x  9 debian debian 4.0K Mar  4 23:51 .config
drwx------  3 debian debian 4.0K Mar  4 23:37 .dbus
-rw-r--r--  1 debian debian   35 Mar  4 23:37 .dmrc
drwxr-xr-x  2 debian debian 4.0K Mar  8  2015 .fontconfig
drwxr-xr-x 24 debian debian 4.0K Mar  8  2015 .gimp-2.8
drwx------  3 debian debian 4.0K Mar  8  2015 .local

The options in the command now reveal a variety of things about the file: their permission status (column 1), owner and group (columns 3 and 4), file sizes (column 5) , and modification date (column 6). The l option outputs in the list form, adding the a forces the command to show any hidden files, and the h option turns it all into a human readable format.

Device and system insight – find out what USB devices are connected – lsusb

This captures and lists all USB devices currently connected to the BBB while showing basic information about the device.

How to do it…

Use the following command to find out about the USB devices connected:

debian@beaglebone:~$ lsusb
Bus 001 Device 002: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Note

Note that the first device here shows a connected USB audio dongle.

Device and system insight – get information about connected devices – cat/proc/bus/input/devices

Where lusb leaves off, this command lists more detailed information about any device connected to the BBB. Note the difference in output for the same USB audio device (C-media) shown in our preceding recipe using lsusb.

How to do it…

Use the following command to get information about connected devices:

debian@beaglebone:~$ cat /proc/bus/input/devices

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="tps65217_pwr_but"
P: Phys=
S: Sysfs=/devices/ocp.3/44e0b000.i2c/i2c-0/0-0024/input/input0
U: Uniq=
H: Handlers=kbd event0
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0003 Vendor=0d8c Product=013c Version=0100
N: Name="C-Media Electronics Inc.       USB PnP Sound Device"
P: Phys=usb-musb-hdrc.1.auto-1/input3
S: Sysfs=/devices/ocp.3/47400000.usb/musb-hdrc.1.auto/usb1/1-1/1-1:1.3/input/input1
U: Uniq=
H: Handlers=kbd event1
B: PROP=0
B: EV=13
B: KEY=78 0 e0000 0 0 0
B: MSC=10

There's more…

The stem of this command is cat, which is one of the most popular commands to quickly read files without writing or modifying them:

$  cat file4

You can try it with any file to see how the screen output looks.

Device and system insight – get your version of Linux

You can find out which version and distribution of Linux you are running through several methods.

How to do it…

To find out your version of Linux, use the following command:

debian@beaglebone:~$ lsb_release -a

The screen output will look similar to this:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.6 (wheezy)
Release: 7.6
Codename: wheezy

The preceding command is a good complement to the more typical way we determine the actual kernel version, which is as follows:

$ uname -r
3.8.13-boneXX

Device and system insight – find out running processes – ps

In Linux, applications are referred to as processes, and each is given a unique ID number or PID. The ps (that is, process status) command provides information about the currently running tasks or processes. The output includes the PIDs.

How to do it…

This command takes a snapshot of your board. You can run the command unadulterated and with no options, as follows:

$ ps

But the output is a thin gruel:

debian@beaglebone:~$ ps
PID    TTY    TIME    CMD
1623    pts/0    00:00:00    bash
2035    pts/0    00:00:00    ps

So, it is often better to modify the output in order to get more insight into the running processes and display this information in a more orderly fashion, as in the following command:

$ ps aux

The following is the output:

How to do it…

The aux series of options complement the basic command by doing the following:

  • The a option shows the processes for all users. Historically, ps requires different syntax depending on our flavor of Linux or Unix. This option simplifies the method required to add options.
  • Using the u option tells the command to display the user or owner of the process in the output. The reason you want to do this is that there are processes typically running at the root level and other processes running at a non-root-user level. However, we frequently want to see all processes running regardless of user, so this option is important.
  • The x option ensures that processes that are not currently running in a terminal window—which form the majority of running processes—are also in the output that we want to see.

Device and system insight – find out the resources used by running processes – top and htop

This command takes it a bit further than the ps command as it dynamically updates not only the processes running but also the CPU resources used by the processes. The status is continuous and in real time.

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

How to do it…

Perform the following steps:

  1. To change from your current default directory to the Desktop directory, type in these commands:
    debian@beaglebone:~$ cd Desktop
    debian@beaglebone:~/Desktop$
    
  2. To go back one directory level, use the following command lines:
    debian@beaglebone:~/Desktop$ cd ..
    debian@beaglebone:~$
    
  3. Go to the Home directory using the following command:
    debian@beaglebone:~/Desktop$ cd ~
    debian@beaglebone:~$
    
  4. Go back to the previous directory you were in by typing in the following commands:
    debian@beaglebone:~$ cd -
    /home/debian/Desktop
    debian@beaglebone:~/Desktop$
    

There's more…

In addition to the preceding commands, you should be familiar with the four special directory symbols:

  • The current directory (.)
  • The parent directory (..)
  • The root directory (/)
  • The home directory (~)

System navigational command – show your current directory – pwd

This stands for print working directory, a means to show the user where in the system they are currently working or the absolute path relative to the current directory.

How to do it…

Use the following command to show your current directory:

debian@beaglebone:~$ pwd
/home/Debian

System navigational command – find a file – find

Looking for a file? One way to search for it is with the find function. Just be sure to add the -name option so that the output shows the directory location. Add sudo as well so that you do not get directory access errors.

How to do it…

Use the following command to find a file:

$ sudo find / -name <filename>

Device and system insight – shows what's inside a directory – ls

The ls command lists the contents of your current directory.

How to do it…

Use the following command to see the list of contents in the current directory:

debian@beaglebone:~$ ls
Desktop

At the moment, the only thing you will see is the Desktop directory. As we continue adding content in subsequent chapters, there will be more that would be seen upon using ls.

Adding the -lah option at the end of the command gives more detailed information on the files, as follows:

debian@beaglebone:~$ ls -lah
total 112K
drwxr-xr-x 19 debian debian 4.0K May  4 23:37 .
drwxr-xr-x  3 root   root   4.0K May  5 00:05 ..
-rw-------  1 debian debian   55 May  4 23:37 .Xauthority
-rw-------  1 debian debian 2.4K May  5 01:41 .bash_history
-rw-r--r--  1 debian debian  220 Jan  1  2015 .bash_logout
-rw-r--r--  1 debian debian 3.4K Jan  1  2015 .bashrc
drwxr-xr-x  6 debian debian 4.0K Mar  8  2015 .cache
drwxr-xr-x  9 debian debian 4.0K Mar  4 23:51 .config
drwx------  3 debian debian 4.0K Mar  4 23:37 .dbus
-rw-r--r--  1 debian debian   35 Mar  4 23:37 .dmrc
drwxr-xr-x  2 debian debian 4.0K Mar  8  2015 .fontconfig
drwxr-xr-x 24 debian debian 4.0K Mar  8  2015 .gimp-2.8
drwx------  3 debian debian 4.0K Mar  8  2015 .local

The options in the command now reveal a variety of things about the file: their permission status (column 1), owner and group (columns 3 and 4), file sizes (column 5) , and modification date (column 6). The l option outputs in the list form, adding the a forces the command to show any hidden files, and the h option turns it all into a human readable format.

Device and system insight – find out what USB devices are connected – lsusb

This captures and lists all USB devices currently connected to the BBB while showing basic information about the device.

How to do it…

Use the following command to find out about the USB devices connected:

debian@beaglebone:~$ lsusb
Bus 001 Device 002: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Note

Note that the first device here shows a connected USB audio dongle.

Device and system insight – get information about connected devices – cat/proc/bus/input/devices

Where lusb leaves off, this command lists more detailed information about any device connected to the BBB. Note the difference in output for the same USB audio device (C-media) shown in our preceding recipe using lsusb.

How to do it…

Use the following command to get information about connected devices:

debian@beaglebone:~$ cat /proc/bus/input/devices

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="tps65217_pwr_but"
P: Phys=
S: Sysfs=/devices/ocp.3/44e0b000.i2c/i2c-0/0-0024/input/input0
U: Uniq=
H: Handlers=kbd event0
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0003 Vendor=0d8c Product=013c Version=0100
N: Name="C-Media Electronics Inc.       USB PnP Sound Device"
P: Phys=usb-musb-hdrc.1.auto-1/input3
S: Sysfs=/devices/ocp.3/47400000.usb/musb-hdrc.1.auto/usb1/1-1/1-1:1.3/input/input1
U: Uniq=
H: Handlers=kbd event1
B: PROP=0
B: EV=13
B: KEY=78 0 e0000 0 0 0
B: MSC=10

There's more…

The stem of this command is cat, which is one of the most popular commands to quickly read files without writing or modifying them:

$  cat file4

You can try it with any file to see how the screen output looks.

Device and system insight – get your version of Linux

You can find out which version and distribution of Linux you are running through several methods.

How to do it…

To find out your version of Linux, use the following command:

debian@beaglebone:~$ lsb_release -a

The screen output will look similar to this:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.6 (wheezy)
Release: 7.6
Codename: wheezy

The preceding command is a good complement to the more typical way we determine the actual kernel version, which is as follows:

$ uname -r
3.8.13-boneXX

Device and system insight – find out running processes – ps

In Linux, applications are referred to as processes, and each is given a unique ID number or PID. The ps (that is, process status) command provides information about the currently running tasks or processes. The output includes the PIDs.

How to do it…

This command takes a snapshot of your board. You can run the command unadulterated and with no options, as follows:

$ ps

But the output is a thin gruel:

debian@beaglebone:~$ ps
PID    TTY    TIME    CMD
1623    pts/0    00:00:00    bash
2035    pts/0    00:00:00    ps

So, it is often better to modify the output in order to get more insight into the running processes and display this information in a more orderly fashion, as in the following command:

$ ps aux

The following is the output:

How to do it…

The aux series of options complement the basic command by doing the following:

  • The a option shows the processes for all users. Historically, ps requires different syntax depending on our flavor of Linux or Unix. This option simplifies the method required to add options.
  • Using the u option tells the command to display the user or owner of the process in the output. The reason you want to do this is that there are processes typically running at the root level and other processes running at a non-root-user level. However, we frequently want to see all processes running regardless of user, so this option is important.
  • The x option ensures that processes that are not currently running in a terminal window—which form the majority of running processes—are also in the output that we want to see.

Device and system insight – find out the resources used by running processes – top and htop

This command takes it a bit further than the ps command as it dynamically updates not only the processes running but also the CPU resources used by the processes. The status is continuous and in real time.

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

There's more…

In addition to the preceding commands, you should be familiar with the four special directory symbols:

  • The current directory (.)
  • The parent directory (..)
  • The root directory (/)
  • The home directory (~)

System navigational command – show your current directory – pwd

This stands for print working directory, a means to show the user where in the system they are currently working or the absolute path relative to the current directory.

How to do it…

Use the following command to show your current directory:

debian@beaglebone:~$ pwd
/home/Debian

System navigational command – find a file – find

Looking for a file? One way to search for it is with the find function. Just be sure to add the -name option so that the output shows the directory location. Add sudo as well so that you do not get directory access errors.

How to do it…

Use the following command to find a file:

$ sudo find / -name <filename>

Device and system insight – shows what's inside a directory – ls

The ls command lists the contents of your current directory.

How to do it…

Use the following command to see the list of contents in the current directory:

debian@beaglebone:~$ ls
Desktop

At the moment, the only thing you will see is the Desktop directory. As we continue adding content in subsequent chapters, there will be more that would be seen upon using ls.

Adding the -lah option at the end of the command gives more detailed information on the files, as follows:

debian@beaglebone:~$ ls -lah
total 112K
drwxr-xr-x 19 debian debian 4.0K May  4 23:37 .
drwxr-xr-x  3 root   root   4.0K May  5 00:05 ..
-rw-------  1 debian debian   55 May  4 23:37 .Xauthority
-rw-------  1 debian debian 2.4K May  5 01:41 .bash_history
-rw-r--r--  1 debian debian  220 Jan  1  2015 .bash_logout
-rw-r--r--  1 debian debian 3.4K Jan  1  2015 .bashrc
drwxr-xr-x  6 debian debian 4.0K Mar  8  2015 .cache
drwxr-xr-x  9 debian debian 4.0K Mar  4 23:51 .config
drwx------  3 debian debian 4.0K Mar  4 23:37 .dbus
-rw-r--r--  1 debian debian   35 Mar  4 23:37 .dmrc
drwxr-xr-x  2 debian debian 4.0K Mar  8  2015 .fontconfig
drwxr-xr-x 24 debian debian 4.0K Mar  8  2015 .gimp-2.8
drwx------  3 debian debian 4.0K Mar  8  2015 .local

The options in the command now reveal a variety of things about the file: their permission status (column 1), owner and group (columns 3 and 4), file sizes (column 5) , and modification date (column 6). The l option outputs in the list form, adding the a forces the command to show any hidden files, and the h option turns it all into a human readable format.

Device and system insight – find out what USB devices are connected – lsusb

This captures and lists all USB devices currently connected to the BBB while showing basic information about the device.

How to do it…

Use the following command to find out about the USB devices connected:

debian@beaglebone:~$ lsusb
Bus 001 Device 002: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Note

Note that the first device here shows a connected USB audio dongle.

Device and system insight – get information about connected devices – cat/proc/bus/input/devices

Where lusb leaves off, this command lists more detailed information about any device connected to the BBB. Note the difference in output for the same USB audio device (C-media) shown in our preceding recipe using lsusb.

How to do it…

Use the following command to get information about connected devices:

debian@beaglebone:~$ cat /proc/bus/input/devices

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="tps65217_pwr_but"
P: Phys=
S: Sysfs=/devices/ocp.3/44e0b000.i2c/i2c-0/0-0024/input/input0
U: Uniq=
H: Handlers=kbd event0
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0003 Vendor=0d8c Product=013c Version=0100
N: Name="C-Media Electronics Inc.       USB PnP Sound Device"
P: Phys=usb-musb-hdrc.1.auto-1/input3
S: Sysfs=/devices/ocp.3/47400000.usb/musb-hdrc.1.auto/usb1/1-1/1-1:1.3/input/input1
U: Uniq=
H: Handlers=kbd event1
B: PROP=0
B: EV=13
B: KEY=78 0 e0000 0 0 0
B: MSC=10

There's more…

The stem of this command is cat, which is one of the most popular commands to quickly read files without writing or modifying them:

$  cat file4

You can try it with any file to see how the screen output looks.

Device and system insight – get your version of Linux

You can find out which version and distribution of Linux you are running through several methods.

How to do it…

To find out your version of Linux, use the following command:

debian@beaglebone:~$ lsb_release -a

The screen output will look similar to this:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.6 (wheezy)
Release: 7.6
Codename: wheezy

The preceding command is a good complement to the more typical way we determine the actual kernel version, which is as follows:

$ uname -r
3.8.13-boneXX

Device and system insight – find out running processes – ps

In Linux, applications are referred to as processes, and each is given a unique ID number or PID. The ps (that is, process status) command provides information about the currently running tasks or processes. The output includes the PIDs.

How to do it…

This command takes a snapshot of your board. You can run the command unadulterated and with no options, as follows:

$ ps

But the output is a thin gruel:

debian@beaglebone:~$ ps
PID    TTY    TIME    CMD
1623    pts/0    00:00:00    bash
2035    pts/0    00:00:00    ps

So, it is often better to modify the output in order to get more insight into the running processes and display this information in a more orderly fashion, as in the following command:

$ ps aux

The following is the output:

How to do it…

The aux series of options complement the basic command by doing the following:

  • The a option shows the processes for all users. Historically, ps requires different syntax depending on our flavor of Linux or Unix. This option simplifies the method required to add options.
  • Using the u option tells the command to display the user or owner of the process in the output. The reason you want to do this is that there are processes typically running at the root level and other processes running at a non-root-user level. However, we frequently want to see all processes running regardless of user, so this option is important.
  • The x option ensures that processes that are not currently running in a terminal window—which form the majority of running processes—are also in the output that we want to see.

Device and system insight – find out the resources used by running processes – top and htop

This command takes it a bit further than the ps command as it dynamically updates not only the processes running but also the CPU resources used by the processes. The status is continuous and in real time.

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

System navigational command – show your current directory – pwd

This stands for print working directory, a means to show the user where in the system they are currently working or the absolute path relative to the current directory.

How to do it…

Use the following command to show your current directory:

debian@beaglebone:~$ pwd
/home/Debian

System navigational command – find a file – find

Looking for a file? One way to search for it is with the find function. Just be sure to add the -name option so that the output shows the directory location. Add sudo as well so that you do not get directory access errors.

How to do it…

Use the following command to find a file:

$ sudo find / -name <filename>

Device and system insight – shows what's inside a directory – ls

The ls command lists the contents of your current directory.

How to do it…

Use the following command to see the list of contents in the current directory:

debian@beaglebone:~$ ls
Desktop

At the moment, the only thing you will see is the Desktop directory. As we continue adding content in subsequent chapters, there will be more that would be seen upon using ls.

Adding the -lah option at the end of the command gives more detailed information on the files, as follows:

debian@beaglebone:~$ ls -lah
total 112K
drwxr-xr-x 19 debian debian 4.0K May  4 23:37 .
drwxr-xr-x  3 root   root   4.0K May  5 00:05 ..
-rw-------  1 debian debian   55 May  4 23:37 .Xauthority
-rw-------  1 debian debian 2.4K May  5 01:41 .bash_history
-rw-r--r--  1 debian debian  220 Jan  1  2015 .bash_logout
-rw-r--r--  1 debian debian 3.4K Jan  1  2015 .bashrc
drwxr-xr-x  6 debian debian 4.0K Mar  8  2015 .cache
drwxr-xr-x  9 debian debian 4.0K Mar  4 23:51 .config
drwx------  3 debian debian 4.0K Mar  4 23:37 .dbus
-rw-r--r--  1 debian debian   35 Mar  4 23:37 .dmrc
drwxr-xr-x  2 debian debian 4.0K Mar  8  2015 .fontconfig
drwxr-xr-x 24 debian debian 4.0K Mar  8  2015 .gimp-2.8
drwx------  3 debian debian 4.0K Mar  8  2015 .local

The options in the command now reveal a variety of things about the file: their permission status (column 1), owner and group (columns 3 and 4), file sizes (column 5) , and modification date (column 6). The l option outputs in the list form, adding the a forces the command to show any hidden files, and the h option turns it all into a human readable format.

Device and system insight – find out what USB devices are connected – lsusb

This captures and lists all USB devices currently connected to the BBB while showing basic information about the device.

How to do it…

Use the following command to find out about the USB devices connected:

debian@beaglebone:~$ lsusb
Bus 001 Device 002: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Note

Note that the first device here shows a connected USB audio dongle.

Device and system insight – get information about connected devices – cat/proc/bus/input/devices

Where lusb leaves off, this command lists more detailed information about any device connected to the BBB. Note the difference in output for the same USB audio device (C-media) shown in our preceding recipe using lsusb.

How to do it…

Use the following command to get information about connected devices:

debian@beaglebone:~$ cat /proc/bus/input/devices

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="tps65217_pwr_but"
P: Phys=
S: Sysfs=/devices/ocp.3/44e0b000.i2c/i2c-0/0-0024/input/input0
U: Uniq=
H: Handlers=kbd event0
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0003 Vendor=0d8c Product=013c Version=0100
N: Name="C-Media Electronics Inc.       USB PnP Sound Device"
P: Phys=usb-musb-hdrc.1.auto-1/input3
S: Sysfs=/devices/ocp.3/47400000.usb/musb-hdrc.1.auto/usb1/1-1/1-1:1.3/input/input1
U: Uniq=
H: Handlers=kbd event1
B: PROP=0
B: EV=13
B: KEY=78 0 e0000 0 0 0
B: MSC=10

There's more…

The stem of this command is cat, which is one of the most popular commands to quickly read files without writing or modifying them:

$  cat file4

You can try it with any file to see how the screen output looks.

Device and system insight – get your version of Linux

You can find out which version and distribution of Linux you are running through several methods.

How to do it…

To find out your version of Linux, use the following command:

debian@beaglebone:~$ lsb_release -a

The screen output will look similar to this:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.6 (wheezy)
Release: 7.6
Codename: wheezy

The preceding command is a good complement to the more typical way we determine the actual kernel version, which is as follows:

$ uname -r
3.8.13-boneXX

Device and system insight – find out running processes – ps

In Linux, applications are referred to as processes, and each is given a unique ID number or PID. The ps (that is, process status) command provides information about the currently running tasks or processes. The output includes the PIDs.

How to do it…

This command takes a snapshot of your board. You can run the command unadulterated and with no options, as follows:

$ ps

But the output is a thin gruel:

debian@beaglebone:~$ ps
PID    TTY    TIME    CMD
1623    pts/0    00:00:00    bash
2035    pts/0    00:00:00    ps

So, it is often better to modify the output in order to get more insight into the running processes and display this information in a more orderly fashion, as in the following command:

$ ps aux

The following is the output:

How to do it…

The aux series of options complement the basic command by doing the following:

  • The a option shows the processes for all users. Historically, ps requires different syntax depending on our flavor of Linux or Unix. This option simplifies the method required to add options.
  • Using the u option tells the command to display the user or owner of the process in the output. The reason you want to do this is that there are processes typically running at the root level and other processes running at a non-root-user level. However, we frequently want to see all processes running regardless of user, so this option is important.
  • The x option ensures that processes that are not currently running in a terminal window—which form the majority of running processes—are also in the output that we want to see.

Device and system insight – find out the resources used by running processes – top and htop

This command takes it a bit further than the ps command as it dynamically updates not only the processes running but also the CPU resources used by the processes. The status is continuous and in real time.

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

How to do it…

Use the following command to show your current directory:

debian@beaglebone:~$ pwd
/home/Debian

System navigational command – find a file – find

Looking for a file? One way to search for it is with the find function. Just be sure to add the -name option so that the output shows the directory location. Add sudo as well so that you do not get directory access errors.

How to do it…

Use the following command to find a file:

$ sudo find / -name <filename>

Device and system insight – shows what's inside a directory – ls

The ls command lists the contents of your current directory.

How to do it…

Use the following command to see the list of contents in the current directory:

debian@beaglebone:~$ ls
Desktop

At the moment, the only thing you will see is the Desktop directory. As we continue adding content in subsequent chapters, there will be more that would be seen upon using ls.

Adding the -lah option at the end of the command gives more detailed information on the files, as follows:

debian@beaglebone:~$ ls -lah
total 112K
drwxr-xr-x 19 debian debian 4.0K May  4 23:37 .
drwxr-xr-x  3 root   root   4.0K May  5 00:05 ..
-rw-------  1 debian debian   55 May  4 23:37 .Xauthority
-rw-------  1 debian debian 2.4K May  5 01:41 .bash_history
-rw-r--r--  1 debian debian  220 Jan  1  2015 .bash_logout
-rw-r--r--  1 debian debian 3.4K Jan  1  2015 .bashrc
drwxr-xr-x  6 debian debian 4.0K Mar  8  2015 .cache
drwxr-xr-x  9 debian debian 4.0K Mar  4 23:51 .config
drwx------  3 debian debian 4.0K Mar  4 23:37 .dbus
-rw-r--r--  1 debian debian   35 Mar  4 23:37 .dmrc
drwxr-xr-x  2 debian debian 4.0K Mar  8  2015 .fontconfig
drwxr-xr-x 24 debian debian 4.0K Mar  8  2015 .gimp-2.8
drwx------  3 debian debian 4.0K Mar  8  2015 .local

The options in the command now reveal a variety of things about the file: their permission status (column 1), owner and group (columns 3 and 4), file sizes (column 5) , and modification date (column 6). The l option outputs in the list form, adding the a forces the command to show any hidden files, and the h option turns it all into a human readable format.

Device and system insight – find out what USB devices are connected – lsusb

This captures and lists all USB devices currently connected to the BBB while showing basic information about the device.

How to do it…

Use the following command to find out about the USB devices connected:

debian@beaglebone:~$ lsusb
Bus 001 Device 002: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Note

Note that the first device here shows a connected USB audio dongle.

Device and system insight – get information about connected devices – cat/proc/bus/input/devices

Where lusb leaves off, this command lists more detailed information about any device connected to the BBB. Note the difference in output for the same USB audio device (C-media) shown in our preceding recipe using lsusb.

How to do it…

Use the following command to get information about connected devices:

debian@beaglebone:~$ cat /proc/bus/input/devices

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="tps65217_pwr_but"
P: Phys=
S: Sysfs=/devices/ocp.3/44e0b000.i2c/i2c-0/0-0024/input/input0
U: Uniq=
H: Handlers=kbd event0
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0003 Vendor=0d8c Product=013c Version=0100
N: Name="C-Media Electronics Inc.       USB PnP Sound Device"
P: Phys=usb-musb-hdrc.1.auto-1/input3
S: Sysfs=/devices/ocp.3/47400000.usb/musb-hdrc.1.auto/usb1/1-1/1-1:1.3/input/input1
U: Uniq=
H: Handlers=kbd event1
B: PROP=0
B: EV=13
B: KEY=78 0 e0000 0 0 0
B: MSC=10

There's more…

The stem of this command is cat, which is one of the most popular commands to quickly read files without writing or modifying them:

$  cat file4

You can try it with any file to see how the screen output looks.

Device and system insight – get your version of Linux

You can find out which version and distribution of Linux you are running through several methods.

How to do it…

To find out your version of Linux, use the following command:

debian@beaglebone:~$ lsb_release -a

The screen output will look similar to this:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.6 (wheezy)
Release: 7.6
Codename: wheezy

The preceding command is a good complement to the more typical way we determine the actual kernel version, which is as follows:

$ uname -r
3.8.13-boneXX

Device and system insight – find out running processes – ps

In Linux, applications are referred to as processes, and each is given a unique ID number or PID. The ps (that is, process status) command provides information about the currently running tasks or processes. The output includes the PIDs.

How to do it…

This command takes a snapshot of your board. You can run the command unadulterated and with no options, as follows:

$ ps

But the output is a thin gruel:

debian@beaglebone:~$ ps
PID    TTY    TIME    CMD
1623    pts/0    00:00:00    bash
2035    pts/0    00:00:00    ps

So, it is often better to modify the output in order to get more insight into the running processes and display this information in a more orderly fashion, as in the following command:

$ ps aux

The following is the output:

How to do it…

The aux series of options complement the basic command by doing the following:

  • The a option shows the processes for all users. Historically, ps requires different syntax depending on our flavor of Linux or Unix. This option simplifies the method required to add options.
  • Using the u option tells the command to display the user or owner of the process in the output. The reason you want to do this is that there are processes typically running at the root level and other processes running at a non-root-user level. However, we frequently want to see all processes running regardless of user, so this option is important.
  • The x option ensures that processes that are not currently running in a terminal window—which form the majority of running processes—are also in the output that we want to see.

Device and system insight – find out the resources used by running processes – top and htop

This command takes it a bit further than the ps command as it dynamically updates not only the processes running but also the CPU resources used by the processes. The status is continuous and in real time.

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

System navigational command – find a file – find

Looking for a file? One way to search for it is with the find function. Just be sure to add the -name option so that the output shows the directory location. Add sudo as well so that you do not get directory access errors.

How to do it…

Use the following command to find a file:

$ sudo find / -name <filename>

Device and system insight – shows what's inside a directory – ls

The ls command lists the contents of your current directory.

How to do it…

Use the following command to see the list of contents in the current directory:

debian@beaglebone:~$ ls
Desktop

At the moment, the only thing you will see is the Desktop directory. As we continue adding content in subsequent chapters, there will be more that would be seen upon using ls.

Adding the -lah option at the end of the command gives more detailed information on the files, as follows:

debian@beaglebone:~$ ls -lah
total 112K
drwxr-xr-x 19 debian debian 4.0K May  4 23:37 .
drwxr-xr-x  3 root   root   4.0K May  5 00:05 ..
-rw-------  1 debian debian   55 May  4 23:37 .Xauthority
-rw-------  1 debian debian 2.4K May  5 01:41 .bash_history
-rw-r--r--  1 debian debian  220 Jan  1  2015 .bash_logout
-rw-r--r--  1 debian debian 3.4K Jan  1  2015 .bashrc
drwxr-xr-x  6 debian debian 4.0K Mar  8  2015 .cache
drwxr-xr-x  9 debian debian 4.0K Mar  4 23:51 .config
drwx------  3 debian debian 4.0K Mar  4 23:37 .dbus
-rw-r--r--  1 debian debian   35 Mar  4 23:37 .dmrc
drwxr-xr-x  2 debian debian 4.0K Mar  8  2015 .fontconfig
drwxr-xr-x 24 debian debian 4.0K Mar  8  2015 .gimp-2.8
drwx------  3 debian debian 4.0K Mar  8  2015 .local

The options in the command now reveal a variety of things about the file: their permission status (column 1), owner and group (columns 3 and 4), file sizes (column 5) , and modification date (column 6). The l option outputs in the list form, adding the a forces the command to show any hidden files, and the h option turns it all into a human readable format.

Device and system insight – find out what USB devices are connected – lsusb

This captures and lists all USB devices currently connected to the BBB while showing basic information about the device.

How to do it…

Use the following command to find out about the USB devices connected:

debian@beaglebone:~$ lsusb
Bus 001 Device 002: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Note

Note that the first device here shows a connected USB audio dongle.

Device and system insight – get information about connected devices – cat/proc/bus/input/devices

Where lusb leaves off, this command lists more detailed information about any device connected to the BBB. Note the difference in output for the same USB audio device (C-media) shown in our preceding recipe using lsusb.

How to do it…

Use the following command to get information about connected devices:

debian@beaglebone:~$ cat /proc/bus/input/devices

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="tps65217_pwr_but"
P: Phys=
S: Sysfs=/devices/ocp.3/44e0b000.i2c/i2c-0/0-0024/input/input0
U: Uniq=
H: Handlers=kbd event0
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0003 Vendor=0d8c Product=013c Version=0100
N: Name="C-Media Electronics Inc.       USB PnP Sound Device"
P: Phys=usb-musb-hdrc.1.auto-1/input3
S: Sysfs=/devices/ocp.3/47400000.usb/musb-hdrc.1.auto/usb1/1-1/1-1:1.3/input/input1
U: Uniq=
H: Handlers=kbd event1
B: PROP=0
B: EV=13
B: KEY=78 0 e0000 0 0 0
B: MSC=10

There's more…

The stem of this command is cat, which is one of the most popular commands to quickly read files without writing or modifying them:

$  cat file4

You can try it with any file to see how the screen output looks.

Device and system insight – get your version of Linux

You can find out which version and distribution of Linux you are running through several methods.

How to do it…

To find out your version of Linux, use the following command:

debian@beaglebone:~$ lsb_release -a

The screen output will look similar to this:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.6 (wheezy)
Release: 7.6
Codename: wheezy

The preceding command is a good complement to the more typical way we determine the actual kernel version, which is as follows:

$ uname -r
3.8.13-boneXX

Device and system insight – find out running processes – ps

In Linux, applications are referred to as processes, and each is given a unique ID number or PID. The ps (that is, process status) command provides information about the currently running tasks or processes. The output includes the PIDs.

How to do it…

This command takes a snapshot of your board. You can run the command unadulterated and with no options, as follows:

$ ps

But the output is a thin gruel:

debian@beaglebone:~$ ps
PID    TTY    TIME    CMD
1623    pts/0    00:00:00    bash
2035    pts/0    00:00:00    ps

So, it is often better to modify the output in order to get more insight into the running processes and display this information in a more orderly fashion, as in the following command:

$ ps aux

The following is the output:

How to do it…

The aux series of options complement the basic command by doing the following:

  • The a option shows the processes for all users. Historically, ps requires different syntax depending on our flavor of Linux or Unix. This option simplifies the method required to add options.
  • Using the u option tells the command to display the user or owner of the process in the output. The reason you want to do this is that there are processes typically running at the root level and other processes running at a non-root-user level. However, we frequently want to see all processes running regardless of user, so this option is important.
  • The x option ensures that processes that are not currently running in a terminal window—which form the majority of running processes—are also in the output that we want to see.

Device and system insight – find out the resources used by running processes – top and htop

This command takes it a bit further than the ps command as it dynamically updates not only the processes running but also the CPU resources used by the processes. The status is continuous and in real time.

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

How to do it…

Use the following command to find a file:

$ sudo find / -name <filename>

Device and system insight – shows what's inside a directory – ls

The ls command lists the contents of your current directory.

How to do it…

Use the following command to see the list of contents in the current directory:

debian@beaglebone:~$ ls
Desktop

At the moment, the only thing you will see is the Desktop directory. As we continue adding content in subsequent chapters, there will be more that would be seen upon using ls.

Adding the -lah option at the end of the command gives more detailed information on the files, as follows:

debian@beaglebone:~$ ls -lah
total 112K
drwxr-xr-x 19 debian debian 4.0K May  4 23:37 .
drwxr-xr-x  3 root   root   4.0K May  5 00:05 ..
-rw-------  1 debian debian   55 May  4 23:37 .Xauthority
-rw-------  1 debian debian 2.4K May  5 01:41 .bash_history
-rw-r--r--  1 debian debian  220 Jan  1  2015 .bash_logout
-rw-r--r--  1 debian debian 3.4K Jan  1  2015 .bashrc
drwxr-xr-x  6 debian debian 4.0K Mar  8  2015 .cache
drwxr-xr-x  9 debian debian 4.0K Mar  4 23:51 .config
drwx------  3 debian debian 4.0K Mar  4 23:37 .dbus
-rw-r--r--  1 debian debian   35 Mar  4 23:37 .dmrc
drwxr-xr-x  2 debian debian 4.0K Mar  8  2015 .fontconfig
drwxr-xr-x 24 debian debian 4.0K Mar  8  2015 .gimp-2.8
drwx------  3 debian debian 4.0K Mar  8  2015 .local

The options in the command now reveal a variety of things about the file: their permission status (column 1), owner and group (columns 3 and 4), file sizes (column 5) , and modification date (column 6). The l option outputs in the list form, adding the a forces the command to show any hidden files, and the h option turns it all into a human readable format.

Device and system insight – find out what USB devices are connected – lsusb

This captures and lists all USB devices currently connected to the BBB while showing basic information about the device.

How to do it…

Use the following command to find out about the USB devices connected:

debian@beaglebone:~$ lsusb
Bus 001 Device 002: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Note

Note that the first device here shows a connected USB audio dongle.

Device and system insight – get information about connected devices – cat/proc/bus/input/devices

Where lusb leaves off, this command lists more detailed information about any device connected to the BBB. Note the difference in output for the same USB audio device (C-media) shown in our preceding recipe using lsusb.

How to do it…

Use the following command to get information about connected devices:

debian@beaglebone:~$ cat /proc/bus/input/devices

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="tps65217_pwr_but"
P: Phys=
S: Sysfs=/devices/ocp.3/44e0b000.i2c/i2c-0/0-0024/input/input0
U: Uniq=
H: Handlers=kbd event0
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0003 Vendor=0d8c Product=013c Version=0100
N: Name="C-Media Electronics Inc.       USB PnP Sound Device"
P: Phys=usb-musb-hdrc.1.auto-1/input3
S: Sysfs=/devices/ocp.3/47400000.usb/musb-hdrc.1.auto/usb1/1-1/1-1:1.3/input/input1
U: Uniq=
H: Handlers=kbd event1
B: PROP=0
B: EV=13
B: KEY=78 0 e0000 0 0 0
B: MSC=10

There's more…

The stem of this command is cat, which is one of the most popular commands to quickly read files without writing or modifying them:

$  cat file4

You can try it with any file to see how the screen output looks.

Device and system insight – get your version of Linux

You can find out which version and distribution of Linux you are running through several methods.

How to do it…

To find out your version of Linux, use the following command:

debian@beaglebone:~$ lsb_release -a

The screen output will look similar to this:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.6 (wheezy)
Release: 7.6
Codename: wheezy

The preceding command is a good complement to the more typical way we determine the actual kernel version, which is as follows:

$ uname -r
3.8.13-boneXX

Device and system insight – find out running processes – ps

In Linux, applications are referred to as processes, and each is given a unique ID number or PID. The ps (that is, process status) command provides information about the currently running tasks or processes. The output includes the PIDs.

How to do it…

This command takes a snapshot of your board. You can run the command unadulterated and with no options, as follows:

$ ps

But the output is a thin gruel:

debian@beaglebone:~$ ps
PID    TTY    TIME    CMD
1623    pts/0    00:00:00    bash
2035    pts/0    00:00:00    ps

So, it is often better to modify the output in order to get more insight into the running processes and display this information in a more orderly fashion, as in the following command:

$ ps aux

The following is the output:

How to do it…

The aux series of options complement the basic command by doing the following:

  • The a option shows the processes for all users. Historically, ps requires different syntax depending on our flavor of Linux or Unix. This option simplifies the method required to add options.
  • Using the u option tells the command to display the user or owner of the process in the output. The reason you want to do this is that there are processes typically running at the root level and other processes running at a non-root-user level. However, we frequently want to see all processes running regardless of user, so this option is important.
  • The x option ensures that processes that are not currently running in a terminal window—which form the majority of running processes—are also in the output that we want to see.

Device and system insight – find out the resources used by running processes – top and htop

This command takes it a bit further than the ps command as it dynamically updates not only the processes running but also the CPU resources used by the processes. The status is continuous and in real time.

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

Device and system insight – shows what's inside a directory – ls

The ls command lists the contents of your current directory.

How to do it…

Use the following command to see the list of contents in the current directory:

debian@beaglebone:~$ ls
Desktop

At the moment, the only thing you will see is the Desktop directory. As we continue adding content in subsequent chapters, there will be more that would be seen upon using ls.

Adding the -lah option at the end of the command gives more detailed information on the files, as follows:

debian@beaglebone:~$ ls -lah
total 112K
drwxr-xr-x 19 debian debian 4.0K May  4 23:37 .
drwxr-xr-x  3 root   root   4.0K May  5 00:05 ..
-rw-------  1 debian debian   55 May  4 23:37 .Xauthority
-rw-------  1 debian debian 2.4K May  5 01:41 .bash_history
-rw-r--r--  1 debian debian  220 Jan  1  2015 .bash_logout
-rw-r--r--  1 debian debian 3.4K Jan  1  2015 .bashrc
drwxr-xr-x  6 debian debian 4.0K Mar  8  2015 .cache
drwxr-xr-x  9 debian debian 4.0K Mar  4 23:51 .config
drwx------  3 debian debian 4.0K Mar  4 23:37 .dbus
-rw-r--r--  1 debian debian   35 Mar  4 23:37 .dmrc
drwxr-xr-x  2 debian debian 4.0K Mar  8  2015 .fontconfig
drwxr-xr-x 24 debian debian 4.0K Mar  8  2015 .gimp-2.8
drwx------  3 debian debian 4.0K Mar  8  2015 .local

The options in the command now reveal a variety of things about the file: their permission status (column 1), owner and group (columns 3 and 4), file sizes (column 5) , and modification date (column 6). The l option outputs in the list form, adding the a forces the command to show any hidden files, and the h option turns it all into a human readable format.

Device and system insight – find out what USB devices are connected – lsusb

This captures and lists all USB devices currently connected to the BBB while showing basic information about the device.

How to do it…

Use the following command to find out about the USB devices connected:

debian@beaglebone:~$ lsusb
Bus 001 Device 002: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Note

Note that the first device here shows a connected USB audio dongle.

Device and system insight – get information about connected devices – cat/proc/bus/input/devices

Where lusb leaves off, this command lists more detailed information about any device connected to the BBB. Note the difference in output for the same USB audio device (C-media) shown in our preceding recipe using lsusb.

How to do it…

Use the following command to get information about connected devices:

debian@beaglebone:~$ cat /proc/bus/input/devices

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="tps65217_pwr_but"
P: Phys=
S: Sysfs=/devices/ocp.3/44e0b000.i2c/i2c-0/0-0024/input/input0
U: Uniq=
H: Handlers=kbd event0
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0003 Vendor=0d8c Product=013c Version=0100
N: Name="C-Media Electronics Inc.       USB PnP Sound Device"
P: Phys=usb-musb-hdrc.1.auto-1/input3
S: Sysfs=/devices/ocp.3/47400000.usb/musb-hdrc.1.auto/usb1/1-1/1-1:1.3/input/input1
U: Uniq=
H: Handlers=kbd event1
B: PROP=0
B: EV=13
B: KEY=78 0 e0000 0 0 0
B: MSC=10

There's more…

The stem of this command is cat, which is one of the most popular commands to quickly read files without writing or modifying them:

$  cat file4

You can try it with any file to see how the screen output looks.

Device and system insight – get your version of Linux

You can find out which version and distribution of Linux you are running through several methods.

How to do it…

To find out your version of Linux, use the following command:

debian@beaglebone:~$ lsb_release -a

The screen output will look similar to this:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.6 (wheezy)
Release: 7.6
Codename: wheezy

The preceding command is a good complement to the more typical way we determine the actual kernel version, which is as follows:

$ uname -r
3.8.13-boneXX

Device and system insight – find out running processes – ps

In Linux, applications are referred to as processes, and each is given a unique ID number or PID. The ps (that is, process status) command provides information about the currently running tasks or processes. The output includes the PIDs.

How to do it…

This command takes a snapshot of your board. You can run the command unadulterated and with no options, as follows:

$ ps

But the output is a thin gruel:

debian@beaglebone:~$ ps
PID    TTY    TIME    CMD
1623    pts/0    00:00:00    bash
2035    pts/0    00:00:00    ps

So, it is often better to modify the output in order to get more insight into the running processes and display this information in a more orderly fashion, as in the following command:

$ ps aux

The following is the output:

How to do it…

The aux series of options complement the basic command by doing the following:

  • The a option shows the processes for all users. Historically, ps requires different syntax depending on our flavor of Linux or Unix. This option simplifies the method required to add options.
  • Using the u option tells the command to display the user or owner of the process in the output. The reason you want to do this is that there are processes typically running at the root level and other processes running at a non-root-user level. However, we frequently want to see all processes running regardless of user, so this option is important.
  • The x option ensures that processes that are not currently running in a terminal window—which form the majority of running processes—are also in the output that we want to see.

Device and system insight – find out the resources used by running processes – top and htop

This command takes it a bit further than the ps command as it dynamically updates not only the processes running but also the CPU resources used by the processes. The status is continuous and in real time.

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

How to do it…

Use the following command to see the list of contents in the current directory:

debian@beaglebone:~$ ls
Desktop

At the moment, the only thing you will see is the Desktop directory. As we continue adding content in subsequent chapters, there will be more that would be seen upon using ls.

Adding the -lah option at the end of the command gives more detailed information on the files, as follows:

debian@beaglebone:~$ ls -lah
total 112K
drwxr-xr-x 19 debian debian 4.0K May  4 23:37 .
drwxr-xr-x  3 root   root   4.0K May  5 00:05 ..
-rw-------  1 debian debian   55 May  4 23:37 .Xauthority
-rw-------  1 debian debian 2.4K May  5 01:41 .bash_history
-rw-r--r--  1 debian debian  220 Jan  1  2015 .bash_logout
-rw-r--r--  1 debian debian 3.4K Jan  1  2015 .bashrc
drwxr-xr-x  6 debian debian 4.0K Mar  8  2015 .cache
drwxr-xr-x  9 debian debian 4.0K Mar  4 23:51 .config
drwx------  3 debian debian 4.0K Mar  4 23:37 .dbus
-rw-r--r--  1 debian debian   35 Mar  4 23:37 .dmrc
drwxr-xr-x  2 debian debian 4.0K Mar  8  2015 .fontconfig
drwxr-xr-x 24 debian debian 4.0K Mar  8  2015 .gimp-2.8
drwx------  3 debian debian 4.0K Mar  8  2015 .local

The options in the command now reveal a variety of things about the file: their permission status (column 1), owner and group (columns 3 and 4), file sizes (column 5) , and modification date (column 6). The l option outputs in the list form, adding the a forces the command to show any hidden files, and the h option turns it all into a human readable format.

Device and system insight – find out what USB devices are connected – lsusb

This captures and lists all USB devices currently connected to the BBB while showing basic information about the device.

How to do it…

Use the following command to find out about the USB devices connected:

debian@beaglebone:~$ lsusb
Bus 001 Device 002: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Note

Note that the first device here shows a connected USB audio dongle.

Device and system insight – get information about connected devices – cat/proc/bus/input/devices

Where lusb leaves off, this command lists more detailed information about any device connected to the BBB. Note the difference in output for the same USB audio device (C-media) shown in our preceding recipe using lsusb.

How to do it…

Use the following command to get information about connected devices:

debian@beaglebone:~$ cat /proc/bus/input/devices

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="tps65217_pwr_but"
P: Phys=
S: Sysfs=/devices/ocp.3/44e0b000.i2c/i2c-0/0-0024/input/input0
U: Uniq=
H: Handlers=kbd event0
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0003 Vendor=0d8c Product=013c Version=0100
N: Name="C-Media Electronics Inc.       USB PnP Sound Device"
P: Phys=usb-musb-hdrc.1.auto-1/input3
S: Sysfs=/devices/ocp.3/47400000.usb/musb-hdrc.1.auto/usb1/1-1/1-1:1.3/input/input1
U: Uniq=
H: Handlers=kbd event1
B: PROP=0
B: EV=13
B: KEY=78 0 e0000 0 0 0
B: MSC=10

There's more…

The stem of this command is cat, which is one of the most popular commands to quickly read files without writing or modifying them:

$  cat file4

You can try it with any file to see how the screen output looks.

Device and system insight – get your version of Linux

You can find out which version and distribution of Linux you are running through several methods.

How to do it…

To find out your version of Linux, use the following command:

debian@beaglebone:~$ lsb_release -a

The screen output will look similar to this:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.6 (wheezy)
Release: 7.6
Codename: wheezy

The preceding command is a good complement to the more typical way we determine the actual kernel version, which is as follows:

$ uname -r
3.8.13-boneXX

Device and system insight – find out running processes – ps

In Linux, applications are referred to as processes, and each is given a unique ID number or PID. The ps (that is, process status) command provides information about the currently running tasks or processes. The output includes the PIDs.

How to do it…

This command takes a snapshot of your board. You can run the command unadulterated and with no options, as follows:

$ ps

But the output is a thin gruel:

debian@beaglebone:~$ ps
PID    TTY    TIME    CMD
1623    pts/0    00:00:00    bash
2035    pts/0    00:00:00    ps

So, it is often better to modify the output in order to get more insight into the running processes and display this information in a more orderly fashion, as in the following command:

$ ps aux

The following is the output:

How to do it…

The aux series of options complement the basic command by doing the following:

  • The a option shows the processes for all users. Historically, ps requires different syntax depending on our flavor of Linux or Unix. This option simplifies the method required to add options.
  • Using the u option tells the command to display the user or owner of the process in the output. The reason you want to do this is that there are processes typically running at the root level and other processes running at a non-root-user level. However, we frequently want to see all processes running regardless of user, so this option is important.
  • The x option ensures that processes that are not currently running in a terminal window—which form the majority of running processes—are also in the output that we want to see.

Device and system insight – find out the resources used by running processes – top and htop

This command takes it a bit further than the ps command as it dynamically updates not only the processes running but also the CPU resources used by the processes. The status is continuous and in real time.

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

Device and system insight – find out what USB devices are connected – lsusb

This captures and lists all USB devices currently connected to the BBB while showing basic information about the device.

How to do it…

Use the following command to find out about the USB devices connected:

debian@beaglebone:~$ lsusb
Bus 001 Device 002: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Note

Note that the first device here shows a connected USB audio dongle.

Device and system insight – get information about connected devices – cat/proc/bus/input/devices

Where lusb leaves off, this command lists more detailed information about any device connected to the BBB. Note the difference in output for the same USB audio device (C-media) shown in our preceding recipe using lsusb.

How to do it…

Use the following command to get information about connected devices:

debian@beaglebone:~$ cat /proc/bus/input/devices

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="tps65217_pwr_but"
P: Phys=
S: Sysfs=/devices/ocp.3/44e0b000.i2c/i2c-0/0-0024/input/input0
U: Uniq=
H: Handlers=kbd event0
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0003 Vendor=0d8c Product=013c Version=0100
N: Name="C-Media Electronics Inc.       USB PnP Sound Device"
P: Phys=usb-musb-hdrc.1.auto-1/input3
S: Sysfs=/devices/ocp.3/47400000.usb/musb-hdrc.1.auto/usb1/1-1/1-1:1.3/input/input1
U: Uniq=
H: Handlers=kbd event1
B: PROP=0
B: EV=13
B: KEY=78 0 e0000 0 0 0
B: MSC=10

There's more…

The stem of this command is cat, which is one of the most popular commands to quickly read files without writing or modifying them:

$  cat file4

You can try it with any file to see how the screen output looks.

Device and system insight – get your version of Linux

You can find out which version and distribution of Linux you are running through several methods.

How to do it…

To find out your version of Linux, use the following command:

debian@beaglebone:~$ lsb_release -a

The screen output will look similar to this:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.6 (wheezy)
Release: 7.6
Codename: wheezy

The preceding command is a good complement to the more typical way we determine the actual kernel version, which is as follows:

$ uname -r
3.8.13-boneXX

Device and system insight – find out running processes – ps

In Linux, applications are referred to as processes, and each is given a unique ID number or PID. The ps (that is, process status) command provides information about the currently running tasks or processes. The output includes the PIDs.

How to do it…

This command takes a snapshot of your board. You can run the command unadulterated and with no options, as follows:

$ ps

But the output is a thin gruel:

debian@beaglebone:~$ ps
PID    TTY    TIME    CMD
1623    pts/0    00:00:00    bash
2035    pts/0    00:00:00    ps

So, it is often better to modify the output in order to get more insight into the running processes and display this information in a more orderly fashion, as in the following command:

$ ps aux

The following is the output:

How to do it…

The aux series of options complement the basic command by doing the following:

  • The a option shows the processes for all users. Historically, ps requires different syntax depending on our flavor of Linux or Unix. This option simplifies the method required to add options.
  • Using the u option tells the command to display the user or owner of the process in the output. The reason you want to do this is that there are processes typically running at the root level and other processes running at a non-root-user level. However, we frequently want to see all processes running regardless of user, so this option is important.
  • The x option ensures that processes that are not currently running in a terminal window—which form the majority of running processes—are also in the output that we want to see.

Device and system insight – find out the resources used by running processes – top and htop

This command takes it a bit further than the ps command as it dynamically updates not only the processes running but also the CPU resources used by the processes. The status is continuous and in real time.

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

How to do it…

Use the following command to find out about the USB devices connected:

debian@beaglebone:~$ lsusb
Bus 001 Device 002: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Note

Note that the first device here shows a connected USB audio dongle.

Device and system insight – get information about connected devices – cat/proc/bus/input/devices

Where lusb leaves off, this command lists more detailed information about any device connected to the BBB. Note the difference in output for the same USB audio device (C-media) shown in our preceding recipe using lsusb.

How to do it…

Use the following command to get information about connected devices:

debian@beaglebone:~$ cat /proc/bus/input/devices

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="tps65217_pwr_but"
P: Phys=
S: Sysfs=/devices/ocp.3/44e0b000.i2c/i2c-0/0-0024/input/input0
U: Uniq=
H: Handlers=kbd event0
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0003 Vendor=0d8c Product=013c Version=0100
N: Name="C-Media Electronics Inc.       USB PnP Sound Device"
P: Phys=usb-musb-hdrc.1.auto-1/input3
S: Sysfs=/devices/ocp.3/47400000.usb/musb-hdrc.1.auto/usb1/1-1/1-1:1.3/input/input1
U: Uniq=
H: Handlers=kbd event1
B: PROP=0
B: EV=13
B: KEY=78 0 e0000 0 0 0
B: MSC=10

There's more…

The stem of this command is cat, which is one of the most popular commands to quickly read files without writing or modifying them:

$  cat file4

You can try it with any file to see how the screen output looks.

Device and system insight – get your version of Linux

You can find out which version and distribution of Linux you are running through several methods.

How to do it…

To find out your version of Linux, use the following command:

debian@beaglebone:~$ lsb_release -a

The screen output will look similar to this:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.6 (wheezy)
Release: 7.6
Codename: wheezy

The preceding command is a good complement to the more typical way we determine the actual kernel version, which is as follows:

$ uname -r
3.8.13-boneXX

Device and system insight – find out running processes – ps

In Linux, applications are referred to as processes, and each is given a unique ID number or PID. The ps (that is, process status) command provides information about the currently running tasks or processes. The output includes the PIDs.

How to do it…

This command takes a snapshot of your board. You can run the command unadulterated and with no options, as follows:

$ ps

But the output is a thin gruel:

debian@beaglebone:~$ ps
PID    TTY    TIME    CMD
1623    pts/0    00:00:00    bash
2035    pts/0    00:00:00    ps

So, it is often better to modify the output in order to get more insight into the running processes and display this information in a more orderly fashion, as in the following command:

$ ps aux

The following is the output:

How to do it…

The aux series of options complement the basic command by doing the following:

  • The a option shows the processes for all users. Historically, ps requires different syntax depending on our flavor of Linux or Unix. This option simplifies the method required to add options.
  • Using the u option tells the command to display the user or owner of the process in the output. The reason you want to do this is that there are processes typically running at the root level and other processes running at a non-root-user level. However, we frequently want to see all processes running regardless of user, so this option is important.
  • The x option ensures that processes that are not currently running in a terminal window—which form the majority of running processes—are also in the output that we want to see.

Device and system insight – find out the resources used by running processes – top and htop

This command takes it a bit further than the ps command as it dynamically updates not only the processes running but also the CPU resources used by the processes. The status is continuous and in real time.

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

Device and system insight – get information about connected devices – cat/proc/bus/input/devices

Where lusb leaves off, this command lists more detailed information about any device connected to the BBB. Note the difference in output for the same USB audio device (C-media) shown in our preceding recipe using lsusb.

How to do it…

Use the following command to get information about connected devices:

debian@beaglebone:~$ cat /proc/bus/input/devices

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="tps65217_pwr_but"
P: Phys=
S: Sysfs=/devices/ocp.3/44e0b000.i2c/i2c-0/0-0024/input/input0
U: Uniq=
H: Handlers=kbd event0
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0003 Vendor=0d8c Product=013c Version=0100
N: Name="C-Media Electronics Inc.       USB PnP Sound Device"
P: Phys=usb-musb-hdrc.1.auto-1/input3
S: Sysfs=/devices/ocp.3/47400000.usb/musb-hdrc.1.auto/usb1/1-1/1-1:1.3/input/input1
U: Uniq=
H: Handlers=kbd event1
B: PROP=0
B: EV=13
B: KEY=78 0 e0000 0 0 0
B: MSC=10

There's more…

The stem of this command is cat, which is one of the most popular commands to quickly read files without writing or modifying them:

$  cat file4

You can try it with any file to see how the screen output looks.

Device and system insight – get your version of Linux

You can find out which version and distribution of Linux you are running through several methods.

How to do it…

To find out your version of Linux, use the following command:

debian@beaglebone:~$ lsb_release -a

The screen output will look similar to this:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.6 (wheezy)
Release: 7.6
Codename: wheezy

The preceding command is a good complement to the more typical way we determine the actual kernel version, which is as follows:

$ uname -r
3.8.13-boneXX

Device and system insight – find out running processes – ps

In Linux, applications are referred to as processes, and each is given a unique ID number or PID. The ps (that is, process status) command provides information about the currently running tasks or processes. The output includes the PIDs.

How to do it…

This command takes a snapshot of your board. You can run the command unadulterated and with no options, as follows:

$ ps

But the output is a thin gruel:

debian@beaglebone:~$ ps
PID    TTY    TIME    CMD
1623    pts/0    00:00:00    bash
2035    pts/0    00:00:00    ps

So, it is often better to modify the output in order to get more insight into the running processes and display this information in a more orderly fashion, as in the following command:

$ ps aux

The following is the output:

How to do it…

The aux series of options complement the basic command by doing the following:

  • The a option shows the processes for all users. Historically, ps requires different syntax depending on our flavor of Linux or Unix. This option simplifies the method required to add options.
  • Using the u option tells the command to display the user or owner of the process in the output. The reason you want to do this is that there are processes typically running at the root level and other processes running at a non-root-user level. However, we frequently want to see all processes running regardless of user, so this option is important.
  • The x option ensures that processes that are not currently running in a terminal window—which form the majority of running processes—are also in the output that we want to see.

Device and system insight – find out the resources used by running processes – top and htop

This command takes it a bit further than the ps command as it dynamically updates not only the processes running but also the CPU resources used by the processes. The status is continuous and in real time.

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

How to do it…

Use the following command to get information about connected devices:

debian@beaglebone:~$ cat /proc/bus/input/devices

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="tps65217_pwr_but"
P: Phys=
S: Sysfs=/devices/ocp.3/44e0b000.i2c/i2c-0/0-0024/input/input0
U: Uniq=
H: Handlers=kbd event0
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0003 Vendor=0d8c Product=013c Version=0100
N: Name="C-Media Electronics Inc.       USB PnP Sound Device"
P: Phys=usb-musb-hdrc.1.auto-1/input3
S: Sysfs=/devices/ocp.3/47400000.usb/musb-hdrc.1.auto/usb1/1-1/1-1:1.3/input/input1
U: Uniq=
H: Handlers=kbd event1
B: PROP=0
B: EV=13
B: KEY=78 0 e0000 0 0 0
B: MSC=10

There's more…

The stem of this command is cat, which is one of the most popular commands to quickly read files without writing or modifying them:

$  cat file4

You can try it with any file to see how the screen output looks.

Device and system insight – get your version of Linux

You can find out which version and distribution of Linux you are running through several methods.

How to do it…

To find out your version of Linux, use the following command:

debian@beaglebone:~$ lsb_release -a

The screen output will look similar to this:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.6 (wheezy)
Release: 7.6
Codename: wheezy

The preceding command is a good complement to the more typical way we determine the actual kernel version, which is as follows:

$ uname -r
3.8.13-boneXX

Device and system insight – find out running processes – ps

In Linux, applications are referred to as processes, and each is given a unique ID number or PID. The ps (that is, process status) command provides information about the currently running tasks or processes. The output includes the PIDs.

How to do it…

This command takes a snapshot of your board. You can run the command unadulterated and with no options, as follows:

$ ps

But the output is a thin gruel:

debian@beaglebone:~$ ps
PID    TTY    TIME    CMD
1623    pts/0    00:00:00    bash
2035    pts/0    00:00:00    ps

So, it is often better to modify the output in order to get more insight into the running processes and display this information in a more orderly fashion, as in the following command:

$ ps aux

The following is the output:

How to do it…

The aux series of options complement the basic command by doing the following:

  • The a option shows the processes for all users. Historically, ps requires different syntax depending on our flavor of Linux or Unix. This option simplifies the method required to add options.
  • Using the u option tells the command to display the user or owner of the process in the output. The reason you want to do this is that there are processes typically running at the root level and other processes running at a non-root-user level. However, we frequently want to see all processes running regardless of user, so this option is important.
  • The x option ensures that processes that are not currently running in a terminal window—which form the majority of running processes—are also in the output that we want to see.

Device and system insight – find out the resources used by running processes – top and htop

This command takes it a bit further than the ps command as it dynamically updates not only the processes running but also the CPU resources used by the processes. The status is continuous and in real time.

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

There's more…

The stem of this command is cat, which is one of the most popular commands to quickly read files without writing or modifying them:

$  cat file4

You can try it with any file to see how the screen output looks.

Device and system insight – get your version of Linux

You can find out which version and distribution of Linux you are running through several methods.

How to do it…

To find out your version of Linux, use the following command:

debian@beaglebone:~$ lsb_release -a

The screen output will look similar to this:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.6 (wheezy)
Release: 7.6
Codename: wheezy

The preceding command is a good complement to the more typical way we determine the actual kernel version, which is as follows:

$ uname -r
3.8.13-boneXX

Device and system insight – find out running processes – ps

In Linux, applications are referred to as processes, and each is given a unique ID number or PID. The ps (that is, process status) command provides information about the currently running tasks or processes. The output includes the PIDs.

How to do it…

This command takes a snapshot of your board. You can run the command unadulterated and with no options, as follows:

$ ps

But the output is a thin gruel:

debian@beaglebone:~$ ps
PID    TTY    TIME    CMD
1623    pts/0    00:00:00    bash
2035    pts/0    00:00:00    ps

So, it is often better to modify the output in order to get more insight into the running processes and display this information in a more orderly fashion, as in the following command:

$ ps aux

The following is the output:

How to do it…

The aux series of options complement the basic command by doing the following:

  • The a option shows the processes for all users. Historically, ps requires different syntax depending on our flavor of Linux or Unix. This option simplifies the method required to add options.
  • Using the u option tells the command to display the user or owner of the process in the output. The reason you want to do this is that there are processes typically running at the root level and other processes running at a non-root-user level. However, we frequently want to see all processes running regardless of user, so this option is important.
  • The x option ensures that processes that are not currently running in a terminal window—which form the majority of running processes—are also in the output that we want to see.

Device and system insight – find out the resources used by running processes – top and htop

This command takes it a bit further than the ps command as it dynamically updates not only the processes running but also the CPU resources used by the processes. The status is continuous and in real time.

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

Device and system insight – get your version of Linux

You can find out which version and distribution of Linux you are running through several methods.

How to do it…

To find out your version of Linux, use the following command:

debian@beaglebone:~$ lsb_release -a

The screen output will look similar to this:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.6 (wheezy)
Release: 7.6
Codename: wheezy

The preceding command is a good complement to the more typical way we determine the actual kernel version, which is as follows:

$ uname -r
3.8.13-boneXX

Device and system insight – find out running processes – ps

In Linux, applications are referred to as processes, and each is given a unique ID number or PID. The ps (that is, process status) command provides information about the currently running tasks or processes. The output includes the PIDs.

How to do it…

This command takes a snapshot of your board. You can run the command unadulterated and with no options, as follows:

$ ps

But the output is a thin gruel:

debian@beaglebone:~$ ps
PID    TTY    TIME    CMD
1623    pts/0    00:00:00    bash
2035    pts/0    00:00:00    ps

So, it is often better to modify the output in order to get more insight into the running processes and display this information in a more orderly fashion, as in the following command:

$ ps aux

The following is the output:

How to do it…

The aux series of options complement the basic command by doing the following:

  • The a option shows the processes for all users. Historically, ps requires different syntax depending on our flavor of Linux or Unix. This option simplifies the method required to add options.
  • Using the u option tells the command to display the user or owner of the process in the output. The reason you want to do this is that there are processes typically running at the root level and other processes running at a non-root-user level. However, we frequently want to see all processes running regardless of user, so this option is important.
  • The x option ensures that processes that are not currently running in a terminal window—which form the majority of running processes—are also in the output that we want to see.

Device and system insight – find out the resources used by running processes – top and htop

This command takes it a bit further than the ps command as it dynamically updates not only the processes running but also the CPU resources used by the processes. The status is continuous and in real time.

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

How to do it…

To find out your version of Linux, use the following command:

debian@beaglebone:~$ lsb_release -a

The screen output will look similar to this:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 7.6 (wheezy)
Release: 7.6
Codename: wheezy

The preceding command is a good complement to the more typical way we determine the actual kernel version, which is as follows:

$ uname -r
3.8.13-boneXX

Device and system insight – find out running processes – ps

In Linux, applications are referred to as processes, and each is given a unique ID number or PID. The ps (that is, process status) command provides information about the currently running tasks or processes. The output includes the PIDs.

How to do it…

This command takes a snapshot of your board. You can run the command unadulterated and with no options, as follows:

$ ps

But the output is a thin gruel:

debian@beaglebone:~$ ps
PID    TTY    TIME    CMD
1623    pts/0    00:00:00    bash
2035    pts/0    00:00:00    ps

So, it is often better to modify the output in order to get more insight into the running processes and display this information in a more orderly fashion, as in the following command:

$ ps aux

The following is the output:

How to do it…

The aux series of options complement the basic command by doing the following:

  • The a option shows the processes for all users. Historically, ps requires different syntax depending on our flavor of Linux or Unix. This option simplifies the method required to add options.
  • Using the u option tells the command to display the user or owner of the process in the output. The reason you want to do this is that there are processes typically running at the root level and other processes running at a non-root-user level. However, we frequently want to see all processes running regardless of user, so this option is important.
  • The x option ensures that processes that are not currently running in a terminal window—which form the majority of running processes—are also in the output that we want to see.

Device and system insight – find out the resources used by running processes – top and htop

This command takes it a bit further than the ps command as it dynamically updates not only the processes running but also the CPU resources used by the processes. The status is continuous and in real time.

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

Device and system insight – find out running processes – ps

In Linux, applications are referred to as processes, and each is given a unique ID number or PID. The ps (that is, process status) command provides information about the currently running tasks or processes. The output includes the PIDs.

How to do it…

This command takes a snapshot of your board. You can run the command unadulterated and with no options, as follows:

$ ps

But the output is a thin gruel:

debian@beaglebone:~$ ps
PID    TTY    TIME    CMD
1623    pts/0    00:00:00    bash
2035    pts/0    00:00:00    ps

So, it is often better to modify the output in order to get more insight into the running processes and display this information in a more orderly fashion, as in the following command:

$ ps aux

The following is the output:

How to do it…

The aux series of options complement the basic command by doing the following:

  • The a option shows the processes for all users. Historically, ps requires different syntax depending on our flavor of Linux or Unix. This option simplifies the method required to add options.
  • Using the u option tells the command to display the user or owner of the process in the output. The reason you want to do this is that there are processes typically running at the root level and other processes running at a non-root-user level. However, we frequently want to see all processes running regardless of user, so this option is important.
  • The x option ensures that processes that are not currently running in a terminal window—which form the majority of running processes—are also in the output that we want to see.

Device and system insight – find out the resources used by running processes – top and htop

This command takes it a bit further than the ps command as it dynamically updates not only the processes running but also the CPU resources used by the processes. The status is continuous and in real time.

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

How to do it…

This command takes a snapshot of your board. You can run the command unadulterated and with no options, as follows:

$ ps

But the output is a thin gruel:

debian@beaglebone:~$ ps
PID    TTY    TIME    CMD
1623    pts/0    00:00:00    bash
2035    pts/0    00:00:00    ps

So, it is often better to modify the output in order to get more insight into the running processes and display this information in a more orderly fashion, as in the following command:

$ ps aux

The following is the output:

How to do it…

The aux series of options complement the basic command by doing the following:

  • The a option shows the processes for all users. Historically, ps requires different syntax depending on our flavor of Linux or Unix. This option simplifies the method required to add options.
  • Using the u option tells the command to display the user or owner of the process in the output. The reason you want to do this is that there are processes typically running at the root level and other processes running at a non-root-user level. However, we frequently want to see all processes running regardless of user, so this option is important.
  • The x option ensures that processes that are not currently running in a terminal window—which form the majority of running processes—are also in the output that we want to see.

Device and system insight – find out the resources used by running processes – top and htop

This command takes it a bit further than the ps command as it dynamically updates not only the processes running but also the CPU resources used by the processes. The status is continuous and in real time.

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

Device and system insight – find out the resources used by running processes – top and htop

This command takes it a bit further than the ps command as it dynamically updates not only the processes running but also the CPU resources used by the processes. The status is continuous and in real time.

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

How to do it…

Use this command line:

$ top

The output is as follows:

How to do it…

The top command is adequate and one that you will hear most Linux users call upon, but there is now a much lovelier, easier way to read a package—htop. Let's install it and compare it to top with the following command:

$ sudo apt-get install htop

Now, the output will be as follows:

How to do it…

As you can see, it has an output that is much cleaner and easier to read.

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

Device and system insight – quitting processes – kill

The typical way to force quit—or kill—an application is to use the kill command and combine it with the process ID (PID), which you can derive from any of the preceding recipes to capture process information. Let's take a look at the basic method.

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

How to do it…

Use the following command to kill processes:

$ kill <process_id>

Here's a real example, which would force quit the top process shown in our prior recipe:

$ kill 4569

When you know the name of a process, you can use the pkill command, which operates on the process name instead, as follows:

$ pkill -9 gdb

Naturally, there is yet another way to skin the cat while killing a process. For example, you may discover that there are several processes associated with one application, and killing them one by one gets a bit slippery. Instead, use killall and kiss them goodbye:

$ killall chromium

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

Device and system insight – message reports – dmesg

As you already know, every time you boot up your device, a ton of messages quickly scrolls past on the screen. Unless you wear a blue leotard with a red cape and possess exceptionally fast eyes, you likely will not catch all these pearls of ASCII wisdom. Yet, these messages can be extremely useful at times, particularly when you need to troubleshoot a system problem.

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

How to do it…

The dmesg command is used as follows:

$ dmesg

Typically, the output looks similar to this:

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.8.13-bone47 (root@imx6q-wandboard-2gb-0) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Fri Apr 11 01:36:09 UTC 2014
[    0.000000] CPU: ARMv7 Processor [413fc082] revision 2 (ARMv7), cr=50c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] Machine: Generic AM33XX (Flattened Device Tree), model: TI AM335x BeagleBone
[    0.000000] Memory policy: ECC disabled, Data cache writeback
[    0.000000] On node 0 totalpages: 130816
[    0.000000] free_area_init_node: node 0, pgdat c0824280, node_mem_map c089f000

The list goes on and on as your machine is full of activity! So, limiting dmesg to output smaller chunks of information with a command option is primarily the course followed. In this case, we want to show the last ten events on the system, so we will use the following:

$ dmesg | tail

Alternatively, we can use the less option, which allows us to advance forward in the list more methodically, as follows:

$ dmesg | less

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a new directory – mkdir

This command helps you create a directory (folder) where you want to work or place files.

How to do it…

Use the following command to make a new directory:

debian@beaglebone:~$ mkdir test

Now is a good time to check your work with the ls command, as follows:

debian@beaglebone:~$ ls
Desktop  test

Modify content/data commands – remove a file or directory – rm

As the name implies, this command removes a file or directory that you designate.

How to do it…

Removing a file is very simple. You just need to type out the following:

$ rm file1

Adding the -i option is good practice, especially for beginners, as it prompts you to confirm the file's removal as follows:

$ rm -ir file1
rm: remove regular file `file1'?

To remove directories, there are principally two commands you can use. Firstly, when you have an empty directory, the command is similar to the following:

$ rmdir dir1

However, rmdir works only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. This option tells rm to remove a directory recursively, as in the following command:

$ rmdir -r dir1

Tip

Red Alert!

Obviously, rm -r can wreak havoc on your system and delete files and directories that you may actually need. To be cautious, the first few times you use this command, you might want to include the -i option. This way, each time you delete a directory and its contents, you will get a prompt before each file is deleted.

Modify content / data commands – download files – wget

Running this command gives you control over grabbing and downloading files from web servers via HTTP, HTTPS, and FTP. Unlike the experience of downloading a file in a web browser, wget is noninteractive. This means that you don't have to be logged on for the command to complete its task, which is potentially a great time-saver with large file downloads.

How to do it…

In order to download files, you can use a command such as the following:

$ wget http://

This will download the Major Tom page at www.hudsonwerks.com into your current directory and create a file named index.html. Next, perform the following steps:

  1. Check your work using the following command:
    $ ls
    Desktop    test    index.html
    
  2. Now, perform another wget; this time, download via ftp. Here, we will grab wget source code from the GNU site as follows:
    wget ftp://ftp.gnu.org/pub/gnu/wget/wget-1.15.tar.xz
    
  3. Check your work again, using the following command this time:
    $ ls
    Desktop    test    index.html    wget-1.15.tar.xz
    

There's more…

Where do we put the new tarballs, packages, and so on? It is good practice to create a directory called /home/username/Packages/. This is where you can put tarballs, their extracted files, compiled code, backups of replaced files, and installation scripts. For this, you can use the following command:

$ mkdir /home/username/Packages/
$ wget -P /home/debian/packages <download-url>

Modify content / data commands – open a tar file – tar [options]

Working with files in the .tar (tarball) format, a type of archived file, is common in Linux. The various options that come with the tar command will considerably ease your management of the files that are not archived.

How to do it…

Use the following command to open a tar file:

$ tar -zxvf file_name.tar.gz

Before actually running the command, let's understand a bit about the options:

  • -z: This is used to uncompress the resulting archive with the gzip command
  • -x: This is used to extract to disk from the archive
  • -v: This produces a verbose output, which means that it shows progress and file names while extracting files
  • -f document.tar.gz: This reads the archive from the file called document.tar.gz

Other file types include .xz, which is the actual file type we downloaded previously using wgetwget-1.15.tar.xz. So, here are the essential recipe steps:

  1. By default, files are extracted into your current directory. However, you can simultaneously extract your files and put them in a different directory with the -C option. Using the following command, we will extract files and put them in the /test directory:
    $ wget-1.15.tar.xz -C test
    
  2. Check your work by navigating to the new directory created and running the following command:
    $ cd /test/ wget-1.15
    

    You should see all the unarchived wget source files with their accompanying directories.

The flip side of unarchiving is archiving. Here's how you can create a tar file:

$ tar -cvf file.tar inputfile1 inputfile2

Replace inputfile1 and inputfile2 with the files and/or directories that you want to combine. You can use any name in the place of file.tar; you should keep the .tar extension, though.

There's more…

You may occasionally run into errors while opening tarballs (or other gzip files). If so, one troubleshooting tip is to ensure that the file is actually a zipped archive with the following command:

$ file filename

This will show the file type and size; if it's not an archive, you can't run the tar command on it.

Modify content / data commands – clean up files – autoclean

This command removes partial packages from the system.

How to do it…

Use the following command to clean up files:

$ sudo apt-get autoclean

Modify content / data commands – purge a package – apt-get purge, apt-get remove --purge

These commands completely get rid of packages and dependencies.

How to do it…

Use the following command to get rid of packages and dependencies:

$ sudo apt-get purge packagename

Alternatively, you can use the following command:

$ sudo apt-get remove  --purge packagename

Modify content/data commands – shutdown the system – [options]

It is a bad idea to pull the plug on your board to turn it off. Doing so can often lead to a corrupted SD card or mangled files. Instead, here's how you power down your board gracefully.

How to do it…

If you want to shut down the BBB and reboot it, the following command will be useful:

sudo shutdown -r now

However, if you simply want to shut down the system and power down completely, then either of the following two options will do the trick:

sudo shutdown -h now

You can alternatively use the following command:

sudo poweroff

See also

You will find some of the best one-page cheat sheet references for Linux commands. Print them out and post them in an honored place!

Device and system insight – shows disk space – df -h

This command outputs information on your board's available disk space and displays it in human readable format.

How to do it…

Use the following command to find out the disk space:

debian@beaglebone:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.2G  1.6G  5.4G  22% /
udev             10M     0   10M   0% /dev
tmpfs           100M  824K   99M   1% /run
/dev/mmcblk0p2  7.2G  1.6G  5.4G  22% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   71M   25M  75% /boot/uboot
/dev/mmcblk1p2  1.7G  1.7G     0 100% /media/rootfs
/dev/mmcblk1p1   96M   11M   86M  11% /media/boot

Device and system insight – explore network – Ifconfig

This lists all the network devices with network information. You will commonly find yourself using this command on the BBB when you need to troubleshoot a problem or set up network connections.

How to do it…

Use the following command to explore network configuration:

$ ifconfig

The output is as follows:

How to do it…

Modify content / data commands – make a ne