Linux rmdir command - Delete empty directories

Linux rmdir command: Delete empty directories.

Linux rmdir command Function Description

Use the rmdir command to delete empty directories on a Linux system.

The rmdir command is used to delete empty directories. When a directory is no longer in use, or when disk space has reached the usage limit, it is necessary to delete the directory that has lost its use value. Use the rmdir command to remove one or more empty subdirectories from a directory. This command removes one or more subdirectories from a directory, where dirname is the directory name. If no path is specified in dirname, the directory specified by dirname in the current directory is deleted; if dirname contains a path, the directory in the specified location is deleted. When deleting a directory, you must have write permission to its parent directory.

The subdirectory should be empty before it is deleted. That is, all files in that directory must be all with the rm command. In addition, the current working directory must be above the deleted directory, not the deleted directory itself, and not a subdirectory of the deleted directory.

While it is also possible to recursively delete all files in a directory and the directory itself with the rm command with the -r option, there is a significant danger in doing so.

Linux rmdir command Syntax

rmdir [option] [directory]

The meaning of each option in the command is shown in the table.

Option Description
-p or --parents Recursive directory deletion, when a subdirectory is deleted and its parent directory is empty, it will also be deleted.
-v or -verboes Showing the detailed execution of the command.
--ignore-fail-on-non-empty This option causes the rmdir command to ignore error messages caused by the deletion of non-empty directories.

Linux rmdir command Examples

Delete empty directory aaa-apidemos

rmdir aaa-apidemos

Output:

Linux rmdir command

Also delete the empty directories /root/newdir2 and /root/newdir2/newdir3

[root@rhel ~]# rmdir -p /root/newdir2/newdir3
rmdir: Delete directory "/root" Failed: Directory is not empty

Above command will delete the empty directories /root/newdir2 and /root/newdir2/newdir3, but since the /root directory is not empty, the /root directory cannot be deleted, while the other two directories have been deleted.

Like(1)

Related

Linux Login Logout Command
Linux login commandLinux logout commandLinux nologin commandLinux exit commandLinux sulogin commandLinux rlogin commandLinux poweroff commandLinux ctrlaltdel CommandLinux shutdown commandLinux halt commandLinux reboot commandLinux init commandLinux runlevel commandLinux telinit command
Linux File Management Command
Linux cat commandLinux tac commandLinux nl commandLinux more commandLinux less commandLinux head commandLinux tail commandLinux rev commandLinux fold commandLinux fmt commandLinux expand commandLinux pr commandLinux sort commandLinux uniq commandLinux cut commandLinux comm commandLinux diff commandLinux join commandLinux diff3 commandLinux cmp commandLinux colrm commandLinux paste commandLinux mkdir commandLinux tr commandLinux split commandLinux csplit commandLinux tee commandLinux unexpand commandLinux patch commandLinux awk commandLinux sed commandLinux od commandLinux pwd commandLinux cd commandLinux ls commandLinux dir commandLinux dirs commandLinux touch commandLinux rmdir commandLinux cp commandLinux mv commandLinux rm commandLinux install commandLinux tmpwatch commandLinux file commandLinux du commandLinux wc commandLinux tree commandLinux cksum commandLinux md5sum commandLinux sum commandLinux dirname commandLinux mkfifo Command
Cron Expressions
Cron Expression to Run Every Day at 12 PMUnderstanding Vue Cron ExpressionsUnderstanding JS Cron ExpressionsA Comprehensive Guide to Cron Expressions for Scheduled TasksUnderstanding Linux Cron ExpressionsUnderstanding Quartz Cron ExpressionsCron ExpressionCron Time ExpressionCron Expression ParsingCron Expression: Executing a Task Every SecondCron Expression for Every Minute ExecutionCron Expression to Execute Every 10 MinutesCron Expression: Executing Every HourCron Expression to Execute Once a YearCron Expression: How to Schedule a Task to Run Daily at Midnight?