The rm command
The rm command is used to delete files.
- rm -i will ask before deleting each file. Some people will have rm aliased to do this automatically (type "alias" to check). Consider using rm -I instead, which will only ask once and only if you are trying to delete three or more files.
- rm -r will recursively delete a directory and all its contents (normally rm will not delete directories, while rmdir will only delete empty directories).
- rm -f will forcibly delete files without asking; this is mostly useful if you have rm aliased to ``rm -i'' but want to delete lots of files without confirming each one.
Removing troublesome files
- Files with spaces or odd characters
- Put single quotes round the filename to hide the space from the shell, you could escape the space with \ if you prefer. Or just start typing the name and hit Tab and the shell will do this for you.
For a filename which begins with a dash (-) you also need to precede the name with ./ to prevent it being treated specially. - No such file or directory
- If this happens when trying to delete a file with a long name, it was probably displayed in truncated format. Press Tab to ask the shell to auto-complete the filename for you.
- Device or resource busy
- You can't delete a file which is in use. Reboot the computer and try again. Or try to track down the processes which are using the file.
/usr/sbin/lsof filename ps auxwww | grep process_id # If the processes belong to you and you don't mind killing them kill process_id # Be more emphatic kill -9 process_id