Manage Directories in Linux
Learn how to create, move, rename, and safely remove Linux directories with mkdir, mv, rmdir, and rm.
Linux directory management means creating locations for files, moving or renaming those locations, and removing them when they are no longer needed. This lesson uses the command line and covers mkdir, mv, rmdir, and recursive rm.
Understand the Linux Directory Hierarchy
A directory is a filesystem object used to contain and organize files and other directories. Linux uses one filesystem hierarchy shaped like a tree. The top of this tree is the root directory, written as /.
Every file and directory descends from /. A directory immediately above another directory is its parent directory. A directory stored inside another directory is a subdirectory, also called a child directory.
/
├── home/
│ └── bob/
│ ├── projects/
│ └── notes/
├── etc/
└── var/
In this example, /home is the parent of /home/bob, and projects is a subdirectory of /home/bob.
Absolute and relative paths
A path is a textual address identifying a file or directory.
- An absolute path begins with
/and starts at the root directory. For example,/home/bob/projects. - A relative path does not begin with
/. The shell interprets it from the current working directory, which is the directory where the shell session is currently operating.
pwd
/home/bob
ls projects
ls /home/bob/projects
Here, projects is relative to /home/bob, while /home/bob/projects identifies the same location absolutely.
Create Directories with mkdir
The mkdir command creates directories. Its basic syntax is:
mkdir DIRECTORY
When you provide only a directory name, Linux creates it in the current working directory.
mkdir projects
ls
To create a directory at a particular location, provide a path:
mkdir /home/bob/archive
This creates archive inside /home/bob, assuming the parent directory exists and you have permission to write there.
Create nested paths with -p
Use -p, meaning create parent directories as needed, when several directories in a path may not exist.
mkdir -p ~/projects/linux/notes
This creates each missing directory: projects, linux, and notes. Without -p, mkdir normally fails if a required parent directory does not already exist.
Without -p, attempting to create a directory that already exists produces an error. With -p, an existing directory is acceptable, provided the path can be created or already represents directories.
mkdir projects
mkdir projects
# mkdir: cannot create directory 'projects': File exists
mkdir -p projects
# No error when projects is already a directory
Move and Rename Directories with mv
The mv command accepts a source followed by a destination:
mv SOURCE DESTINATION
It can move a directory to another location or rename it. Moving normally preserves the directory's contents.
Move a directory into an existing directory
mv my_folder /home/bob/
If /home/bob/ already exists as a directory, the result is usually /home/bob/my_folder. The source directory becomes a child of the destination directory.
Move to a new pathname
mv my_folder /home/bob/my_folder1
If /home/bob/my_folder1 does not already exist, this moves my_folder to /home/bob and gives it the final name my_folder1.
A rename is simply a move within the same parent directory:
mv drafts finished_drafts
This changes drafts to finished_drafts in the current directory.
Remove Empty Directories with rmdir
The rmdir command deletes directories only when they contain no files or subdirectories.
rmdir old_notes
If old_notes contains anything, the command fails with an error indicating that the directory is not empty. Hidden files count as contents too.
ls -la old_notes
rmdir old_notes
Use rmdir -p to remove a directory and empty parent directories in the specified chain:
rmdir -p projects/linux/empty_notes
This removes empty_notes, then linux if it is empty, then projects if it is also empty. It does not remove a parent that still contains other entries.
Remove Directory Trees with rm
The rm command deletes files. With the recursive option -r, it can delete a directory and everything beneath it.
rm -r old_project
Recursive means that the operation applies to the target directory, its files, its subdirectories, and all deeper contents. This is substantially more destructive than rmdir.
For a confirmation-oriented workflow, use -i or -I:
rm -ri DIRECTORYasks for confirmation during recursive removal.rm -rI DIRECTORYprovides an additional confirmation safeguard before a potentially large removal.
rm -rI old_project
The -f option forces removal and suppresses many prompts and errors. Combining it with recursive deletion, as in rm -rf DIRECTORY, is especially risky because it can remove a directory tree without asking for confirmation.
Safe Directory-Management Workflow
- Run
pwdto confirm where relative paths will be interpreted. - Run
lsorls -lato inspect nearby entries and hidden contents. - Prefer an explicit path when the location matters.
- Use tab completion to complete names and reduce spelling errors.
- Quote names containing spaces or special characters.
- Avoid broad wildcards and ambiguous paths in destructive commands.
- Use verbose output where supported and useful, such as
mv -vorrm -rvi, to see what changed.
Spaces and special characters
The shell normally splits unquoted spaces into separate arguments. Quote a directory name containing spaces:
mkdir "Project Notes"
ls -ld "Project Notes"
mv "Project Notes" archived_notes
You can also escape a space with a backslash, as in mkdir Project\ Notes. Quoting is often easier to read and safer for longer paths.
Command Reference
| Command | Primary purpose | Basic syntax | Important options | Key safety consideration |
|---|---|---|---|---|
mkdir | Create directories | mkdir DIRECTORY | -p creates missing parents | Check whether the pathname already exists |
mv | Move or rename directories | mv SOURCE DESTINATION | -v shows changes | An existing destination directory receives the source inside it |
rmdir | Delete empty directories | rmdir DIRECTORY | -p removes empty parents | It cannot remove non-empty directories |
rm -r | Recursively delete a directory tree | rm -r DIRECTORY | -i, -I, and -f | Review the exact path; deletion is difficult to undo |
Choosing a Directory Deletion Command
| Situation | Recommended command | What happens | Risk level |
|---|---|---|---|
| Deleting an empty directory | rmdir DIRECTORY | Deletes only the empty directory | Low |
| Deleting a non-empty directory after reviewing contents | rm -rI DIRECTORY | Recursively deletes the tree with an extra confirmation safeguard | High |
| Deleting nested empty directories | rmdir -p PATH | Removes the empty target and empty parents in the chain | Low to moderate |
| Insufficient permissions | Inspect with ls -ld DIRECTORY | Shows ownership and permission information before requesting authorized access | Do not bypass permissions unnecessarily |
Troubleshooting Directory Commands
“Directory not empty” from rmdir
The directory contains files, hidden files, or nested directories. Inspect everything with:
ls -la DIRECTORY
Remove or relocate the contents first, or use a carefully reviewed recursive rm -r only when deleting the entire tree is intended.
“File exists” from mkdir
A file or directory already uses the requested pathname. Check it with:
ls -ld PATH
Choose another name, use the existing directory, or use mkdir -p when an already-existing directory is acceptable.
mv places a directory somewhere unexpected
The destination probably already existed as a directory, so the source was moved inside it. Check first:
ls -ld DESTINATION
Specify the desired final pathname explicitly when moving or renaming.
Permission denied
Directory operations can fail when you lack write and execute permission on the parent directory, or when the target is protected. Inspect permissions and ownership:
ls -ld DIRECTORY
Use an authorized account or request the necessary access rather than bypassing permissions unnecessarily.
Path not found or names with spaces
If a target cannot be found, the path may be misspelled, the current directory may differ from what you expect, or the object may have moved. Run pwd, inspect with ls, and use an absolute path when location is ambiguous. For spaces, quote the complete path or escape each space.
Practical Practice Sequence
pwd
mkdir -p ~/directory-practice/work/drafts
ls -la ~/directory-practice/work
mv ~/directory-practice/work/drafts ~/directory-practice/work/finished_drafts
ls -la ~/directory-practice/work
rmdir ~/directory-practice/work/finished_drafts
rmdir -p ~/directory-practice/work
The final rmdir commands succeed only because the practice directories are empty. Always adapt the paths to a location you own and inspect before deleting.
For related concepts, see File Structure in Linux, Bourne Again Shell Bash, and Show the Full Path of Shell Commands.