When
you are finished with a directory, you should remove it to keep
your file structure neat and organized. There are two command that
can be used to remove files and directories:
rm
file_name(s)is
used to remove files.
rmdir directory_name(s)
is used to remove empty directories.
rm -r (or
-R)directory_name(s)is
used to remove directories that are not empty (it will also delete
all of the files in the directory).
Tip:
Using just the -r
or (-R) option
can be dangerous, because it removes everything. To protect yourself,
use the -ir
(or -iR) option
instead. The -i option
prompts you for verification before each directory and all of
its contents are removed.
Examples
Use rm
with no options to remove file.
$ ls
ebony ivory
$ rm ebony ivory
$ ls
$
Use the
rmdir command
to delete empty directories.
$ rmdir /tmp/piano
$
If you try
to remove a directory that is not empty with rmdir,
the command will fail.
$ ls
$ Schroeder
$ rmdir Schroeder
rmdir: Schroeder: Directory not empty
$
Use the rm
-r (recursive) option to remove a directory that is not
empty.
$ rm -r Schroeder
$
Use the rm
-ir (recursive)
option to remove a directory that is not empty and to verify which
directories to delete.
$ rm -ir Woodstock
rm: descend into directory Woodstock? y
rm: remove Woodstock/bird? y
rm: remove directory Woodstock/yellow? y
rm: descend into directory Woodstock/Hero? y
rm: remove Woodstock/Hero/flight? y
rm: remove directory Woodstock/Hero? y
rm: remove directory Woodstock? y
$