Essential Linux Commands for Raspberry Pi¶
The Raspberry Pi runs on Linux-based operating systems, primarily Raspberry Pi OS (formerly Raspbian). Understanding essential Linux commands will help you navigate, manage files, install software, and troubleshoot your Raspberry Pi effectively. This guide covers the most important commands you'll need for daily operations.
Getting Started with the Terminal¶
When you first open a terminal on Raspberry Pi OS, you'll see a command prompt that looks like this:
This tells you:
pi— your usernameraspberrypi— the hostname of your machine~— your current directory (~means your home directory,/home/pi)$— you're a regular user (root users see#instead)
Navigation Commands¶
| Command | Description | Example |
|---|---|---|
pwd |
Print working directory - shows your current location | pwd → /home/pi |
ls |
List files and directories in current location | ls |
ls -l |
List files with detailed information (permissions, size, date) | ls -l |
ls -a |
List all files including hidden ones (starting with .) | ls -a |
ls -la |
Combine both detailed and all files options | ls -la |
ls -lh |
List with human-readable file sizes (KB, MB, GB) | ls -lh |
ls -lt |
List sorted by modification time (newest first) | ls -lt |
cd directory |
Change directory - navigate to specified location | cd Documents |
cd .. |
Move up one directory level | cd .. |
cd ~ |
Go to home directory | cd ~ |
cd / |
Go to root directory | cd / |
cd - |
Go to the previous directory | cd - |
tree |
Display directory structure as a tree | tree -L 2 |
Example workflow:
File Operations¶
| Command | Description | Example |
|---|---|---|
touch filename |
Create an empty file | touch notes.txt |
mkdir dirname |
Create a new directory | mkdir projects |
mkdir -p a/b/c |
Create nested directories at once | mkdir -p projects/web/assets |
cp source destination |
Copy files or directories | cp file.txt backup/ |
cp -r source destination |
Copy directories recursively | cp -r projects backup/ |
mv source destination |
Move/rename files or directories | mv file.txt newname.txt |
rm filename |
Remove/delete a file | rm unwanted.txt |
rm -r dirname |
Remove a directory and its contents | rm -r oldproject |
rm -i filename |
Interactive removal (asks for confirmation) | rm -i important.txt |
cat filename |
Display file contents | cat config.txt |
less filename |
View file contents with scrolling | less longfile.log |
head filename |
Show first 10 lines of file | head data.csv |
head -n 20 file |
Show first 20 lines | head -n 20 data.csv |
tail filename |
Show last 10 lines of file | tail log.txt |
tail -f filename |
Watch file for changes in real-time | tail -f /var/log/syslog |
wc -l filename |
Count lines in a file | wc -l data.csv |
diff file1 file2 |
Compare two files | diff old.conf new.conf |
ln -s target link |
Create a symbolic link | ln -s /etc/nginx/sites-available/mysite mysite |
Example workflow for creating a backup:
File Permissions¶
| Command | Description | Example |
|---|---|---|
chmod permissions file |
Change file permissions | chmod 755 script.sh |
chmod +x filename |
Make a file executable | chmod +x run.sh |
chmod -R 755 dir |
Apply permissions recursively | chmod -R 755 /var/www |
chown user:group file |
Change file owner and group | sudo chown pi:pi myfile.txt |
chown -R user:group dir |
Change ownership recursively | sudo chown -R www-data:www-data /var/www |
Understanding Permission Numbers¶
Each file has three permission groups: Owner, Group, and Others. Each group can have read (4), write (2), and execute (1) permissions:
Common permission patterns:
| Pattern | Numeric | Use Case |
|---|---|---|
rwxr-xr-x |
755 |
Executable scripts, directories |
rw-r--r-- |
644 |
Regular files, config files |
rw------- |
600 |
Private files (SSH keys, passwords) |
rwx------ |
700 |
Private directories, scripts |
rwxrwxr-x |
775 |
Shared group directories |
Searching and Filtering¶
These commands are incredibly powerful for finding files and filtering text — essential skills for any Linux user.
find — Search for Files¶
grep — Search Inside Files¶
Combining find and grep¶
Text Processing¶
awk — Column-Based Processing¶
sed — Stream Editor¶
sort and uniq¶
Piping and Redirection¶
Piping (|) and redirection (>, >>) are what make the Linux command line truly powerful. They let you chain commands together to build complex data processing pipelines.
Redirection¶
Piping¶
Process Management¶
| Command | Description | Example |
|---|---|---|
ps aux |
List all running processes | ps aux |
top |
Show real-time system processes | top |
htop |
Enhanced version of top (install: sudo apt install htop) |
htop |
kill PID |
Terminate a process by its ID | kill 1234 |
kill -9 PID |
Force kill a process | kill -9 1234 |
killall name |
Kill all processes by name | killall chromium |
bg |
Resume a stopped job in the background | bg |
fg |
Bring a background job to foreground | fg |
jobs |
List background jobs | jobs |
nohup cmd & |
Run a command that survives logout | nohup ./server.py & |
Running Long Processes¶
System Information¶
| Command | Description | Example |
|---|---|---|
uname -a |
Show kernel and system info | uname -a |
cat /etc/os-release |
Show OS version | cat /etc/os-release |
hostname -I |
Show IP addresses | hostname -I |
df -h |
Show disk space usage in human-readable format | df -h |
du -sh dir |
Show directory size | du -sh ~/projects |
free -h |
Display memory usage | free -h |
vcgencmd measure_temp |
Show CPU temperature | vcgencmd measure_temp |
vcgencmd measure_volts |
Show voltage | vcgencmd measure_volts |
vcgencmd get_throttled |
Check throttling status | vcgencmd get_throttled |
cat /proc/cpuinfo |
Display CPU information | cat /proc/cpuinfo |
lscpu |
Show CPU architecture details | lscpu |
uptime |
Show how long system has been running | uptime |
who |
Show who is logged in | who |
lsblk |
List block devices (disks and partitions) | lsblk |
lsusb |
List USB devices | lsusb |
lspci |
List PCI devices (Pi 5 only) | lspci |
dmesg \| tail |
Show recent kernel messages | dmesg \| tail -20 |
Raspberry Pi-specific monitoring script:
Save this as ~/status.sh, make it executable with chmod +x ~/status.sh, and run it anytime with ./status.sh.
Package Management¶
| Command | Description | Example |
|---|---|---|
sudo apt update |
Update package lists | sudo apt update |
sudo apt upgrade |
Upgrade all installed packages | sudo apt upgrade |
sudo apt full-upgrade |
Upgrade with dependency changes | sudo apt full-upgrade |
sudo apt install package |
Install a package | sudo apt install python3-pip |
sudo apt remove package |
Remove a package | sudo apt remove unwanted-pkg |
sudo apt purge package |
Remove package and its config files | sudo apt purge unwanted-pkg |
sudo apt autoremove |
Remove unneeded dependencies | sudo apt autoremove |
sudo apt clean |
Clear downloaded package cache | sudo apt clean |
dpkg -l |
List all installed packages | dpkg -l |
dpkg -l \| grep name |
Search installed packages | dpkg -l \| grep python |
apt search keyword |
Search for packages | apt search python |
apt show package |
Show package details | apt show nginx |
apt list --upgradable |
Show available upgrades | apt list --upgradable |
Example update workflow:
Automatic Security Updates
Install unattended-upgrades for automatic security patches:
Disk and Storage Management¶
Network Commands¶
| Command | Description | Example |
|---|---|---|
ip addr |
Show network interfaces and IP addresses | ip addr |
ip route |
Show routing table | ip route |
ping host |
Test connectivity to a host | ping -c 4 google.com |
traceroute host |
Trace route to a host | traceroute google.com |
nslookup domain |
DNS lookup | nslookup google.com |
dig domain |
Detailed DNS lookup | dig google.com |
ss -tuln |
Show listening ports (modern replacement for netstat) | ss -tuln |
netstat -tuln |
Show listening ports | netstat -tuln |
ssh user@host |
Connect to remote system via SSH | ssh pi@192.168.1.5 |
scp file user@host:path |
Securely copy files between systems | scp data.txt pi@192.168.1.5:~/ |
rsync -avz src dest |
Sync files efficiently | rsync -avz ~/projects/ pi@backup:~/projects/ |
wget URL |
Download files from the web | wget https://example.com/file.zip |
curl URL |
Transfer data from/to servers | curl -s https://api.ipify.org |
Useful networking one-liners:
systemd Service Management¶
systemd is the service manager on Raspberry Pi OS. Understanding it is essential for managing background services.
For creating your own services, see Managing Your Program as a systemd Daemon.
Text Editing¶
While not commands per se, knowing a text editor is essential:
| Editor | Description | How to use |
|---|---|---|
nano |
Simple, beginner-friendly editor | nano filename.txt |
vim |
Powerful, keyboard-driven editor | vim filename.txt |
Basic nano commands:
Ctrl+O: Save fileCtrl+X: Exit editorCtrl+K: Cut current lineCtrl+U: Paste cut lineCtrl+W: Search in fileCtrl+G: Get help
Useful One-Liner Collection¶
Here are battle-tested one-liners that you'll use again and again on your Raspberry Pi:
Shell Script Basics¶
Once you're comfortable with individual commands, you can combine them into scripts for automation.
Your First Script¶
Save as ~/maintenance.sh and run:
Key Scripting Concepts¶
Tips for Command Line Efficiency¶
- Tab completion: Press Tab to autocomplete commands and filenames
- Command history:
- Press Up Arrow to cycle through previous commands
historyto see all past commandsCtrl+Rto search command history interactively!!to repeat the last command (great withsudo !!)
- Wildcards:
*matches everything:rm *.tmp?matches single character:file?.txtmatchesfile1.txt[0-9]matches a range:log[0-9].txt
- Combining commands:
&&— run next command only if previous succeeded:make && ./program||— run next command only if previous failed:ping -c1 8.8.8.8 || echo "No internet";— run next command regardless:echo "start"; sleep 5; echo "done"
- Aliases — Create shortcuts for common commands:
- Ctrl shortcuts:
Ctrl+C— Cancel current commandCtrl+D— Exit the terminal / end inputCtrl+L— Clear the screenCtrl+A— Move cursor to beginning of lineCtrl+E— Move cursor to end of lineCtrl+U— Delete from cursor to start of line
Learning More¶
man command— View the manual for any commandcommand --help— Show brief help informationtldr command— Show simplified examples (install:sudo apt install tldr)
Related Guides¶
- Networking Setup and Management — Configure Wi-Fi, static IPs, and remote access
- System Settings Configuration — Optimize your Raspberry Pi's performance and settings
- Managing Your Program as a systemd Daemon — Turn your scripts into services
- Security Hardening Guide — Secure your Raspberry Pi
Most importantly, don't be afraid to experiment! The command line becomes more intuitive with practice, and it's the most powerful way to control your Raspberry Pi.