Installation guide

Chapter 12. Managing Files and Directories 131
12.4.4. Deleting Files and Directories
You learned about creating files with the touch command, and you created the directory tigger
using mkdir.
Now you need to learn how to delete files and directories. Deleting files and directories with the
rm command is a straightforward process. See the rm man page for more information. Options for
removing files and directories include:
-i interactive. Prompts you to confirm the deletion. This option can stop you from deleting a
file by mistake.
-f — force. Overrides interactive mode and removes the file(s) without prompting. This might not
be a good idea, unless you know exactly what you are doing.
-v — verbose. Shows a list of files as they are being removed.
-r — recursive. Will delete a directory and all files and subdirectories it contains.
To delete the file piglet.txt with the rm command:
rm piglet.txt
Warning
Once a file or directory is removed with the rm command, it is gone permanently and cannot be
returned.
Use the -i (interactive) option to give you a second chance to think about whether or not you really
want to delete the file.
[newuser@localhost newuser]$ rm -i piglet.txt
rm: remove ’piglet.txt’?
You can also delete files using the wildcard *, but be careful, because you can easily delete files you
did not intend to throw away.
To remove a file using a wildcard, you would type:
rm pig*
The above command will remove all files in the directory which start with the letters pig.
You can also remove multiple files using the rm command. For example:
rm piglet.txt sneakers.txt
You can use rmdir to remove a directory (rmdir foo, for example), but only if the directory is
empty. To remove directories with rm, you must specify the -r option.
For example, if you want to recursively remove the directory tigger you would type:
rm -r tigger
If you want to combine options, such as forcing a recursive deletion, you can type:
rm -rf tigger