Skip to content

Essential Linux Commands for Raspberry Pi

Learn the terminal by working on a few files you can inspect, not by running a maintenance script you do not yet understand. This lesson builds a small practice directory and explains each command's effect.

Before you start

You need a working login on Raspberry Pi OS and no extra hardware. Run the commands in the Pi's terminal: either a local terminal window or an established SSH session. If you opened a terminal on another computer but have not used SSH, those commands act on that other computer.

You will create files in a new folder, not change system settings. Complete first-login checks if you are unsure which machine you are using.

1. Understand the prompt and your location

A prompt might look like this:

your-user@pi-lab:~ $

Do not type the prompt itself. The account and hostname depend on your installation; there is no required pi username. ~ means your home directory. A prompt can be customised, so use commands rather than its appearance to confirm your identity.

1
2
3
whoami
hostname
pwd

pwd prints the current directory. A relative path such as notes.txt refers to that directory. An absolute path such as /etc/os-release starts at the filesystem root. / is a directory, not the administrator account called root.

2. Create a fresh practice directory

This block creates a uniquely named directory in your home and enters it:

1
2
3
practice_dir=$(mktemp -d "$HOME/pi-terminal-practice.XXXXXX")
cd "$practice_dir"
pwd

The assignment stores the directory name in practice_dir. Quotes keep the path together if it contains spaces. mktemp -d avoids overwriting an earlier exercise. Keep using this terminal: the variable does not automatically exist in a new session.

Check: pwd ends with pi-terminal-practice. followed by a generated suffix. If creation or cd fails, stop rather than continuing in an unknown directory.

3. Create, read and edit a file

1
2
3
4
printf 'Raspberry Pi notes\nLED needs a resistor\n' > notes.txt
cat notes.txt
wc -l notes.txt
ls -l notes.txt

printf writes the text; > redirects it into a file and replaces that file's previous contents. Here it is safe because you are creating a fresh exercise file. cat displays it and wc -l reports 2 notes.txt.

Open it in the editor:

nano notes.txt

Add a third line, such as Check the wiring before power. In nano, press Ctrl+O, Enter to save, then Ctrl+X to exit. If nano is unavailable, skip the editor exercise or install it intentionally later; do not switch to editing a system file.

Run cat notes.txt again. Check: the saved text appears. Compilers and other tools read the saved file, not unsaved editor contents.

4. Copy and compare without losing the original

1
2
3
4
5
mkdir copies
cp -i notes.txt copies/notes.txt
cmp notes.txt copies/notes.txt
mv -i copies/notes.txt copies/saved-notes.txt
ls -l copies

mkdir creates a directory. cp copies the file; mv renames or moves it. The -i option asks before overwriting an existing destination. cmp prints nothing when the files match.

A copied file on the same card is useful for practice but not protection against losing the card. Learn separate-device backups in Storage Management.

5. Search and combine commands

1
2
3
grep -n 'LED' notes.txt
find . -maxdepth 2 -type f
printf 'button\nled\nbutton\n' | sort | uniq -c

grep -n finds matching text with line numbers. find . searches the current directory and the depth limit bounds the exercise. The pipe | sends output into the next command: sort groups identical lines so uniq -c can count them.

Check: grep shows the LED line, find shows the exercise files, and the final command counts two button lines and one led line.

grep returns exit status 1 when there is no match, not necessarily because the file is broken. To inspect a command's status, immediately run echo $?. Zero usually means success, but each tool documents its own statuses.

6. Understand deletion before using it

Terminal deletion normally does not move files into the desktop Trash. For this lesson, delete only the copy whose name you have just checked:

1
2
3
ls -l copies/saved-notes.txt
rm -i copies/saved-notes.txt
rmdir copies

Answer y only if the displayed path is the exercise copy. rmdir refuses a nonempty directory, which is a useful safety check.

Do not add recursive deletion, wildcards or sudo to make a failed removal work. Keep notes.txt and the practice directory for later lessons. Confirm with ls -la.

7. Learn permissions using a harmless script

Use nano status.sh in the practice directory and save this:

1
2
3
4
#!/bin/sh
printf 'Running on: '
hostname
df -h /

Then run:

1
2
3
4
sh status.sh
chmod u+x status.sh
./status.sh
ls -l status.sh

sh asks the shell to read the file. chmod u+x adds permission for the owner to execute it, and ./ means the executable in the current directory. You should see the hostname and root filesystem usage both times.

There is no need to run this script as root. Continue with the permissions lesson to understand file owner, group and directory access.

8. Inspect the Pi without tuning it

1
2
3
4
5
6
cat /etc/os-release
uname -r
free -h
df -h /
uptime
systemctl --failed --no-pager
Result How to read it
OS and kernel Identify the environment when reporting a problem
free -h The available column estimates memory available for applications; cached memory is not automatically a problem
df -h / Space in the mounted root filesystem, not total capacity of every disk
uptime Time running and load averages, not CPU temperature
Failed units Investigate the named service and its logs; do not disable it just to remove the warning

Run top to observe processes; press q to leave it. Use Ctrl+C to interrupt a foreground command such as ping or a program you launched. Neither key combination is a substitute for powering down the computer.

For an installed service, separate current state from startup policy:

1
2
3
systemctl is-active ssh
systemctl is-enabled ssh
journalctl -b -u ssh -n 20 --no-pager

Log access depends on your account permissions. If access is denied, understand that restriction before using sudo. Do not restart SSH as a practice exercise while it is your only connection.

9. Understand package changes and sudo

sudo runs an authorised command with administrator privileges. It is not a general repair for errors. A password prompt typically requests your account password, even after an SSH-key login. Password characters are not displayed.

On Raspberry Pi OS, routine updates within the installed release use:

sudo apt update
sudo apt full-upgrade

apt update refreshes package indexes; it does not install all updates. full-upgrade changes installed packages and can propose removals to resolve dependencies. Read its summary and proceed only if those changes make sense. Stop on errors; do not blindly continue to reboot.

These commands are not part of the file exercise and were not executed to validate it. Do not add -y, automatic deletion or repository changes simply to make the sequence unattended. A major OS-release migration is a separate task. See the official update guidance.

If a lesson requires a package such as build-essential, install it when you reach that lesson. A first terminal session does not require every development or monitoring tool.

Troubleshooting

Problem What to check
No such file or directory Use pwd and ls; check spelling and case
Permission denied File owner, mode and directory access; do not immediately use sudo
command not found Typo or missing package; command -v helps check availability
Program does not reflect edits Save the file, then rebuild if it is compiled
Editor or command seems stuck Read the on-screen prompt; Ctrl+C interrupts many foreground commands
practice_dir is empty in a new session Find the previously printed path or create a new exercise; variables are session-local
Disk is full Inspect usage first; do not recursively delete system logs or arbitrary directories

For tool-specific help, use man cp or cp --help. Quit a manual page with q. The GNU Coreutils manual documents the file utilities used here.

Check your understanding and continue

Without looking at a command list, can you create a file, locate it, copy it, compare the copy and explain what > would overwrite? Can you tell whether a command runs on the Pi or the client computer?

The noninteractive practice steps and observation commands are validated separately from interactive nano use and administrative package changes. They do not prove a new OS installation was performed.

Next, use System Settings to learn a controlled setting change, then Networking. Keep Permissions and Storage as the next foundational lessons.

Donate