Updated April 14, 2023
Introduction to Linux Remove Directory and Contents
In the Linux operating system, we are working on the multiuser operating system. Every user like a user account or service account is having a bunch of hierarchical data. The chunk of data is available in multiple grouping hierarchies. It will be very difficult to remove or delete the same nested data. In Linux, we are having the remove directory function. With the help of the same functionality, we can remove the nested content directories. There are different ways to remove the contents from the Linux environment. Generally, we are using the “rm” command or utility to remove the nested contents.
The rm utility was written by Paul Rubin, David MacKenzie, Richard M. Stallman, and Jim Meyering.
Syntax:
rm [ OPTION ] ... INPUT FILE ...
- rm: We can use the “rm” keyword in the syntax or command. It will take different arguments like options, input files. As per the provided arguments, it will remove the input files in recursive mode or the single shot mode.
- OPTIONS: We can provide the different flags as the option that is compatible with the “rm” command.
- INPUT FILE: As per the condition or requirement, we can provide the file or file path to the cat command.
How Linux Remove Directory and Contents works?
As we have said that the Linux is a multi-user support operating system and manages multiple user environments with the data. The remove directory i.e. “rm” utility is used to delete the data or file content single or recursively. As per the requirement, we can use multiple options with the rm command. Once more thing, we need to pass the input file or directory to the rm command. As per the provide arguments i.e. the option and the input file. The rm command will delete the file.
By default, the rm command will not able to delete the directory. To delete the directory, we need to add the options with the rm command and even though we need to have the proper permission to delete the directory or the content.
Below are the lists of options that are compatible with the cat command.
Sr No | Option | Description |
1 | -f, –force | It will never ever prompt any update or input. It will directly process the rm command. We can also use it when we need to delete or remove the data forcefully. |
2 | -i | It will help to come with the prompt information before removing any contents |
3 | -I | It is similar to the previous “-i” option but it will only prompt once before removing more than three files. When we are removing the files in the recursive mode we can also use the same functionality. |
4 | –interactive[=WHEN] | It will help to use the prompt option according to the WHEN condition. It will be used with the “-I” option as well as with “-i”. The option without when then the prompt will be always. |
5 | –one-file-system | When we need to remove the hierarchy content recursively and need to skip any directory from the file system. The file system is different from that of the corresponding command line argument. |
6 | –no-preserve-root | It will not treat ‘/’, especially in the rm environment. |
7 | –preserve-root | It will not remove the ‘/’ (it is the default) |
8 | -r, -R, –recursive | It will help to remove the nested directories or the multiple contents or files. The file removes mode will be recursive. |
9 | -d, –dir | In the rm command, we are not able to remove the directly without an option. We can use the “-d” option to remove the empty directories |
10 | -v, –verbose | It will help to print the information i.e. what is going on and what is the status of it. |
11 | –help | It will give the compatible command information on the screen and exit the command window. |
12 | –version | It will print the version information of the rm command and exit the print window |
Examples to Implement Linux Remove Directory and Contents
Following are the examples as given below:
1. Linux Remove Directory – Interactive Mode
In the Linux environment, we are able to delete multiple files in the interactive mode.
Command:
rm -i file*
Explanation: As per the above command, we are having multiple files at the “/root/data” location. With the help of “-i” option, we are able to delete the multiple files in interactive mode.
Output:
2. Linux Remove Directory and Contents – With Recursive Mode
While removing the multiple directories or files, the rm tool will always as for the action. To avoid this, we need to use a recursive mode.
Command:
rm -rf *
Explanation: As per the above command, we are using the “-r” option with the remove command. It will help to delete all the files or directory recursive.
Output:
3. Linux Remove Directory and Contents – Remove Directory
In rm utility, we are having the functionality to remove the directory. To remove the empty directory, we need to use the “-d” option.
Command:
rm -d data1
Explanation: We are finding the files into “/root/data” location. To remove the empty directory, we need to use the “-d” option with the rm command.
Output:
4. Linux Remove Directory and Contents – With “-f” Option
In Linux, we are having the functionality to delete or remove the files/directories forcefully.
Note: While running the “-f” option with the rm command or tool. We need to use it very carefully because it will not come up with the popup window. It will delete all the contents. It will be very difficult to recover.
Command:
rm -df *
Explanation: As per the above command, we are using the “-f” option with the rm command. It will delete all the files or directories available in it.
Output:
Conclusion
We have seen the uncut concept of “Linux Remove Directory and Contents” with the proper example, explanation, and command with different outputs. As per the requirement, we can use the different options with the rm command. It will very useful to remove multiple contents at the same time.
Recommended Articles
We hope that this EDUCBA information on “Linux Remove Directory and Contents” was beneficial to you. You can view EDUCBA’s recommended articles for more information.