VMware ESXi and vSphere Cluster Management
Manage Directories in Linux
Learn how to create, move, rename, and safely delete Linux directories with mkdir, mv, rmdir, and rm.
A Linux directory, commonly called a folder, is a filesystem container that can hold files and other directories. Directories organize information in a hierarchy, much like branches and leaves in a tree.
Understanding Linux directories and paths
The Linux filesystem begins at the root directory, written as /. All other directories and files are located below this starting point. For example:
/
├── home
│ └── bob
│ └── projects
└── etc
A directory immediately above another directory is its parent directory. A directory contained inside another directory is a subdirectory. In this example, /home is the parent of /home/bob, and projects is a subdirectory of /home/bob.
A path is text that identifies the location of a file or directory. Linux uses two main kinds of paths:
- Absolute path: begins with
/and starts at the root directory. For example,/home/bob/projectsidentifies the same location regardless of your current directory. - Relative path: does not begin with
/and is interpreted from the shell's current working directory. For example,projectsmeans a directory namedprojectsinside the directory where you currently are.
Before using relative paths, check your location:
pwd
The pwd command prints the current working directory. You can then use ls to inspect nearby files and directories.
Creating directories with mkdir
The mkdir command creates directories. Its basic syntax is:
mkdir DIRECTORY
Create a directory in the current location
To create a directory named projects in the current working directory:
mkdir projects
To create a directory at a specified path, provide that path as the argument:
mkdir /home/bob/projects
This requires the parent path, such as /home/bob, to already exist and to be writable by you.
Create missing parent directories with mkdir -p
The -p option creates missing parent directories as needed. This command creates projects, linux, and notes if they do not already exist:
mkdir -p ~/projects/linux/notes
The tilde, ~, is commonly expanded by the shell to your home directory. Without -p, mkdir fails if an intermediate parent does not exist.
Names containing spaces
The shell normally treats spaces as separators between arguments. Quote a path containing spaces:
mkdir "Project Notes"
mkdir "work files/archive"
You can also escape each space with a backslash:
mkdir Project\ Notes
Moving and renaming directories with mv
The mv command moves a directory to another location. Its general form is:
mv SOURCE DESTINATION
For example, this command moves my_folder so that its destination path is /home/bob/my_folder1:
mv my_folder /home/bob/my_folder1
If /home/bob/my_folder1 does not already exist, the directory is moved and takes the new name my_folder1. The source directory's contents move with it.
If the destination already exists as a directory, mv usually places the source inside it instead. For example:
mv my_folder /home/bob
This normally results in /home/bob/my_folder. Always check the destination before moving, because an existing directory can produce unexpected nesting.
ls -ld /home/bob /home/bob/my_folder1
Rename a directory
Renaming is a move within the same parent directory. To rename drafts to completed:
mv drafts completed
Both names refer to directories in the current working directory. You can also use complete paths when the directories are elsewhere.
Deleting empty directories with rmdir
Use rmdir when you want to remove a directory only if it is empty:
rmdir temporary
rmdir refuses to remove a directory that contains files or subdirectories. This safety behavior is useful when your intention is specifically to remove an empty directory.
If rmdir reports that a directory is not empty, inspect it first:
ls -la logs
The -a option includes hidden entries, and -l displays detailed information. Hidden entries often begin with a dot, so a directory that appears empty with ordinary ls may still contain something.
After inspection, you can remove or relocate the contents and then use rmdir, or deliberately remove the entire directory tree with a recursive rm command.
Deleting directory trees with rm -r
A recursive operation applies to a directory and all nested contents beneath it. The rm -r or equivalent rm -R option recursively removes the named directory and everything below it:
rm -r old_project
This is destructive. It removes files, subdirectories, and the named directory itself, and it normally does not provide a simple undo mechanism. Review the path carefully before pressing Enter.
Use confirmation options
The -i option asks for confirmation during removal:
rm -ri old_project
This is useful for a disposable test directory whose contents you want to review as removal proceeds. For larger removals, rm -I provides a less repetitive safeguard by requesting confirmation for a substantial recursive operation rather than asking about every single item. It is still important to inspect the target first.
Be especially cautious with rm -rf. The -f option forces removal and suppresses many prompts and errors. Do not use it unless the target path has been carefully verified and permanent deletion is definitely intended.
Safe directory-management workflow
- Run
pwdbefore commands that use relative paths. - Use
lsto inspect the current location and confirm names. - Use
ls -la DIRECTORYwhen hidden entries may be present. - Review both source and destination paths before using
mv. - Prefer
rmdirwhen you want Linux to enforce that a directory is empty. - For recursive deletion, list the contents first and use
rm -riwhen interactive confirmation is appropriate. - Use tab completion in the shell where available. It reduces typing mistakes and can help reveal the exact spelling of a path.
Linux directory management commands
| Command | Purpose | Typical syntax | Important behavior or caution |
|---|---|---|---|
mkdir | Create a directory | mkdir projects | Parent directories must already exist unless -p is used. |
mkdir -p | Create a directory and missing parents | mkdir -p ~/projects/linux/notes | Creates each missing directory in the path. |
mv | Move or rename a directory | mv SOURCE DESTINATION | An existing destination directory may cause nesting. |
rmdir | Remove an empty directory | rmdir temporary | Refuses to remove directories containing entries. |
rm -r | Recursively remove a directory tree | rm -r old_project | Destructive; removes the directory and all contents. |
rm -ri | Recursively remove with confirmations | rm -ri old_project | Provides interactive prompts during removal. |
pwd | Show the current working directory | pwd | Use it before relying on relative paths. |
ls -la | List ordinary and hidden entries | ls -la logs | Useful for checking a directory before deletion. |
Choosing a deletion method
| Directory state | Recommended command or action | Why |
|---|---|---|
| Known to be empty | rmdir DIRECTORY | It removes only an empty directory and prevents accidental content deletion. |
| Contains files or subdirectories | Remove or relocate the contents, or use a carefully reviewed rm -r | The directory cannot be removed with rmdir; recursive removal deletes its entire tree. |
| Contents need review before removal | ls -la DIRECTORY, then consider rm -ri DIRECTORY | Inspection and confirmation reduce the chance of deleting the wrong data. |
| Contents should be preserved | Move the contents or move the directory with mv | Preserves the data instead of permanently deleting it. |
Troubleshooting directory commands
“Directory not empty” from rmdir
The directory contains files, subdirectories, or hidden entries. Run ls -la DIRECTORY, then decide whether to remove or move the contents, or intentionally use recursive removal.
mkdir says a parent path does not exist
One or more parent directories are missing. Create them first, or use mkdir -p for the complete nested path.
mv creates unexpected nesting
The destination already exists as a directory, so the source was placed inside it. Check the destination with ls and specify the exact final pathname you want.
The command cannot find the directory
The path may be misspelled, or a relative path may be interpreted from the wrong current working directory. Use pwd, inspect nearby entries with ls, and use an absolute path when appropriate.
A name with spaces is split into multiple arguments
Quote the path, such as "Project Notes", or escape each space with a backslash.
Permission denied
You may lack the required permissions for the directory or its parent directory. Confirm ownership and permissions, then use an authorized account or approved administrative procedure. Do not bypass permissions without understanding the effect of the command.
Key points
/is the root directory and the top of the Linux filesystem hierarchy.- Absolute paths begin with
/; relative paths start from the current working directory. - Use
mkdirto create directories andmkdir -pfor missing parent directories. - Use
mvto move or rename directories, while checking whether the destination already exists. - Use
rmdirfor empty directories. - Use recursive
rmonly after reviewing the target and accepting that deletion is destructive.
For a related reference, see Manage Directories in Linux.