Learn / Raspberry Pi / Setup and first steps

Linux Command Line for Raspberry Pi

ReviewedBeginnerFreeAbout 6 min
Pi 4
In short

Linux Command Line for Raspberry Pi: the short answer

Most Raspberry Pi work happens in a terminal, either on the Pi or over SSH. The prompt shows your user name, the Pi's name and the current folder; ~ means your home folder. Example: A full card is a common cause of strange failures. Check df -h whenever a Pi misbehaves.

Level
Beginner
Time
About 6 min
Topic
Raspberry Pi
You will learn to

Move around files, edit, install software and check the system from the terminal, using the twenty commands you will use every day.

Before this: Setting Up a Raspberry Pi

Most Raspberry Pi work happens in a terminal, either on the Pi or over SSH. The prompt shows your user name, the Pi's name and the current folder; ~ means your home folder. Navigate with pwd (where am I?), ls -l (list files), cd folder (go into a folder), cd .. (go up). Make folders with mkdir, copy with cp, move or rename with mv, and delete with rm, which does not use a recycle bin, so double-check before pressing Enter.

Edit text files with nano filename: type, then press Ctrl+O to save and Ctrl+X to exit. View a file with cat or less, and follow a growing log with tail -f. Commands that change the system need sudo, which runs them as the administrator: sudo apt update refreshes the list of software and sudo apt install package installs one. Do not run everything with sudo; normal projects should run as your own user.

A few commands tell you about the system. df -h shows free space on the card, free -h shows memory, htop shows running programs and CPU use (press q to quit), and vcgencmd measure_temp shows the chip's temperature. hostname -I prints the Pi's IP address. Press the Tab key to complete file names and the up arrow to repeat earlier commands, and add --help after any command to see its options. Ctrl+C stops a running program.

How the file system is arranged

Linux has one tree that starts at /. Your own files live in /home/yourname, which ~ stands for. System settings are in /etc, programs in /usr and /bin, logs in /var/log, and attached drives under /media or /mnt. A path that begins with / is absolute and works from anywhere; one that does not is relative to where you are now.

Names are case sensitive: Notes.txt and notes.txt are different files. Avoid spaces in names, which need quoting, and prefer underscores or hyphens.

Permissions in one minute

ls -l shows letters such as -rwxr-xr--. The first character is the type, then three sets for the owner, the group and everyone else, each with r (read), w (write) and x (run). A script will not run until it has the x bit: chmod +x script.py. When a command says "Permission denied", look at who owns the file with ls -l before reaching for sudo.

sudo gives full control, and with it the power to break the system. Use it for installing and for changing system files, not for running your own programs.

Pipes and searching

The | symbol sends the output of one command into another. ls | wc -l counts files; dmesg | tail shows the latest kernel messages; ps aux | grep python finds your running Python programs. grep searches text: grep -i error /var/log/syslog finds lines mentioning error, ignoring case.

To save a command's output, use > to write to a file (replacing it) or >> to add to the end. Be careful with >, as it silently overwrites.

Keeping a program running

A program started over SSH stops when you disconnect. To keep it going, use tmux or screen, or add & and nohup, or better, make it a service that starts at boot with systemd. A short service file with ExecStart pointing to your script, then sudo systemctl enable --now yourservice, restarts it after a crash and on every power-up.

Read its output with journalctl -u yourservice -f. Logs are the first place to look when a project that worked yesterday fails today.

Common mistakes
  • Running rm -rf with a variable that might be empty.
  • Using sudo for everything, then finding files owned by root that your own user cannot edit.
  • Editing a system file without a backup copy (cp file file.bak).
Worked example

The Pi is slow and a script fails with 'No space left on device'

  1. df -h shows the root file system at 100 % used.
  2. du -sh ~/* shows a folder of camera photos taking 12 GB.
  3. Move the photos to a laptop with scp or delete old ones with rm.
  4. df -h now shows 60 % used, and the script runs again.

Result. A full card is a common cause of strange failures. Check df -h whenever a Pi misbehaves.

Try it

Over SSH, create a folder ~/projects/test, write a one-line Python script in it with nano, run it with python3, then list the folder with ls -l. Check the free space, memory and temperature, and write down the IP address.

Check yourself

3 quick questions

  1. Which command shows where you are in the file system?

  2. Why be careful with rm?

  3. How do you save and exit in nano?

Common questions

What will I learn in "Linux Command Line for Raspberry Pi"?

Move around files, edit, install software and check the system from the terminal, using the twenty commands you will use every day.

How do I the Pi is slow and a script fails with 'No space left on device'?

df -h shows the root file system at 100 % used. du -sh ~/* shows a folder of camera photos taking 12 GB. Move the photos to a laptop with scp or delete old ones with rm. df -h now shows 60 % used, and the script runs again. A full card is a common cause of strange failures. Check df -h whenever a Pi misbehaves.

Which command shows where you are in the file system?

pwd. pwd prints the working directory.

Why be careful with rm?

Deleted files do not go to a recycle bin. rm deletes permanently.

How do you save and exit in nano?

Ctrl+O then Ctrl+X. Ctrl+O writes the file, Ctrl+X exits.