Skip to content

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.

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:

1
2
3
4
5
6
7
8
# Check where you are
pwd
# List files in current directory
ls -la
# Navigate to Documents folder
cd Documents
# Verify your new location
pwd

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:

1
2
3
4
5
6
7
# Create a backup directory
mkdir ~/backup
# Copy important configuration files
cp /boot/firmware/config.txt ~/backup/
cp -r ~/projects ~/backup/
# Verify the backup was created
ls -l ~/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:

1
2
3
4
5
6
# Check system temperature
vcgencmd measure_temp
# See current memory usage
free -h
# Identify CPU-intensive processes
top

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:

1
2
3
4
5
6
# Update package lists
sudo apt update
# Upgrade all packages
sudo apt upgrade
# Clean up unnecessary packages
sudo apt autoremove

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:

1
2
3
4
5
6
# Check your IP address
ip addr
# Test internet connectivity
ping -c 4 google.com
# See what services are listening on your system
netstat -tuln

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

  1. Tab completion: Press Tab to autocomplete commands and filenames
  2. Command history:
    • Press Up Arrow to cycle through previous commands
    • history to see all past commands
    • Ctrl+R to search command history
  3. Wildcards: Use * to match multiple files (rm *.tmp)
  4. Combining commands: Use && to run commands in sequence (mkdir test && cd test)
  5. Redirecting output:
    • command > file.txt - Save output to file
    • command >> file.txt - Append output to file
  6. Piping: Connect commands with | (cat file.txt | grep search_term)

Learning More

  • man command - View the manual for any command
  • command --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.