Handy Linux Commands for Your Raspberry Pi
Linux has a lot of commands available for you to use on your Raspberry Pi, and they're not always obvious. Here is a selection of Linux commands you might find useful.
Set the time and date with Linux commands
Say you have just booted up your Raspberry Pi and not bothered to plug in the Ethernet, so the clock is all screwed up. You can set the time and date with
You need to have root privileges to do this. For example, if it's the 9th of August, 2013, at quarter past four in the afternoon, type
sudo date --set="9 AUG 2013 16:15:00"
Run two games at once on your Raspberry Pi
Say you have two games attached to your GPIO port. This is fine because they both use different pins, so there is no GPIO pin resource clash. However, say you want to run both of them on the Raspberry Pi at the same time. How can you do this without swapping the program that's running?
Linux is multitasking, so you can do this simply in two different ways.
The first is to open up two copies of the LXTerminal application. Just double-click the desktop icon twice. Drag the windows so they don't overlap and resize them if needed. Then click in one window and, using the change directory cd command, navigate to the folder that contains the Blastoff program. Type
sudo python blastOff.py
Then click in the other terminal window and navigate to the folder that contains the Copycat game. Type
sudo python copyCat.py
They both run and you can see the printout of each in a separate window. The sounds for both are mixed, so maybe you'll want to demonstrate them one at a time.
You can do the same thing without going into to the desktop by having multiple command line sessions. By pressing keys Alt+F1 through Alt + F6, you get six virtual console screens. By using these keys, you can switch between each one. You can log on as the same or a different user and run the two games at the same time.
The disadvantage of this is method as opposed to the desktop method is that you can only see one game’s printout at a time.
Deal with errors with Linux commands
Every Linux command has an input, an output, and an error log. Most of the time, you get your output and errors mixed together, but you can divert them into separate streams if you want. You can use the 1> command to divert the output into one file and the 2> to divert the errors to another. To find a list of Python-type files, type
find / -name *.py 1> pythonList 2> pythonErrors
Note that here you do not prefix the command with sudo, so there will be some access permission issues.
After a time, the prompt returns. To see the results, type
to catalog the file or print it out, and see the list and the errors.
The errors are mainly a list of access-denied messages generated during the search. Try this again with
sudo find / -name *.py 1> pythonList 2> pythonErrors
and you will find that the errors file is empty.
You should tidy things up by deleting these files. Type
If you don't want to actually store any information in a file, you can use a null file. This in effect directly pours the output stream down the drain as it's generated. To use this, specify /dev/null as the filename. That way, if you're not interested in the errors and just the list, you don't get the two in the same file and you don’t have to delete a file you don’t want.
Use the pipe symbol to direct two outputs into different files
You can direct the two outputs of a command into different files. You can do exactly the same with the input of a command. You can direct the output of one Linux command to the input of another.
This is done with the | symbol, which is called the bar or pipe symbol. It is often found at the outer reaches of the keyboard. On a Windows keyboard, it might be above the Windows key or between the Ctrl and Alt keys. On a Mac, it is often next to the Return key.
Take the command to list all the files (ls) and pipe the output into the word count. In this case, you only want to count the number of lines because that will be equal to the number files. Type the command
ls | wc -l
This returns the number of files and folders/directories in the current directory.
Find stuff in files with the grep command
Suppose you have written a function definition and you know what it is called. Unfortunately, you do not remember what file it was in. In that case, the powerful grep command can help you. Basically, it searches for a pattern or sequence of words you give it, and the command prints out the whole line and file that match this.
For example, to find out all the programs with a main function in them, navigate to the folder you want to search and type
grep "main" *.py
This gives you a list of all the lines in all the Python files containing the word main. To put this in context, you can print out any number of lines before the match and any number of lines after the match. Use the -B and -A options. Say you want the line before the match and two after it. Type
grep -B 1 -A 2 "main" *.py
By not using the wild card character (*), you can restrict the number of files that grep searches down to just a single file if need be. This is also very useful for finding stuff in other people's code. Remember, you can always direct the output into a file.