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 argumentFor 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.txtA relative path is interpreted from the terminal's current working directory, which is the directory where a command is currently operating.
example.txtIf 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.
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.txtAfter 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.txtIf 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/piThe 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.
lsUseful 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.
pwdCopying files and directories with cp
The cp command copies data. Its basic structure is a source followed by a destination:
cp source destinationCopy 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.txtBefore 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-backupThis 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
qto quit and return to the shell prompt.
cat prints a file directly to the terminal:
cat /home/pi/textfile.txtcat 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.txtMove 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.txtCheck 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.txtThe 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
lsThis 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_directoryThis 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 directoryThe combination rm -rf adds forced deletion. It can suppress prompts and errors for some conditions, so it is highly destructive:
rm -rf directoryNever use recursive or forced deletion options unless you understand the exact path and have verified that the contents are disposable.
Basic file-management command reference
Getting command help
Use a man page, which is built-in command documentation, with man followed by a command name:
man rm
man cpManual 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 --helpRead the manual or help output before using destructive flags such as recursive or forced deletion options.
Safe file-management habits
- Run
lsto verify files and directories before copying, moving, or deleting them. - Run
pwdwhen 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 especiallyrm -rfas 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/piUse 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/fileWhen 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_directoryless 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. pwdidentifies the current working directory, andcdchanges it.touchcreates an empty file if needed or updates an existing file's modification timestamp.cpcopies,mvmoves or renames, andrmremoves.cp -rcopies directories recursively;rm -rdeletes directory contents recursively.rmdirremoves only empty directories.lessis paged and exits withq;catprints directly to the terminal.- Linux filenames and paths are case-sensitive.
- Use
man commandorcommand --helpto 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.