Building more complex commands using Raspberry Pi terminal

Probably this would be unwise to go through a long list of available Unix commands. It is quite long, and there is no reason to point out each of them here. You can take a look at some basic ones in the following list.

It is more important to learn how to use them and get the desired result by building more complex commands. Commands can also be combined into a single line using piping. In this case, the output of one command becomes the input of the next one, and so on. Let’s go with a few examples.

We all know that Raspberry Pi comes with Python installed. So we should expect to find lots of .py files here:

sudo find / -name *.py

this throws us a large list of file names:

Finding and displaying files that way is useless. Viewing is even more painful. Let’s say we simply want to count all .py files. For this, we use the same command, but instead of throwing the list to the terminal, we feed it to another command that does the counting of lines (we get every file in a new line):

sudo find / -name *.py | wc -l

And in the terminal, we get the nice number 4485. so we already get more informative output. We can do more interesting tricks here. Let’s say we want to count .py programs that contain the word “game.” Again we need to use the pipe “|” character to combine several commands.

sudo find / -name *.py | grep “game” | wc -l

What we did here is that with the find command, we found all file names. Then we feed this list to grep command, which searches for the word “game” in the list, and then we count those words with wc -l.

We are already doing complex tasks here. Imagine the C or Python code you would write to achieve this.

Sometimes you need to create a sequence of several programs that would run one after another. This is practically called scripting. For instance, we can make a nice display of what we did:

echo “Counting word “game” in .py files”; sudo find / -name *.py | grep “game” | wc -l; echo “End”

If you need to output the result to file, just wrap all lines with brackets “( )” and use the “>” symbol indicating that results should be sent to the file:

(echo “Counting word “game” in .py files”; sudo find / -name *.py | grep “game” | wc -l; echo “End”) > gamecount

this writes results to file. In order to view its contents you can type

less gamecount

In the meantime, you will notice that your commands get longer and complex. Or you get tired of typing the same commands over and over again. This is where shell scripts come in handy. Scripts are nothing more than a set of commands to perform complex tasks.

Let’s build a simple script that would take care of upgrading the system. First of all, let’s go to our home directory

cd ~pi

Then lets create new directory myscripts and go to it:

mkdir myscripts; cd myscripts

Then we can create a file with name Use nano editor to edit it.

Sudo nano

then we should modify the script file to be executable:

sudo chmod +x /home/pi/myscripts/

and then we can simply run it by typing

sudo ./

You can also include a reboot command in the script to make upgrades to actions with a simple command:

sudo shutdown -r 1

It restarts Raspberry Pi in 1 minute.

Scripting is the main tool of Linux enthusiasts. This is where whole administration, customization are automated. Scripts can have variables, conditions, looks, and more. This source might give some clue about this.

Leave a Reply