In Linux there are a few commands that work for both files and directories and some that only work for directories. This section discusses the commands that can only work with directories.
There’s not much to creating a new directory in Linux; just use the mkdir command:
The system creates a new directory and assigns it a new inode number.
Removing directories can be tricky, but there’s a reason for that. There are lots of opportunities for bad things to happen when you start deleting directories. Bash tries to protect us from accidental catastrophes as much as possible. The basic command for removing a directory is rmdir:
By default, the rmdir command only works for removing empty directories. Since there is a file in the dir1 directory, the rmdir command refuses to remove it. You can remove nonempty directories using the --ignore-fail-on-non-empty parameter.
You can also use the rm command when handling directories.
If you try using it without parameters, as with files, you’ll be somewhat disappointed:
However, if you really want to remove a directory, you can use the -r parameter to recursively remove the files in the directory, then the directory itself:
While this works, it’s somewhat awkward. Notice that you still must verify every file that gets removed. For a directory with lots of files and subdirectories, this can become tedious.
The ultimate solution for throwing caution to the wind and removing an entire directory, contents and all, is the rm command with both the -r and -f parameters:
That’s it. No warnings, no fanfare, just another shell prompt. This, of course, is an extremely dangerous tool to have, especially if you’re logged in as the root user account. Use it sparingly and only after triple checking to make sure that you’re doing exactly what you want to do.
3.138.120.187