Working with directories is a fundamental part of navigating and organizing your Linux system. Whether you’re setting up project structures or managing application files, knowing how to create and remove directories efficiently is an essential skill. In this guide, we’ll explore the different ways you can create, manage, and remove directories from the command line.
If you’re just getting started with ViM and want to level up your workflow, you might also enjoy our guide on splitting the screen in ViM. Combined with efficient file and directory manipulation, this can dramatically improve your productivity.
You may also find it useful to learn how to open files and directories from the terminal, how to count how many times a string appears in a file, how to install the latest kernel on Ubuntu, or how to manage your shell environment.
Creating Directories
1. Create Multiple Directories at Once
You can create several directories with a single mkdir command:
mkdir dir1 dir2 dir3
ls -l
drwxrwxr-x 2 geekvater geekvater 4096 ... dir1
drwxrwxr-x 2 geekvater geekvater 4096 ... dir2
drwxrwxr-x 2 geekvater geekvater 4096 ... dir3
2. Directories with Spaces in Their Names
Use quotes to create directories with spaces:
mkdir "Anakin Skywalker" "Han Solo" "Jabba The Hut"
ls -l
drwxrwxr-x 2 geekvater geekvater 4096 ... Anakin Skywalker
drwxrwxr-x 2 geekvater geekvater 4096 ... Han Solo
drwxrwxr-x 2 geekvater geekvater 4096 ... Jabba The Hut
3. Creating Directory Trees with -p
To create nested directories:
mkdir -p ~/dir1/dir2/dir3
tree -L 1 ~/dir1/
/home/geekvater/dir1/
└── dir2
└── dir3
You can simplify directory creation with an alias:
alias mkdir="mkdir -p"
To make this alias persistent:
- For bash, add the line to your
~/.bashrcfile:echo "alias mkdir='mkdir -p'" >> ~/.bashrc && source ~/.bashrc - For zsh, add the line to your
~/.zshrcfile:echo "alias mkdir='mkdir -p'" >> ~/.zshrc && source ~/.zshrc
You can read more about customizing your shell environment in this detailed guide.
4. Create Directories with Custom Permissions
Use -m to set permissions at creation:
mkdir -m 740 secure_dir
ls -ld secure_dir
drwxr----- 2 geekvater geekvater 4096 ... secure_dir
For more on managing file permissions and output control, see our tips on redirecting stdout and stderr or how to open files from the terminal.
Removing Directories
5. Removing Empty Directories
To remove a directory that contains no files:
rmdir mike
ls -l mike
ls: cannot access mike: No such file or directory
6. Removing Non-Empty Directories
Use rm -r for recursive removal:
rm -r Aa
ls -l Aa
ls: cannot access Aa: No such file or directory
This command will delete the directory and all of its contents.
Conclusion
Managing directories from the command line is a vital skill that every Linux user should master. The mkdir, rmdir, and rm -r commands are powerful tools that give you complete control over your filesystem layout.
Whether you’re scripting complex automation tasks or just keeping your home directory clean, these directory commands can make your life much easier. Add to that aliases, permission flags, and integration with other utilities, and you’ve got a solid foundation for any Linux workflow.






Pingback: How to Change a User’s Default Shell in Linux - iDoLinux
Pingback: How to Change a User’s Default Shell in Linux - Open Source Software News