Directory Management in Raspberry Pi OS
Learn how to create, move, copy, rename, inspect, and delete directories in Raspberry Pi OS using mkdir, mv, rmdir, rm -r, cp -r, and ls.
Directories are the Linux equivalent of folders. They are filesystem containers that organize regular files and other directories. This lesson uses the Raspberry Pi OS terminal and assumes you understand the current working directory and basic commands such as cd, pwd, and ls. For a terminal introduction, see Terminal In Raspbian.
How directories fit into the Linux filesystem
A filesystem is the structure an operating system uses to store and organize files and directories. Linux uses a hierarchical, tree-shaped structure. The top of this tree is the root directory, written as /.
Directories can contain other directories, which are often called subdirectories, as well as regular files. A regular file stores data such as text, a program, an image, or a configuration. A directory instead stores the organization and references needed to access items below it.
/(root)
├── home/
│ └── pi/
│ └── new-directory/
├── tmp/
└── etc/
In this example, /home/pi and /tmp are directories below /. The path /home/pi/new-directory identifies a directory nested inside the pi home directory.
Understanding directory paths
A path is the written location of a file or directory.
Absolute paths
An absolute path gives a complete location beginning at the root directory. It always starts with /. Examples include:
/home/pi— the home directory traditionally used by thepiuser./tmp— a location commonly used for temporary files./home/pi/new-directory— a directory namednew-directoryinside/home/pi.
Relative paths
A relative path is interpreted from the current working directory. For example, if pwd reports /home/pi, then new-directory refers to /home/pi/new-directory.
Move and copy commands normally receive two important arguments: the source, which is the original directory, and the destination, which is the target location or new name. The order is source first, destination second.
Essential directory-management commands
Creating directories with mkdir
The mkdir command creates a new directory. To create new-directory in the current working directory, run:
mkdir new-directory
To create it at a specific absolute path, provide the complete path:
mkdir /home/pi/new-directory
This command creates an empty directory under /home/pi. The parent directory, /home/pi, must already exist, and you must have permission to write there.
Creating missing parent directories
The -p option creates missing parent directories as needed:
mkdir -p /home/pi/projects/example
If projects does not exist, mkdir -p creates it before creating example. It does not report an error merely because the requested directory already exists.
Moving and renaming directories with mv
Use mv to relocate a directory. The source comes first and the destination comes second:
mv /home/pi/new-directory /tmp/
After a successful move, the directory is located at /tmp/new-directory. It is no longer at /home/pi/new-directory.
mv can also rename a directory when the destination is a new name in the same parent directory:
mv /home/pi/new-directory /home/pi/project-directory
This changes the directory's name from new-directory to project-directory without moving it to a different parent.
Copying directories with cp -r
The cp command copies items. Directories require the recursive option, -r, so that their contents and nested directories are copied too:
cp -r /home/pi/new-directory /tmp/
This places a copy at /tmp/new-directory while leaving the source at /home/pi/new-directory.
Deleting empty directories with rmdir
Use rmdir when the directory is empty:
rmdir /tmp/new-directory
The directory is removed only if it contains no regular files, hidden files, or nested directories. This restriction makes rmdir a useful first choice when you want deletion to stop rather than remove contents.
Removing a directory and its contents with rm -r
If a directory contains anything, an attempt to use rmdir usually fails with a message such as “Directory not empty.” First inspect the contents:
ls /tmp/new-directory
If you have confirmed that the entire directory tree should be deleted, use recursive removal:
rm -r /tmp/new-directory
Recursive means the operation applies to the directory, its files, and all nested directories. This command is destructive and normally does not provide a recovery facility. Check the spelling and complete target path before pressing Enter.
An interactive variant asks for confirmation during removal:
rm -ri /tmp/new-directory
Interactive removal is safer for uncertain situations, but read every prompt carefully. Do not use rm -r or rm -ri on a path you have not verified.
Safe command use and verification
- Inspect a target before moving, copying, or deleting it:
ls /path/to/parentorls /path/to/directory. - Use
pwdwhen a relative path may be ambiguous. An absolute path can reduce confusion. - Linux names and paths are case-sensitive.
Project,project, andPROJECTcan refer to different directories. - Remember the source-first, destination-second order for
mvandcp -r. - Confirm the result after an operation with
ls, using both the old and new paths when appropriate. - Commands run with elevated permissions can affect locations your normal user cannot change. Avoid
sudofor destructive commands unless you fully understand the target.
A cautious practice sequence
mkdir /home/pi/new-directory
ls /home/pi
cp -r /home/pi/new-directory /tmp/
ls /home/pi
ls /tmp
mv /home/pi/new-directory /home/pi/project-directory
ls /home/pi
This sequence creates a directory, checks the parent, copies the directory while retaining the source, and then renames the source. To remove the temporary copy after checking it, use rmdir /tmp/new-directory if it is empty, or inspect it and use rm -r /tmp/new-directory only when its contents are disposable.
Troubleshooting directory commands
“Directory not empty”
The directory may contain regular files, hidden files, or nested directories. Run ls /path/to/directory to inspect it. Remove individual unwanted items where practical. Use rm -r only when the whole directory tree should be deleted.
“No such file or directory”
Check the spelling and capitalization of the path. The item may have already been moved or deleted, or a relative path may be based on a different current directory. Use pwd, switch to an absolute path, and inspect parent directories with ls.
“Permission denied”
The current user may not have permission to modify the target location. Work in a directory owned by that user, such as the home directory, and check permissions and ownership before considering administrative privileges. Do not use sudo as a way to guess at a path.
The copy is nested unexpectedly
If the destination already existed, cp -r may have placed the source directory inside it. Inspect the destination before copying and use ls afterward to verify the final nesting level.
A move was made when a backup was intended
mv relocates the original. Use cp -r when the original must remain. After copying, verify that both the source and destination exist.
Key points to remember
/is the root directory and the starting point of the Linux filesystem tree.- Absolute paths begin with
/; relative paths begin from the current working directory. mkdircreates directories, andmkdir -pcan create missing parents.mvmoves or renames;cp -rcopies while preserving the source.rmdirremoves only empty directories.rm -rremoves a directory and its contents recursively, so verify the path first.
For related command practice, see Useful Terminal Commands and File Management.