Raspberry Pi - Linux Shell
Raspberry Pi - Linux Shell (Command Line)
Section titled “Raspberry Pi - Linux Shell (Command Line)”While the Raspberry Pi OS offers a graphical desktop, the Linux shell (also known as the command line or terminal) provides a powerful text-based way to interact with your Pi. Understanding basic shell commands is essential for many tasks, including system administration, programming, and automation. The default shell on Raspberry Pi OS is Bash (Bourne Again SHell).
Opening a Terminal Window
Section titled “Opening a Terminal Window”You can access the shell in a couple of ways from the desktop environment:
- Click the Terminal icon (usually looks like
>_or a black screen) on the taskbar at the top of the screen. - Open the Application Menu (Raspberry icon) -> Accessories -> Terminal.
- Use the keyboard shortcut:
Ctrl + Alt + T.
If your Pi boots directly to the command line (Lite version of the OS), you are already in the shell.
Understanding the Prompt
Section titled “Understanding the Prompt”When the terminal opens, you’ll see a prompt, typically looking like this:
pi@raspberrypi:~ $Let’s break down its components:
pi: The username of the currently logged-in user (the default user ispi).@: Separator.raspberrypi: The hostname of the machine (its name on the network). You can change this.:: Separator.~: The current working directory. The tilde (~) is a shortcut for the user’s home directory (e.g.,/home/pi).$: Indicates that you are logged in as a regular user. If you see a#symbol, it means you are operating as the superuser (root), which has administrative privileges.
You type commands after the $ (or #) symbol and press Enter to execute them.
Basic Navigation and File Management
Section titled “Basic Navigation and File Management”Listing Files and Directories (ls)
Section titled “Listing Files and Directories (ls)”To see the contents of the current directory:
lsCommon options for ls:
ls -l: Long listing format (shows permissions, owner, size, modification date).ls -a: Show all files, including hidden ones (names starting with a dot.).ls -h: Human-readable file sizes (e.g., KB, MB, GB) when used with-l.ls -t: Sort by modification time, newest first.ls -r: Reverse the sort order.
You can combine options: ls -lah shows all files in long format with human-readable sizes.
Note: Linux commands and filenames are case-sensitive. MyFile.txt is different from myfile.txt.
Changing Directory (cd)
Section titled “Changing Directory (cd)”To move into a different directory:
cd DirectoryNameExample: cd Documents moves into the Documents directory (assuming it exists in the current location).
Special cd commands:
cd ..: Move up one level to the parent directory.cd ~or justcd: Go back to your home directory.cd /: Go to the root directory of the filesystem.cd -: Go back to the previous directory you were in.
Finding Your Current Location (pwd)
Section titled “Finding Your Current Location (pwd)”To print the full path of the current working directory:
pwdCreating Directories (mkdir)
Section titled “Creating Directories (mkdir)”To make a new directory:
mkdir NewDirectoryNameExample: mkdir MyProject
You can create multiple directories at once: mkdir Dir1 Dir2 Dir3
Create nested directories (parent must exist or use -p): mkdir -p ParentDir/ChildDir
Removing Empty Directories (rmdir)
Section titled “Removing Empty Directories (rmdir)”To remove an empty directory:
rmdir EmptyDirectoryNamermdir will fail if the directory is not empty.
Removing Files and Directories (rm)
Section titled “Removing Files and Directories (rm)”To remove (delete) a file:
rm filename.txtTo remove a directory and all its contents (use with extreme caution!):
rm -r DirectoryNameCommon options for rm:
-ror-R: Recursive, required for removing directories.-f: Force, attempts to remove without prompting (dangerous!).-i: Interactive, prompts for confirmation before each removal.-v: Verbose, shows what is being done.
Warning: Files removed with rm are generally not recoverable. There is no Recycle Bin in the standard shell. Double-check commands before pressing Enter, especially when using -r or wildcards.
Copying Files and Directories (cp)
Section titled “Copying Files and Directories (cp)”To copy a file:
cp source_file destination_fileExample: cp report.txt report_backup.txt (creates a copy in the same directory)
To copy a file into another directory:
cp source_file destination_directory/Example: cp image.jpg Pictures/
To copy a directory and its contents:
cp -r source_directory destination_directory/Example: cp -r MyProject Backups/
Moving and Renaming Files and Directories (mv)
Section titled “Moving and Renaming Files and Directories (mv)”To move a file or directory:
mv source destination_directory/Example: mv draft.txt Documents/
To rename a file or directory:
mv old_name new_nameExample: mv temp_file.txt final_report.txt
Viewing File Content
Section titled “Viewing File Content”Displays the entire content of a file to the screen. Best for short files.
cat filename.txtDisplays file content one screenful at a time. Allows scrolling up and down (using arrow keys, Page Up/Down) and searching (/ followed by search term). Press q to quit.
less long_file.loghead and tail
Section titled “head and tail”Display the beginning (head) or end (tail) of a file. Useful for quickly checking log files.
head filename.txt # Shows first 10 lines by defaulttail filename.txt # Shows last 10 lines by defaulttail -n 20 filename.txt # Shows last 20 linestail -f filename.log # Follows the file, showing new lines as they are added (press Ctrl+C to stop)Permissions
Section titled “Permissions”When you use ls -l, the first column shows file permissions, like -rw-r--r-- or drwxr-xr-x.
- First character:
-for a regular file,dfor a directory. - Next 3 characters: Permissions for the file owner (User).
- Next 3 characters: Permissions for the file’s group (Group).
- Last 3 characters: Permissions for everyone else (Others).
Permissions types:
r: Read permission (view file content, list directory content).w: Write permission (modify file, create/delete files in directory).x: Execute permission (run file as a program, enter directory withcd).-: Permission denied.
The chmod command is used to change permissions (e.g., chmod +x script.sh makes a script executable).
Getting Help (man)
Section titled “Getting Help (man)”To learn more about a specific command, use the manual (man) command:
man command_nameExample: man ls shows the manual page for the ls command. Use arrow keys to scroll, q to quit.
Superuser Privileges (sudo)
Section titled “Superuser Privileges (sudo)”Many administrative tasks (like installing software, editing system files, managing services) require root privileges. Instead of logging in as root directly (which is generally discouraged), use the sudo command to run a single command with root privileges.
sudo command_to_run_as_rootExample: sudo apt update (updates the package list).
You will usually be prompted for your user’s password (the pi user’s password by default) the first time you use sudo in a session.
Shell Shortcuts and Efficiency
Section titled “Shell Shortcuts and Efficiency”- Tab Completion: Start typing a command or filename and press the
Tabkey. The shell will try to autocomplete it. If there are multiple possibilities, pressingTabtwice often shows the options. - History: Press the Up Arrow key to recall previous commands. Use Down Arrow to move forward through history.
Ctrl + R: Reverse search through command history. Start typing part of a command, and it will show the most recent match.Ctrl + C: Stop (interrupt) the currently running command.Ctrl + L: Clear the terminal screen.Ctrl + A: Move cursor to the beginning of the line.Ctrl + E: Move cursor to the end of the line.
Input/Output Redirection and Pipes
Section titled “Input/Output Redirection and Pipes”Redirection (>, >>)
Section titled “Redirection (>, >>)”Send the output of a command to a file instead of the screen.
ls -l > file_list.txt # Overwrites file_list.txt with the outputls -l >> file_list.txt # Appends the output to file_list.txtPipes (|)
Section titled “Pipes (|)”Send the output of one command as the input to another command.
ls -l | less # View long listing page by pagedpkg -l | grep python # List installed packages and filter for lines containing 'python'System Shutdown and Reboot
Section titled “System Shutdown and Reboot”To safely shut down your Raspberry Pi:
sudo shutdown -h nowOr, often simpler:
sudo poweroffTo reboot your Raspberry Pi:
sudo rebootAlways shut down properly before disconnecting power to avoid corrupting the MicroSD card.