The Raspbian Command Line, Part 2

Last time we learned how to explore our Raspberry Pi. This time we’re going to begin looking at how to change things.

Taking a peek

The first thing we’re going to do is learn how to see what’s inside all of those files we’ve been seeing. A good place to start exploring is in the /var/log/apt directory where our Pi keeps a record of all the files we have downloaded with apt. Let’s open our terminals and type nano history.log if we’re already in the /var/log/apt directory or nano /var/log/apt/history.log if we’re anywhere else in the file structure.

There’s not a lot we can do with this file but it’s interesting to look around. Type in cd .. to go back to the /var/log directory and explore some of the files there with the nano filename command. When we are having problems getting something to work in the future, this will be a good place to look when we’re trying to figure out why it’s not working.

Let’s take it up one notch. Go to the /boot directory by typing in cd /boot. Next let’s take a look at the config.txt file. Enter nano config.txt into the command line. This file controls all of the basic configuration settings for our Raspberry Pi. Many of the programs we have installed, or may install later, have a configuration file. Some programs require specialized configurations of these files that can’t be done automatically.

Before we try to modify this file to play with its settings, we should make a backup, just in case we want to return things to how they were. To do that, we need to know the cp (copy) command. We’re not going to get into every detail of the cp command here, but to get an idea of how versatile it is type cp --help into the terminal to get a long list of the different ways to use it.

Let’s start by making a backup of the config.txt file we were just looking at. Type sudo cp config.txt config.txt.BAK. What just happened? The cp command needs at least two arguments, the file to be copied (in this case config.txt) and where the file is to be copied to. The interesting part is that we have to specify the name of copied file as well (config.txt.BAK). Keep in mind that if the new file doesn’t exist, cp will create it. If it already exits, cp will overwrite the old file.

We can also use the cp command to copy our file into another directory. Say for example we’ve been working on a new index.html file for our website in our home directory and we want to move it to the directory of our web server. In this case we type sudo cp ~/index.html /var/www/html/. We’re using the ~ as a shortcut for our home directory, index.html is the file we’re moving, and /var/www/html/ is the destination directory we are copying to.

There’s a lot more that the cp command is capable of but we’re going to leave off here and look at two more commands.

Move and Remove

Sometimes we don’t want to copy a file we just want to move it. On our Raspberry Pi it’s just as easy as copying. To move files we are going to use the mv command. It takes the same basic form as the cp command but we can also use it to rename files. Say we’ve been working on a new home page but we called it testPage.html, now we want to rename it index.html. To accomplish this we would type mv testPage.html index.html. If we then wanted to move that index.html file to our web server directory (without leaving a copy in our home directory) we would type sudo index.html /var/www/html/ (we need to use the sudo this time because the directory /var/www/html/ is assigned to the root user).

Finally, it’s time to remove files with the rm command. This command is pretty straight forward. Let’s say we copied the file index.html from our home directory to our web server directory and now we want to get rid of the file in our home directory, type rm index.html. Super simple.

Suppose we’ve made a directory where we’ve been keeping our website stuff while we tested it out, but now we don’t need it anymore. We have two options depending on whether our directory has files in it or not. If our directory is empty we need to use the -d flag: rm -d emptyDirectory/. If we still have files in the directory and we want to remove them and the directory we need to use the -r flag: rm -r directory/. Remember, if we’re told that we don’t have permission to run a specific command, just append sudo to the beginning.

We’ve barely scratched the surface of what we can do with these four commands. There’s more ways to use them than I can reasonably list here, but I encourage you to look at the --help documentation for the commands and look at other tutorials online to see how much these seemingly simple commands can do.