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.
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 |
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 / |
Example workflow:
File Operations¶
| Command | Description | Example |
|---|---|---|
touch filename |
Create an empty file | touch notes.txt |
mkdir dirname |
Create a new directory | mkdir projects |
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 |
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 |
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 |
chown user:group file |
Change file owner and group | sudo chown pi:pi myfile.txt |
Understanding permissions numbers: - First digit: Owner permissions (read=4, write=2, execute=1) - Second digit: Group permissions - Third digit: Everyone else
For example, chmod 755 script.sh gives:
- Owner: read(4) + write(2) + execute(1) = 7
- Group: read(4) + execute(1) = 5
- Others: read(4) + execute(1) = 5
System Information¶
| Command | Description | Example |
|---|---|---|
top |
Show real-time system processes | top |
htop |
Enhanced version of top (may need installation) | htop |
df -h |
Show disk space usage in human-readable format | df -h |
free -h |
Display memory usage | free -h |
vcgencmd measure_temp |
Show CPU temperature | vcgencmd measure_temp |
cat /proc/cpuinfo |
Display CPU information | cat /proc/cpuinfo |
uptime |
Show how long system has been running | uptime |
who |
Show who is logged in | who |
ps aux |
List all running processes | ps aux |
kill PID |
Terminate a process by its ID | kill 1234 |
killall process_name |
Kill all processes by name | killall chromium |
Example for monitoring system:
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 install package |
Install a package | sudo apt install python3-pip |
sudo apt remove package |
Remove a package | sudo apt remove unwanted-pkg |
sudo apt autoremove |
Remove unneeded dependencies | sudo apt autoremove |
dpkg -l |
List all installed packages | dpkg -l |
apt search keyword |
Search for packages | apt search python |
Example update workflow:
Network Commands¶
| Command | Description | Example |
|---|---|---|
ifconfig |
Show network interfaces | ifconfig |
ip addr |
Modern alternative to ifconfig | ip addr |
ping host |
Test connectivity to a host | ping google.com |
nmap IP |
Scan network ports (may need installation) | nmap 192.168.1.1 |
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:~/ |
wget URL |
Download files from the web | wget https://example.com/file.zip |
curl URL |
Transfer data from/to servers | curl https://api.weather.com |
Example for checking network:
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 file
- Ctrl+X: Exit editor
- Ctrl+G: Get help
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
- Wildcards: Use
*to match multiple files (rm *.tmp) - Combining commands: Use
&&to run commands in sequence (mkdir test && cd test) - Redirecting output:
command > file.txt- Save output to filecommand >> file.txt- Append output to file
- Piping: Connect commands with
|(cat file.txt | grep search_term)
Learning More¶
man command- View the manual for any commandcommand --help- Show brief help information
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.