Top Linux Commands for Managing Directories
This post covers the essential Linux commands for managing directories, including mkdir, mv, rm, and rmdir, with practical terminal examples. Written for beginners working toward the CompTIA Linux+ exam, it explains how to create nested directory structures, move and rename directories, and safely
One of the first practical skills you need as a Linux administrator is the ability to navigate and manage directories from the command line. Whether you are organizing project files, setting up a server environment, or just getting comfortable with the terminal, knowing how to create, move, and remove directories is foundational. This post walks you through the most important commands for Linux directory management, with real examples you can try right now.
Creating Directories with mkdir
The mkdir command (short for "make directory") is your starting point. At its simplest, you pass it a directory name and it creates that directory in your current location.
mkdir projectsThat creates a directory called projects wherever you currently are. But what if you want to create a nested structure all at once? Use the -p flag, which stands for "parents." This tells mkdir to create any missing parent directories along the way.
mkdir -p projects/webserver/configsWithout -p, this command would fail if projects or webserver did not already exist. With it, Linux creates the entire path in one shot. This is especially useful when scripting or setting up directory structures for applications.
You can also create multiple directories at the same level in a single command:
mkdir logs backups tempListing Directory Contents with ls
Before moving or removing anything, you need to see what is there. The ls command lists directory contents. Add -l for a long listing format and -a to show hidden files (those starting with a dot).
ls -la /home/user/projectsSample output:
total 20
drwxr-xr-x 5 user user 4096 Jun 1 10:00 .
drwxr-xr-x 8 user user 4096 Jun 1 09:55 ..
drwxr-xr-x 2 user user 4096 Jun 1 10:00 backups
drwxr-xr-x 3 user user 4096 Jun 1 10:00 webserver
drwxr-xr-x 2 user user 4096 Jun 1 10:00 tempThe d at the start of each permission string tells you these entries are directories, not files.
Moving and Renaming Directories with mv
The mv command handles two jobs: moving a directory to a new location and renaming a directory. Linux treats both operations the same way.
To rename a directory:
mv temp temporaryTo move a directory into another directory:
mv backups /var/archive/This moves the backups directory (and everything inside it) into /var/archive/. If the destination directory does not exist, mv will rename the source to that name instead of moving it inside. Always double-check the destination before running mv on directories with important data.
Removing Directories with rm and rmdir
Here is where things require extra care. Linux gives you two tools for removing directories:
- rmdir: removes an empty directory only. It will refuse to delete anything that still contains files or subdirectories.
- rm -r: recursively removes a directory and all of its contents.
To remove an empty directory:
rmdir temporaryTo remove a directory and everything inside it:
rm -r projects/webserverYou will sometimes see the -f flag added, which suppresses confirmation prompts. The combination rm -rf is powerful and permanent. There is no recycle bin in Linux. Once you run it, the data is gone. Always confirm your path before pressing Enter, especially when running as root.
Checking Where You Are with pwd
It sounds simple, but knowing your current location in the filesystem prevents costly mistakes. The pwd command (print working directory) tells you exactly where you are.
pwd/home/user/projectsMake it a habit to run pwd before executing any destructive command.
What's Next
Now that you can create, move, and remove directories confidently, the next step is understanding file permissions. Knowing who can read, write, and execute files inside those directories is just as important as knowing how to create them. We will cover chmod, chown, and Linux permission notation in the next post in this series.
If you want a solid study companion for your Linux+ exam journey, check out the CompTIA Linux+ Study Guide by Richard Blum, which covers these foundational topics and much more in structured detail.
Tools and resources for this topic
- CompTIA Linux+ Study Guide — Comprehensive Linux+ exam preparation covering system administration, security, and scripting.