The rmdir command is used to remove empty directories in Linux and other Unix-like operating systems. Often referred to as a folder in the Macintosh and Microsoft Windows operating systems, a directory in Linux is merely a special type of file that contains a list of file names and the corresponding inodes for each file and directory that it appears (to the user) to contain. An inode is a data structure (i.e., a way of storing data so that it can be used efficiently) that stores all the information about a file except its name and its actual data. The syntax for rmdir is
When used without any options, rm will delete any empty directories whose names are supplied as arguments (i.e., inputs) regardless of whether such directories have write permission or not. Thus, for example, the following command would remove two empty directories named dir1 and dir2 that are located in the current directory (i.e., the directory in which the user is currently working):
The ability to remove only empty directories is a built-in safeguard that helps prevent the accidental loss of data. This is important because once deleted, it is extremely difficult or impossible to recover deleted data on Unix-like operating systems1. The -p (i.e., parents) option tells rmdir to remove the parent directories of the specified directory if each successive parent directory will, in turn, become empty and if each parent directory has write permission. Thus, for example, the following would remove dir5, dir4 and dir3 if dir5 were empty, dir4 only contained dir5 and dir3 only contained dir4 (which, in turn, contained dir5):
This provides a nice symmetry with the -p option of the mkdir command, which is used to create directories. Thus, the above set of nested directories could be easily created with the following:
In contrast to the rm command, which is used to delete both files and directories, there is no -r option for rmdir. at least on the GNU version that is standard on Linux. That option allows rm to recursively delete a directory by first deleting all of its contents, beginning with those in the lowest levels of subdirectories. Thus, if a user wants to remove an entire directory structure, it is usually most efficient to use rm with its -r option rather than trying to first remove the contents of each directory, its subdirectories, etc. Three options that rmdir shares with rm are -v (i.e., verbose), which provides additional information about what is happening, --help, which provides basic documentation about rmdir, and --version, which tells the version of rmdir that is currently in use. Some differences exist among the various versions of rmdir, so it is always wise to read the documentation for the particular system.
________
Created January 12, 2006. |