Linuxopsys
Linuxopsys

@linuxopsys

17 Tweets 3 reads Jul 05, 2022
Linux basics - managing directories(creating & deleting)🧵↓
You will regularly need to create and delete directories on the Linux system as a system administrator. The mkdir, rm, and rmdir commands make this possible.
Making Directories🐧
Creating a new directory in Linux is simple; simply use the mkdir command. The mkdir command accepts only one parameter: the name of the directory you want to create.
This will create a directory with the name projects in the current working directory
Instead of only supplying the directory name, you can also specify the path to where you want the new directory to be created.
The preceding example will create the srcs directory within the projects directory; however, the parent directory (projects) must already exist for this to work; otherwise, an error will occur.
Create multiple directories🐧
You can easily create many directories with a single command, as demonstrated below:
Deleting directories🐧
Of course, once you've created anything, you'll need to know how to remove it. This is very beneficial if you have a directory taking too much of your system space or you accidentally created a directory in the wrong location.
Removing directories in Linux can be tricky. When you begin deleting directories, there are numerous opportunities for bad things to occur. The Linux shell makes every effort to keep us safe from unintentional disasters.
The most basic command for removing a directory is rmdir, which only deletes empty folders:
As previously stated, the rmdir command deletes empty directories. What if you want to delete directories that contain files? This is where the remove command comes in.
To remove non-empty directories, use the rm command with the -r (-R) and -f options.
-r, -R, --recursive remove directories and their contents recursively
-f , --force ignore nonexistent files and arguments, never prompt
Note 💡
The -r and -R parameters for the rm command have the same effect — it recursively traverses the directory removing files.
As previously stated, deleting directories can be tricky if you don't know what you're doing. The above command is extremely dangerous; therefore, use it sparingly and only after triple-checking that you're doing exactly what you want!
You can also force the rm command to prompt you before deleting anything by using the -i option:
This allows you to double-check each file that needs to be deleted before deleting it.
You can also use the -d option with the rm command to remove empty directories, which is equivalent to the rmdir command. This allows you to double-check that the directory is empty before deleting it.
That's it for today's thread.
Thank you taking your time to read it.
If you enjoyed this thread, follow us @linuxopsys for future Linux posts, which we will be posting on a daily basis.

Loading suggestions...