VMware ESXi and vSphere Cluster Management

Directory Management Commands in Raspberry Pi OS

Learn how to create, move, rename, copy, and delete directories in Raspberry Pi OS using mkdir, mv, rmdir, rm -r, and cp -r.

Directories are containers used to organize files and other directories. In a graphical interface, a directory is commonly called a folder. Raspberry Pi OS, formerly known as Raspbian, provides terminal commands for managing directories efficiently.

This lesson covers mkdir, mv, rmdir, rm -r, and cp -r, along with basic path checking and safety practices.

How Linux directories and paths work

A directory is a special filesystem object that can contain files and subdirectories. Linux organizes these objects in a hierarchical, tree-shaped structure called the filesystem hierarchy.

The top of this tree is the root directory, written as /. Every other directory is located beneath it. For example, /home/pi/project identifies a directory named project inside /home/pi.

A path is a textual address that identifies a file or directory. An absolute path begins with / and is resolved from the root directory. A relative path is interpreted from the current working directory, which is the directory where terminal commands operate unless another location is specified.

/
├── home
│   └── pi
│       └── project
├── tmp
└── etc

For example, /home/pi/project is absolute. If the current working directory is /home/pi, then project is a relative path referring to the same location.

Check your location and directory contents

Before managing directories, confirm where the terminal is operating and inspect the relevant path.

pwd
ls
ls -la /tmp

pwd displays the current working directory. ls lists directory contents, and ls -la DIRECTORY shows detailed contents, including hidden items, in a specified directory.

Creating directories with mkdir

mkdir means “make directory.” It creates a new directory at the path you provide.

mkdir DIRECTORY

To create a directory in the current working directory, use:

mkdir new-directory

This creates new-directory beneath the directory reported by pwd.

You can also provide an absolute path:

mkdir /home/pi/new-directory

This creates new-directory under /home/pi. The parent directory must already exist unless you use the -p option.

mkdir -p /home/pi/projects/raspberry-pi

mkdir -p creates missing parent directories as needed. If the requested directory already exists, behavior is generally non-destructive with -p, provided the path is valid.

Moving and renaming directories with mv

mv moves a directory from one location to another. The first argument is the source, meaning the original directory. The second argument is the destination, meaning the target location or name.

mv SOURCE_DIRECTORY DESTINATION

To move /home/pi/new-directory into /tmp/, run:

mv /home/pi/new-directory /tmp/

After a successful command, the directory is located at /tmp/new-directory and no longer remains at /home/pi/new-directory.

Moving a directory to a different name in the same parent directory performs a rename:

mv /home/pi/new-directory /home/pi/renamed-directory

The directory stays under /home/pi, but its name changes. A move does not retain a duplicate at the source location.

Deleting empty directories with rmdir

rmdir removes a directory only when it is empty. It does not remove files or nested directories.

rmdir DIRECTORY

For example:

rmdir /tmp/new-directory

If /tmp/new-directory contains no files or subdirectories, it is removed. If it contains anything, rmdir reports an error such as “Directory not empty.”

Deleting non-empty directories with rm -r

To remove a directory and its nested contents, use recursive removal:

rm -r DIRECTORY

The -r option means recursive: the command operates on the directory and everything beneath it, including files and subdirectories.

rm -r /tmp/new-directory

This removes /tmp/new-directory and all of its contents. It is a destructive operation and normally does not provide a way to restore deleted data from the command line.

Beginners can use the interactive form, which asks for confirmation during removal:

rm -ri /tmp/new-directory

Copying directories with cp -r

cp copies files. The -r option makes it copy a directory recursively, including its files and subdirectories.

cp -r SOURCE_DIRECTORY DESTINATION

To copy /home/pi/new-directory into /tmp/, run:

cp -r /home/pi/new-directory /tmp/

This creates a copy at /tmp/new-directory while the original remains at /home/pi/new-directory.

Copying versus moving

Both cp -r and mv can place a directory inside an existing destination directory, but they have different results.

Copying a directory

Command: cp -r SOURCE DESTINATION

Original retained? Yes.

Result at destination: A separate duplicate containing the copied contents.

Moving a directory

Command: mv SOURCE DESTINATION

Original retained? No, after a successful move.

Result at destination: The directory is relocated, or renamed if the destination is a new name in the same parent directory.

Basic directory management commands

mkdir — Creates a directory. Example: mkdir new-directory. The parent path must exist unless -p is used.

mv — Moves or renames a directory. Example: mv SOURCE DESTINATION. The source no longer remains at its original path after success.

rmdir — Removes an empty directory. Example: rmdir /tmp/new-directory. It fails when contents remain.

rm -r — Recursively removes a directory and its contents. Example: rm -r /tmp/new-directory. Use only after verifying the path.

cp -r — Recursively copies a directory. Example: cp -r SOURCE DESTINATION. The original remains.

Troubleshooting common problems

“Directory not empty” from rmdir

The directory contains one or more files or subdirectories. Inspect it first:

ls -la /tmp/new-directory

If deleting every contained item is intended, use rm -r /tmp/new-directory carefully.

“No such file or directory” for a source

The directory name or path may be incorrect, or the command may be running from a different current working directory than expected.

pwd
ls
ls -la /home/pi/new-directory

Use an absolute path when you need to remove uncertainty about the source location.

A copy remains when you expected the source to disappear

cp -r creates a duplicate. Use mv SOURCE DESTINATION when the goal is relocation. If an unwanted duplicate exists, verify which copy should remain before removing anything.

The destination already has a directory with the same name

Moving or copying into an existing directory can place the source inside it. Depending on the command and destination form, contents may also be merged or replaced. Inspect the destination first and choose an explicit path or a new name to avoid ambiguity.

“Permission denied”

The current user may lack write permission for the parent directory or target contents. Work in a directory owned by your user when possible. Inspect ownership and permissions before considering elevated privileges such as sudo.

A recursive command targets the wrong location

Stop if the path is uncertain. Check the current location and target contents:

pwd && ls -la /tmp/new-directory

Correct typos, avoid unsafe wildcards, and do not apply destructive commands to broad system paths.

Practical command sequence

The following sequence demonstrates creation, copying, moving, inspection, and removal. Run commands individually so that you can check each result.

mkdir /home/pi/new-directory
cp -r /home/pi/new-directory /tmp/
ls -la /home/pi/new-directory
ls -la /tmp/new-directory
mv /home/pi/new-directory /home/pi/renamed-directory
rmdir /tmp/new-directory

The final rmdir succeeds only if the copied directory is still empty. If it contains files, inspect it and use recursive removal only when deleting its contents is intentional.

Exam-relevant notes

  • / is the root directory and the top of the Linux filesystem hierarchy.
  • An absolute path begins with /; a relative path is interpreted from the current working directory.
  • mkdir creates directories, while mkdir -p can create missing parents.
  • mv relocates or renames a directory and does not leave the original at its old location after success.
  • rmdir removes only empty directories.
  • rm -r removes a directory recursively with its contents and must be used cautiously.
  • cp -r creates a recursive duplicate and leaves the original in place.
  • Use pwd and ls -la to verify locations and contents before destructive operations.