Home / Learn / Code for makers

Linux command line basics for Raspberry Pi users

A Raspberry Pi terminal window with common commands: listing files, updating software and scanning the I²C bus
Beginner

A Raspberry Pi has a desktop, but sooner or later a guide will tell you to "open a terminal and run this". The command line is faster for many jobs, works over a network with no screen attached, and is how most electronics and AI tools are installed. These are the commands worth knowing, and they work the same on almost any Linux computer and, mostly, on a Mac.

Finding your way around

The terminal always has a current folder, called the working directory. Your home folder is written ~.

CommandWhat it does
pwdPrint the current folder
lsList what's in it. ls -l adds sizes and dates, ls -a shows hidden files
cd projectsGo into the projects folder
cd ..Go up one level
cd ~ or just cdGo back to your home folder

Press Tab to complete file and folder names, and the up arrow to bring back previous commands. Names are case-sensitive: Projects and projects are different folders.

Working with files

CommandWhat it does
mkdir sensorsMake a folder
cp a.py b.pyCopy a file. Add -r to copy a folder
mv a.py code/Move a file. Also used to rename: mv old.py new.py
rm a.pyDelete a file. Add -r for a folder
cat log.txtPrint a file
less log.txtScroll through a long file. Press q to quit
tail -f log.txtWatch a file as new lines are added. Ctrl+C to stop
nano notes.txtEdit a file. Ctrl+O saves, Ctrl+X exits
rm has no recycle bin. Deleted files are gone. Be especially careful with rm -r and with * wildcards, and never run a delete command you don't understand.

Running programs

python3 blink.py          # run a Python script
python3 -m venv .venv     # make a virtual environment for a project
source .venv/bin/activate # use it (type "deactivate" to leave)

Press Ctrl+C to stop a running program. On Raspberry Pi OS Bookworm and newer, pip install outside a virtual environment is blocked to protect the system's own Python. Either install the Debian package, such as sudo apt install python3-gpiozero, or use a virtual environment as above.

Installing software with apt

sudo apt update           # fetch the latest package lists
sudo apt full-upgrade     # install updates
sudo apt install i2c-tools

sudo runs one command with administrator rights. It's needed to install software and change system settings, but don't use it for everyday commands. Running your own scripts with sudo can leave files you can't edit later.

Permissions

ls -l shows permissions like -rwxr-xr--: read, write and execute for the owner, the group and everyone else. Two you'll use:

chmod +x start.sh         # make a script executable, then run it with ./start.sh
groups                    # list your groups. Hardware access often needs gpio, i2c or dialout

Useful for electronics

i2cdetect -y 1            # scan I2C bus 1 for devices (from i2c-tools)
ls /dev/tty*              # find serial ports, such as /dev/ttyACM0 for a Pico
dmesg | tail              # see recent hardware messages, e.g. after plugging in USB
vcgencmd measure_temp     # Raspberry Pi CPU temperature
df -h                     # free disk space
free -h                   # free memory

Enable I²C, SPI and SSH first with sudo raspi-config, under Interface Options.

Connecting from another computer

ssh pi@raspberrypi.local  # replace with your username and the Pi's name or IP address

SSH gives you the Pi's terminal from your laptop's, so the Pi can sit in a project box with no screen or keyboard. Use a strong password, or better, SSH keys, especially if the Pi is reachable from the internet.

Shutting down safely

Run sudo shutdown now and wait for the green activity light to stop flashing before you remove the power. Pulling the plug while the Pi is writing can corrupt its SD card. See our Raspberry Pi boards page for other things that catch people out.

Sources

The main references behind this guide. Numbers we worked out ourselves, such as the code outputs, come from running the code.