Rmdir

From Unix SME

Command:

    rmdir


Description:

    The command rmdir is used to remove a directory that is specified by the directory parameter.
    Rmdir only removes directories that are empty and you must have permission to write in its 
    parent directory to do so.


Syn/Syp:

    rmdir  [OPTION]...DIRECTORY... 

Options:

    -p or --parents
            
  • Use -p to remove a directory and all of its ancestors.
  • Parent directories must be empty and the user must have write permission in the parent directories before removal.
    -v or --verbose
  • Prints a description of every process that is completed
    -ignore-fail-on-non-empty
  • This option does not report a failure that occurs if the directory chosen is non-empty
    -version
  • Displays the version information and exits

Use Case:

    Use rmdir when removing a directory that we expect to be empty, 
    so that it will fail if there are still files in it.

Examples:

Example 1 -

  • To remove a single empty directory, use the following syntax:
    rmdir emptyDirectory

Example 2 -

  • To remove multiple directories, use the following syntax:
    rmdir mydir1 mydir2 mydir3

Example 3 -

  • To remove a directory and all of its subdirectories, use the following syntax:
    rmdir -p mydir1/myfolder1/myfile1

Example 4 -

  • To display a message with a description of the action you entered into the command line , use the following syntax:
    rmdir -v mydir1

Example 5 -

  • To remove multiple directories that contain the same expression in their name, use the following syntax:
    rmdir -v mydir*
    rmdir: removing directory, 'mydir1'
    rmdir: removing directory, 'mydir2'
    rmdir: removing directory, 'mydir3'

Example 6 -

  • For this example, mydir1 will contain an empty subdirectory myfolder1. To remove the parent directory and subdirectory of that parent without -p option, use the following syntax:
    rmdir -v mydir1/myfolder1 mydir1
    
    

Example 7 -

  • This example shows what an error message looks like when trying to use rmdir on a non-empty directory:
    rmdir notEmptyDirectory
    rmdir: failed to remove ‘notEmptyDirectory’: Directory not empty

Example 8 -

  • To make it so there is no error message when trying to remove a non-empty directory, use the following syntax:
    rmdir --ignore-fail-on-non-empty mydir1
    ls
    (output)
    mydir1

Example 9 -

  • To get to the manual page for the rmdir command and to exit the manual use the q key:
    man rmdir

Example 10 -

  • Alternatively, you can get more information on the command by:
    rmdir --help