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

This is because Linux has a permissions system that prevents ordinary users from changing system-wide settings. This is great for preventing you from accidentally breaking your settings. However, there are obviously times when you need to do this.

You can use /sudo to run a command as the super user (sometimes called root), which can do pretty much anything on the system. To use it, prefix the command with sudo. For example:

sudo apt-get install synaptic

will install the package synaptic and make it available to all users.

Linux Format cut-out-and-keep command line reference

cd Change directory. eg, cd movies moves to the movies folder. cd moves to your home directory, cd / moves to the root directory, and cd .. moves back one directory.

ls List files. By itself, it lists the files in the current directory. ls movies lists the files in the directory movies. ls -a lists all files (including hidden ones), and ls -l lists more information about each file.

cp Copy files. cp orig-file new-file copies orig-file to new-file.

wget Downloads a file from the internet. To download the Google home page to the current directory, use wget www.google.com.

df -h Displays the amount of space left on the device.

pwd Displays the current directory.

Finding files

find useful flags include: -mtime find files modified in the last days. could be, for example, 2 (exactly two days ago), -2 (less than two days ago) or +2 (more than two days ago). -name find files called .

-iname matches files called but not case sensitive.

-writable finds files that are writable. There are many more options. See the man page for a detailed list. For example, find / -mtime -2 -writable finds all files on the filesystem that were changed less than two days ago and are writable by the current user.

Remote working

ssh Log in to a remote computer using Secure SHell (SSH protocol). ssh pi@192.168.1.2 will log in as user pi on the computer at the IP address 192.168.1.2. Note, this will only work if the remote computer has an SSH server running.

scp Secure CoPy. scp file pi@192.168.1.2 :/home/pi will copy file to the directory home/pi on the machine with 192.168.1.2. scp pi@192.168.1.2:/home/pi/file. will copy /home/pi/file from the machine 192.168.1.2 to the current directory. Note, this will only work if the remote machine has an SCP server running.

Wildcards

* Matches any string of characters, or no characters.

? Matches any single character.

[abc] Matches a, b or c.

[!abc] Matches any character except a, b or c.

[A-Z] Matches any character in the range A–Z (ie, any upper-case letter).

[A-z] Matches any character in the rance A–z (ie, any upper- or lower-case letter).

[one, two] Matches the words one and two.

Information about the computer

top Displays the programs that are currently using the most CPU time and memory.

uname Displays information about the kernel. uname -m will output the architecture it's running on.

lscpu Lists information about the CPU.

dmesg Displays the kernel messages (can be useful for finding problems with hardware).

Text files

head Displays the first ten lines of a text file. Change ten to any other number with the -n flag. eg, dmesg | head -n 15 displays the first 15 lines of the kernel message log.

tail Displays the last ten lines of a text file. Can use the -n flag like head. Can also keep track of a file as it changes with the -f (follow) flag. eg, tail -n15 -f /var/log/syslog will display the final fifteen lines of the system log file, and continue to do so as it changes.

less Allows you to scroll through a text file.

cat Dumps the contents of a text file to the terminal.

nano A user-friendly command line text editor (Ctrl+X exits and gives you the option to save changes). Special keys Ctrl+C Kills whatever program is running in the terminal. Ctrl+D Sends the end-of-file character to whatever program is running in the terminal. Ctrl+Shift+C Copies selected text to the clipboard. Ctrl+Shift+V Pastes text from the clipboard.

Installing software

tar zxvf file.tar.gz
tar xjf file.tar.bz

./configure When you unzip a program's source code, it will usually create a new directory with the program in it. cd into that directory and run ./configure. This will check that your system has everything it needs to compile the software.

make This will compile the software.

make install (needs sudo) This will move the newly compiled software into the appropriate place in your system so you can run it like a normal command.

apt-get This can be used to install and remove software. For example, sudo apt-get install iceweasel will install the package iceweasel (a rebranded version of Firefox). sudo apt-get purge iceweasel will remove the package.

apt-get update will grab an up-to-date list of packages from the repository (a good idea before doing anything).

apt-get upgrade will upgrade all packages that have a newer version in the repository.

apt-cache search will search the repository for all packages relating to keyword.