How to delete folders in Linux: 1. Use the rmdir command to delete empty folders. The syntax is "rmdir [-p] folder name". The option "-p" is used to delete empty directories recursively. 2. Use the rm command to permanently delete the specified folder in the file system. The syntax is "rm -rf folder name"; when using this command to delete files or directories, the system will not generate any prompt message.
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
There are two ways to delete folders in Linux: rmdir command and rm command. Many people are accustomed to using rmdir, but once the directory is not empty, they will fall into deep distress. At this time, they need to use the rm command. Let’s take a look at these two commands.
Linux rmdir command: delete empty directories (empty folders)
mdir (abbreviation for remove empty directories) command is used to delete empty directories Directory, the basic format of this command is:
rmdir [-p] 文件夹名
-p
option is used to recursively delete empty directories.
[Example 1]
[root@localhost ~]#rmdir cangls
It’s that simple, just add the directory name after the command, but whether the command is executed successfully or not depends on Whether the directory to be deleted is an empty directory, because the rmdir command can only delete empty directories.
[Example 2]
By learning the mkdir command, we know that using mkdir -p can create a directory recursively. Similarly, the rmdir command can use the -p option to create a directory recursively. Delete the directory. For example:
[root@localhost ~]# rmdir -p lm/movie/jp/cangls
Note that this method first deletes the lowest level directory (cangls is deleted first), and then deletes the upper-level directories layer by layer. When deleting, you also need to ensure that the directories at all levels are empty directories.
[Example 3]
The rmdir command has a very limited effect because it can only delete empty directories, so once there is content in the directory, an error will be reported. For example:
[root@localhost # mkdir test #建立测试目录 [root@localhost ~]# touch test/boduo [root@localhost ~]# touch test/longze #在测试目录中建立两个文件 [root@localhost ~]# rmdir test rmdir:删除"test"失败:目录非空
This command is relatively "stupid", so it is not commonly used. The rm command is introduced below. Using this command, you can not only delete directories, but also delete files.
Linux rm command: delete files or directories
When the Linux system has been used for a long time, there may be some files that are no longer useful. (i.e. garbage), these files will not only consume valuable hard disk resources, but also reduce the operating efficiency of the system, so they need to be cleaned up in time.
rm is a powerful deletion command that can permanently delete specified files or directories in the file system. When using the rm command to delete a file or directory, the system will not generate any prompt message. The basic format of this command is:
rm[选项] 文件或目录
Options:
-f
: force deletion (force), the opposite of the -i option, use - f, the system will no longer ask, but directly delete the target file or directory.-i
: Just the opposite of -f. Before deleting a file or directory, the system will give a prompt message. Using -i can effectively prevent accidental deletion. Useful files or directories.-r
: Recursive deletion, mainly used to delete directories. It can delete the specified directory and all the content it contains, including all subdirectories and files.
Note that the rm command is a destructive command because the rm command will permanently delete the file or directory. This means that if the file or directory is not deleted, After making a backup, once you delete it using the rm command, it cannot be restored. Therefore, be careful when using the rm command to delete a directory.
【Example 1】Basic usage
If the rm command does not add any options, the default execution is "rm -i file name", that is, That is, before deleting a file, it will ask whether to delete it. For example:
[root@localhost ~]# touch cangls [root@localhost ~]# rm cangls rm:是否删除普通空文件"cangls"?y #删除前会询问是否删除
[Example 2] Delete directory
If you need to delete the directory, you need to use the "-r" option. For example:
[root@localhost ~]# mkdir -p /test/lm/movie/jp #递归建立测试目录 [root@localhost ~]# rm /test rm:无法删除"/test/": 是一个目录 #如果不加"-r"选项,则会报错 [root@localhost ~]# rm -r /test rm:是否进入目录"/test"?y rm:是否进入目录"/test/lm/movie"?y rm:是否删除目录"/test/lm/movie/jp"?y rm:是否删除目录"/test/lm/movie"?y rm:是否删除目录"/test/lm"?y rm:是否删除目录"/test"?y #会分别询问是否进入子目录、是否删除子目录
You will find that if each level of directory and each file needs to be confirmed, it will be a disaster in actual use!
[Example 3] Forced deletion
If there are 10,000 subdirectories or subfiles in the directory to be deleted, then ordinary rm deletion requires at least 10,000 confirmations . Therefore, when actually deleting files, we will choose to force delete. For example:
[root@localhost ~]# mkdir -p /test/lm/movie/jp #重新建立测试目录 [root@localhost ~]# rm -rf /test #强制删除,一了百了
After adding the force function, deletion will become very simple, but it should be noted that data cannot be recovered after force deletion, unless relying on third-party data recovery tools, such as extundelete, etc. But please note that data recovery is difficult to restore complete data. Generally, it is very rare to recover 70%~80%. Therefore, instead of betting on data recovery, it is better to develop good operating habits.
Although the "-rf" option is used to delete directories, no error will be reported when deleting files. Therefore, for ease of use, generally the "-rf" option will be used directly whether deleting files or directories.
Related recommendations: "Linux Video Tutorial"
The above is the detailed content of How to delete a folder in linux. For more information, please follow other related articles on the PHP Chinese website!

Maintenance mode is used for system maintenance and repair, allowing administrators to work in a simplified environment. 1. System Repair: Repair corrupt file system and boot loader. 2. Password reset: reset the root user password. 3. Package management: Install, update or delete software packages. By modifying the GRUB configuration or entering maintenance mode with specific keys, you can safely exit after performing maintenance tasks.

Linux network configuration can be completed through the following steps: 1. Configure the network interface, use the ip command to temporarily set or edit the configuration file persistence settings. 2. Set up a static IP, suitable for devices that require a fixed IP. 3. Manage the firewall and use the iptables or firewalld tools to control network traffic.

Maintenance mode plays a key role in Linux system management, helping to repair, upgrade and configuration changes. 1. Enter maintenance mode. You can select it through the GRUB menu or use the command "sudosystemctlisolaterscue.target". 2. In maintenance mode, you can perform file system repair and system update operations. 3. Advanced usage includes tasks such as resetting the root password. 4. Common errors such as not being able to enter maintenance mode or mount the file system, can be fixed by checking the GRUB configuration and using the fsck command.

The timing and reasons for using Linux maintenance mode: 1) When the system starts up, 2) When performing major system updates or upgrades, 3) When performing file system maintenance. Maintenance mode provides a safe and controlled environment, ensuring operational safety and efficiency, reducing impact on users, and enhancing system security.

Indispensable commands in Linux include: 1.ls: list directory contents; 2.cd: change working directory; 3.mkdir: create a new directory; 4.rm: delete file or directory; 5.cp: copy file or directory; 6.mv: move or rename file or directory. These commands help users manage files and systems efficiently by interacting with the kernel.

In Linux, file and directory management uses ls, cd, mkdir, rm, cp, mv commands, and permission management uses chmod, chown, and chgrp commands. 1. File and directory management commands such as ls-l list detailed information, mkdir-p recursively create directories. 2. Permission management commands such as chmod755file set file permissions, chownuserfile changes file owner, and chgrpgroupfile changes file group. These commands are based on file system structure and user and group systems, and operate and control through system calls and metadata.

MaintenanceModeinLinuxisaspecialbootenvironmentforcriticalsystemmaintenancetasks.Itallowsadministratorstoperformtaskslikeresettingpasswords,repairingfilesystems,andrecoveringfrombootfailuresinaminimalenvironment.ToenterMaintenanceMode,interrupttheboo

The core components of Linux include kernel, file system, shell, user and kernel space, device drivers, and performance optimization and best practices. 1) The kernel is the core of the system, managing hardware, memory and processes. 2) The file system organizes data and supports multiple types such as ext4, Btrfs and XFS. 3) Shell is the command center for users to interact with the system and supports scripting. 4) Separate user space from kernel space to ensure system stability. 5) The device driver connects the hardware to the operating system. 6) Performance optimization includes tuning system configuration and following best practices.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!
