- Christian Vasquez
- No Comments
Table of Contents
Directory management on Linux is an easy task with the use of mkdir
, which creates directories and rmdir
which removes them. mkdir
& rmdir
are essential commands to know, especially if you want to get comfortable with the Linux CLI. You don’t want to be GUI jockey, now…
Create a directory in Linux
Creating a directory is a simple affair, simply call mkdir
followed by the name you wish to give it. This will create a directory in the present working directory.
mkdir mydir
You can enter any number of directories alongside each other, they just need to be separated by a space and they will be created together.
mkdir mydir1 mydir2 mydir3
mkdir options
mkdir
can also create nested directories, it just requires the path and passing the -p
option. -p
tells mkdir
to create the parent directory if it does not exist. For example:
mkdir my/nested/dir -p
If you attempt the above without
-p
, you’ll get an error stating thatmkdir: cannot create directory ‘my/nested/dir’: No such file or directory
.
Another option of note is -v
, which makes mkdir
output verbose information about the current operation, useful for troubleshooting if your output has errors for any reason.
mkdir another/nested/dir -p -v
Remove a directory in Linux
To get rid of unwanted directories we can use rmdir
.
rmdir mydir
Just like with mkdir we can enter many names to be removed, just separate them by a space.
rmdir mydir1 mydir2 mydir3
Note:
rmdir
will only work on empty directories. For non-empty directories, you will need to userm -rf
to remove the directory its contents. For more info onrm
check out our post, linked here.
rmdir options
rmdir
also uses -p
and -v
options and they work identically, only this time -p
will remove the parent directory (but only if you provide the file path).
rmdir another/nested/dir -pv
Using wildcards with rmdir
rmdir
can make use of the wildcard *
, which allows for batch removal operations. For removing a nested series of directories with a depth of 3 we could do this;
rmdir */*/* -pv
As noted above if the directory has anything other than nested directories, then rmdir
will fail. Below you can see an example file structure, and the resulting failure.
tree.└── another └── nested └── dir └── somefile.txtrmdir */*/* -pv rmdir: removing directory, 'another/nested/dir'rmdir: failed to remove 'another/nested/dir': Directory not empty
This version of the command really only works if you have an idea of how the directories are nested and they have to be empty(in order to type out the right number of wildcards), so this command is very situational.
Conclusion
mkdir
and rmdir
are absolute necessities when it comes to basic file operations on a Linux server/system. mkdir
more so than rmdir
, as rmdir
is too situational, and rm
does its job far better. Either way directory management via the CLI is not so tough once you got these commands under your belt. Thanks for reading and checkout our other posts for more command line literacy.
Manage Users in Linux Using USERADD and USERDEL
Christian VasquezFebruary 24, 2023
From time to time (more so if you’re a homelabber or sysadmin) you’ll have to
View Post
What Is The Linux TOUCH Command And How To Master It
Christian VasquezMarch 10, 2023
Learn all about the linux touch command in our comprehensive guide. Discover how to create,
View Post
How To Install and Use UFW Firewall In Linux
Boyd GordonJanuary 12, 2022
UFW, the Uncomplicated Firewall, is as true as a command line utility can be. Posing
View Post
Using Azuri to play Music from Azuracast in Discord!
Christian VasquezJuly 14, 2022
As I mentioned in the last article, Azuri is a discord bot that allows Azuracast’
View Post
Use The SUDO Command On Linux Properly!
Christian VasquezMarch 24, 2023
The SUDO command on Linux is designed to provide temporary administrative privileges, which adds an
View Post
Python Development Crash Course with PyCharm and Kivy
John BorrelliJanuary 21, 2022
Okay, you just installed PyCharm as your IDE and you want to get started. We
View Post
Working With Linux SWAP Space The Easy Way
Omar MartinezMarch 31, 2023
Linux swap space is a designated area of a hard disk used by the operating
View Post
The BEST BASH Cheatsheet you could ever ask for!
Christian VasquezJuly 29, 2022
Are you new to Shell Scripting? Or maybe you forget how to use certain syntax?
View Post
Top 10 Google Drive Alternatives That Care About Privacy
John BorrelliJanuary 27, 2022
We are looking at free Google Drive Alternatives that provide desktop synchronization with your Linux
View Post
Greatly Customizing Your bashrc File In Linux!
Boyd GordonApril 7, 2022
Arguably the most hardworking part of your Linux system. Let’s have a look at customizing
View Post
Optimize Your Fail2ban Config To Protect Linux
Is your server or website getting attacked? Slow down those hackers by protecting your server and website using Fail2ban!
View Post
Boyd GordonApril 21, 2022
How To Create And Remove Directories in Linux With RMDIR and MKDIR
Creating and removing directories in Linux can be done easily using the terminal and basic commands such as mkdir and rmdir, saving time and simplifying
View Post
Christian VasquezFebruary 28, 2023
You must be logged in to post a comment.