How to get to grips with your Raspberry Pi's command line interface

The first wildcard we'll use is *. This matches any string of zero or more characters. In its most basic usage, it'll match every file in the directory. Try running:

ls *

This isn't particularly useful, but we can put it in the middle of other characters. What do you think *.txt will match? Have a think, then run ls *.txt to see if you are right. How about one*? Again, run ls one* to see if you were correct.

The wildcards can be used with any command line programs. They're particularly useful for sorting files. To copy all the .txt files into a new directory, you could run:

mkdir text-files
cp *.txt text-files

We can then check they made it there correctly with:

ls text-files/

Wildcard: ?

The second wildcard we'll look at is ?. This matches any single character. What do you think: ls ??? will match? Have a guess, then run it to see if you're right.

We can also create our own wildcards to match just the characters we want. [abc] will match just a lower-case A, B and C. What do you think ls [ot]* will match? Now try ls [!ot]* What difference did the exclamation mark make? It should have returned everything that didn't start with a lower-case letter O or T.

The commands we've looked at so far have all sent their output to be displayed in the terminal. Most of the time this is what we want, but sometimes it's useful to do other things with it.

In Linux, you can do two other things: send it to a file, or send it to another program. To send it to a file, use the character followed by the filename. Run:

ls > files
cat files

and you should see that it creates a new file called files, which contains the output of ls.

The second option, sending it to another program, is another of the really powerful features of the Linux command line, since it allows you to chain a series of commands together to make one super command.

There are a lot of commands that work with text that are designed to be used in this way. They're beyond the scope of this tutorial, but as you continue to use the command line, you'll come across them and start to see how they can be linked together.

We'll take a look at a simple example. If you run find / (don't do it just yet!) it will list every file on the system. This will produce a reel of filenames that will quickly go off the screen. However, rather than direct it to the screen, we can send it to another command that makes the output easier to read. We can use the less command that we looked at earlier for this. Run:

find / | less

Take it further

We've only been able to touch on the basics of using the command line, but you should have enough now to get started, and hopefully you're starting to see just how powerful the command line interface is once you get to know it.

If you want to know more (and you should!) there are loads of resources around both in print and online. www.linuxcommand.org is a great place to start. Their book (The Linux Command Line) is avaialble from bookshops, or for free online.

sudo

When using the Raspberry Pi for normal use, you can work with files in your home directory (eg, /home/pi). You will also be able to view most other files on the system, but you won't be able to change them. You also won't be able to install software.

TOPICS