You want to delete a file or directory using the Terminal.
The rm (remove) command will delete a file or directory and its contents. It should be used with extreme caution.
Discussion
Deleting a single file is simple and safe. The following example will delete the file my_file.txt from the home directory:
$ cd ~ $ rm my_file.txt $ ls |
You need to have write permission in the directory within which you are trying to carry out the deletion.
You can also use the * wildcard when deleting files. This example will delete all the files starting with my_file. in the current directory:
$ rm my_file.* |
You could also delete all the files in the directory by typing:
$ rm * |
If you want to recursively delete a directory and all its contents, including any directories that it contains, you can use the -r option:
$ rm -r mydir |
When deleting files from a Terminal window, remember that you do not have the safety net of a recycle bin from which files can be undeleted. Also, generally speaking, you won’t be given the option to confirm, the files will just be immediately deleted. This can be totally devastating if you combine it with the command sudo (Recipe 3.11).
See Also
See also Recipe 3.3
If you are concerned about accidentally deleting files or folders, you can force the rm command to confirm by setting up a command alias (Recipe 3.32).