Raspberry Pi online course

File Management in Raspberry Pi OS Terminal

Learn Raspberry Pi OS terminal commands for creating, viewing, copying, moving, renaming, and deleting files and directories safely.

Using the terminal for file management

Raspberry Pi OS is based on Linux, so you can manage files and folders with Linux command-line tools. The terminal is a text-based interface where you enter commands and press Enter to run them.

A command is a program or instruction run from the terminal. Commands operate on files and directories identified by paths. A filename, path, option, or other value supplied to a command is called an argument.

command option argument

For example, in ls /home/pi, ls is the command and /home/pi is an argument identifying the directory to list.

<

Understanding file paths

A path describes the location of a file or directory in the file system. A directory is a folder that contains files and possibly other directories.

Absolute and relative paths

An absolute path starts at the root directory and gives the complete location. It always begins with /.

/home/pi/example.txt

A relative path is interpreted from the terminal's current working directory, which is the directory where a command is currently operating.

example.txt

If the current working directory is /home/pi, the relative path example.txt refers to /home/pi/example.txt.

Common path symbols and locations

The root directory, written as /, is the top level of a Linux file system. The user's home directory is the personal directory for that account. On many Raspberry Pi OS installations it is /home/pi, but newer installations may use a different username. The shortcut ~ means the current user's home directory.

The /tmp directory is used for temporary storage. Files placed there may be removed automatically when the system restarts or when temporary files are cleaned up.

File and Directory Path Examples

Absolute path/home/pi/example.txt — Complete path beginning at the root directory.

Home-directory shortcut~/example.txt — A file named example.txt in the current user's home directory.

Relative pathnotes/today.txt — Interpreted from the current working directory.

Current directory. — The directory where the command is currently operating.

Parent directory.. — The directory one level above the current directory.

Temporary directory/tmp — A common location for temporary files.

Linux paths and filenames are case-sensitive. For example, Notes.txt, notes.txt, and NOTES.TXT can be different filenames.

Creating files with touch

The touch command creates an empty file when the named file does not exist.

touch /home/pi/textfile.txt

After this command, an empty file named textfile.txt exists in the /home/pi directory. Using the home-directory shortcut is often more portable when the username is not pi:

touch ~/textfile.txt

If the file already exists, touch normally keeps its contents and updates its modification timestamp instead. It does not erase existing text.

Confirm the file exists with:

ls /home/pi

The listing should include textfile.txt.

Listing and identifying directories

List contents with ls

ls lists the contents of a directory. With no argument, it lists the current working directory.

ls

Useful options include:

ls -l
ls -la /home/pi

-l displays detailed information. -a includes hidden files, whose names usually begin with a dot. Using both options as ls -la is a useful way to inspect a directory before changing its contents.

Change directory with cd

cd changes the current working directory.

cd /home/pi
cd ~/projects
cd ..
cd ~

cd .. moves to the parent directory, while cd ~ returns to the current user's home directory.

Display the current directory with pwd

When a relative path is confusing, use pwd to print the current working directory.

pwd

Copying files and directories with cp

The cp command copies data. Its basic structure is a source followed by a destination:

cp source destination

Copy a file from the home directory to temporary storage:

cp /home/pi/textfile.txt /tmp/

The original remains in /home/pi, and a copy named textfile.txt is placed in /tmp.

When the destination is an existing directory, the copied file normally keeps its original name. When the destination is a new filename, the copy receives that name:

cp source.txt destination.txt

Before copying, use ls to verify the source and destination. If a destination filename already exists, cp may overwrite it depending on the options and system configuration.

Copy a directory recursively

Directories contain other entries, so copying a directory requires the recursive option -r. Recursive means that an operation also processes the contents of subdirectories.

cp -r ~/projects ~/projects-backup

This creates projects-backup with copied contents. Inspect both paths before and after the command.

Viewing text-file contents

less is a paged viewer for reading text files one screen at a time. It is suitable for longer files.

less /home/pi/textfile.txt
  • Scroll with the arrow keys.
  • Use Space or Page Down to move forward.
  • Use Page Up to move backward.
  • Press q to quit and return to the shell prompt.

cat prints a file directly to the terminal:

cat /home/pi/textfile.txt

cat is convenient for a short text file. For a long file, its entire contents may rapidly scroll past, so less is usually easier to read.

Moving and renaming with mv

mv moves files or directories. It also renames them when the source and destination are in the same directory.

Rename a file:

mv oldname.txt newname.txt

Move a file to another directory:

mv file.txt /home/pi/documents/

As with cp, the final argument may be a destination directory or a new destination filename. Check the paths with ls before pressing Enter.

Deleting files with rm

rm removes files. Unlike a desktop file manager's recycle bin, a normal terminal rm command does not place the file somewhere for easy restoration.

ls /home/pi/textfile.txt
rm /home/pi/textfile.txt

Check the target path carefully before pressing Enter. A typo or overly broad path can remove the wrong file.

For interactive confirmation, use -i:

rm -i /home/pi/textfile.txt

The command asks whether to remove the file. Answer only after confirming the displayed target.

Directory management

Create a directory with mkdir

Use mkdir to create a directory:

mkdir ~/projects
cd ~/projects
pwd
ls

This creates projects, changes into it, confirms the location, and lists its contents.

Remove an empty directory with rmdir

rmdir removes a directory only when it is empty:

rmdir empty_directory

This restriction is useful because it prevents accidental deletion of a directory that still contains files.

Recursive directory deletion

rm -r directory removes a directory and its contents recursively. Inspect the directory first:

ls -la directory
rm -r directory

The combination rm -rf adds forced deletion. It can suppress prompts and errors for some conditions, so it is highly destructive:

rm -rf directory

Never use recursive or forced deletion options unless you understand the exact path and have verified that the contents are disposable.

Deletion Options and Safety Considerations

rm file — Removes one specified file — Medium risk — Verify the exact path with ls first.

rm -i file — Requests confirmation before removal — Lower risk — Read the target carefully and confirm only when correct.

rmdir directory — Removes an empty directory — Lower risk — Use ls -la to confirm it is empty.

rm -r directory — Removes a directory and its contents — High risk — Inspect the directory and avoid broad paths.

rm -rf directory — Forces recursive removal — Very high risk — Avoid unless the target is fully verified and deletion is intentional.

Basic file-management command reference

Basic Raspberry Pi OS File-Management Commands

touch — Create an empty file or update a timestamp — touch notes.txt — Does not erase existing contents.

rm — Remove files — rm notes.txt — No normal recycle-bin recovery.

cp — Copy files or directories — cp a.txt /tmp/ — Use -r for directories.

mv — Move or rename — mv old.txt new.txt — Verify whether the destination is a directory or filename.

less — Read text page by page — less notes.txt — Press q to quit.

cat — Print text directly — cat notes.txt — Best for short readable text files.

ls — List directory contents — ls -la — Use it to verify targets.

cd — Change directory — cd ~/projects — Changes the current working directory.

pwd — Show the current directory — pwd — Helps diagnose relative paths.

mkdir — Create a directory — mkdir new_directory — The parent location must exist unless additional options are used.

rmdir — Remove an empty directory — rmdir empty_directory — Fails if contents remain.

man — Open command documentation — man cp — Press q to exit.

Getting command help

Use a man page, which is built-in command documentation, with man followed by a command name:

man rm
man cp

Manual pages open in a text viewer, usually less. Scroll with the arrow keys or Page Up and Page Down, and press q to exit.

Many commands also support --help for a shorter reference:

cp --help
rm --help

Read the manual or help output before using destructive flags such as recursive or forced deletion options.

Safe file-management habits

  • Run ls to verify files and directories before copying, moving, or deleting them.
  • Run pwd when you are unsure which directory a relative path refers to.
  • Use Tab completion to complete long filenames and paths, reducing typing mistakes.
  • Put quotes around paths containing spaces.
less "My Notes/today.txt"
mv "old project" "new project"
  • Practice in your home directory rather than in protected system locations.
  • Avoid modifying system files unless you understand their purpose and the effect of the change.
  • Treat rm -r, rm -f, and especially rm -rf as potentially destructive.
  • Do not use elevated privileges merely to make an error disappear. Understand the permission problem first.

Troubleshooting common errors

No such file or directory

This usually means the path is incorrect, the command is running from an unexpected directory, or filename capitalization does not match.

pwd
ls
ls /home/pi

Use pwd to identify the current directory, ls to inspect available entries, or an absolute path to avoid ambiguity.

Permission denied

The current user may not have permission to read, write, or delete the target, particularly in a protected system location. Inspect details with:

ls -l path/to/file

When practicing, work in the user's home directory. Do not use elevated privileges unless you understand both the file and the reason permission is required. See Get Help for further guidance.

rm says the target is a directory

Plain rm does not remove directories. Use rmdir if the directory is empty. If a non-empty directory truly must be removed, inspect it first and use rm -r carefully.

cp does not copy a directory

The recursive option was probably omitted. Use cp -r after confirming both source and destination paths.

The copied file cannot be found

The final cp argument may have been interpreted as a new filename rather than a directory, or the file may have been copied under a different name. List the destination:

ls /tmp
ls -la destination_directory

less appears to take over the terminal

The file is open in the interactive viewer. Press q to quit. Use the arrow keys, Space, Page Up, or Page Down to navigate first.

cat produces unreadable output

The target may be binary rather than text, or it may be too large for convenient direct output. Use less for long text files and avoid displaying binary files directly in the terminal.

Exam-relevant notes

  • An absolute path begins with /; a relative path is interpreted from the current working directory.
  • pwd identifies the current working directory, and cd changes it.
  • touch creates an empty file if needed or updates an existing file's modification timestamp.
  • cp copies, mv moves or renames, and rm removes.
  • cp -r copies directories recursively; rm -r deletes directory contents recursively.
  • rmdir removes only empty directories.
  • less is paged and exits with q; cat prints directly to the terminal.
  • Linux filenames and paths are case-sensitive.
  • Use man command or command --help to check syntax and options.

Once these commands are comfortable, related skills include Directory Management, Useful Terminal Commands, Search for Files, and Create and Extract Archives.