Skip to content

Raspberry Pi - Linux Shell

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).

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.

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 is pi).
  • @: 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.

To see the contents of the current directory:

ls

Common 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.

To move into a different directory:

cd DirectoryName

Example: 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 just cd: 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.

To print the full path of the current working directory:

pwd

To make a new directory:

mkdir NewDirectoryName

Example: 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

To remove an empty directory:

rmdir EmptyDirectoryName

rmdir will fail if the directory is not empty.

To remove (delete) a file:

rm filename.txt

To remove a directory and all its contents (use with extreme caution!):

rm -r DirectoryName

Common options for rm:

  • -r or -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.

To copy a file:

cp source_file destination_file

Example: 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_name

Example: mv temp_file.txt final_report.txt

Displays the entire content of a file to the screen. Best for short files.

cat filename.txt

Displays 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.log

Display the beginning (head) or end (tail) of a file. Useful for quickly checking log files.

head filename.txt # Shows first 10 lines by default
tail filename.txt # Shows last 10 lines by default
tail -n 20 filename.txt # Shows last 20 lines
tail -f filename.log # Follows the file, showing new lines as they are added (press Ctrl+C to stop)

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, d for 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 with cd).
  • -: Permission denied.

The chmod command is used to change permissions (e.g., chmod +x script.sh makes a script executable).

To learn more about a specific command, use the manual (man) command:

man command_name

Example: man ls shows the manual page for the ls command. Use arrow keys to scroll, q to quit.

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_root

Example: 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.

  • Tab Completion: Start typing a command or filename and press the Tab key. The shell will try to autocomplete it. If there are multiple possibilities, pressing Tab twice 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.

Send the output of a command to a file instead of the screen.

ls -l > file_list.txt # Overwrites file_list.txt with the output
ls -l >> file_list.txt # Appends the output to file_list.txt

Send the output of one command as the input to another command.

ls -l | less # View long listing page by page
dpkg -l | grep python # List installed packages and filter for lines containing 'python'

To safely shut down your Raspberry Pi:

sudo shutdown -h now

Or, often simpler:

sudo poweroff

To reboot your Raspberry Pi:

sudo reboot

Always shut down properly before disconnecting power to avoid corrupting the MicroSD card.