Delete files and folders.
Syntax rm [options] file ... unlink file Options -d Attempt to remove folders. -f Attempt to remove the files without prompting for confirmation, regardless of the file's permissions. If the file does not exist, do not display a diagnostic message or modify the exit status to reflect an error. -i Request confirmation before attempting to remove each file. -P Overwrite regular files before deleting them. Files are overwritten three times, first with the byte pattern 0xff, then 0x00, and then 0xff again, before they are deleted. -R Remove the entire file hierarchy rooted in each file argument. (implies the -d Delete folders). If the -i option is specified, the user is prompted for confirmation before each directory's contents are processed (as well as before the attempt is made to remove the directory). If the user does not respond affirmatively, the file hierarchy rooted in that directory is skipped. -r Equivalent to -R. -v Be verbose when deleting files, showing them as they are removed. -W Attempt to undelete the named files. Currently, this option can only be used to recover files covered by whiteouts. unlink Unlink a file, only one argument, which must not be a directory can be supplied. Calls the unlink() function which removes the link named by path from its directory and decrements the link count of the file which was referenced by the link.
The rm utility removes symbolic links, not the files referenced by the links.
If you are expanding a variable always put quotes around the filename in case it contains spaces:
Also consider the case where the $variable has not been set: rm -rf /$variable is not a good idea.
To delete a file with non-printable characters in the name: 'bad file name' Use the shell wildcard "?" for each character
If an error occurs, rm exits with a value >0.
Delete all .jpg files in the current folder:
$ rm *.jpg
Delete the folder named "temp", and all its contents:
$ rm -R temp
Delete a protected folder:
$ sudo rm -r NAME_OF_FOLDER_TO_DELETE
you will be prompted for your password. hit return.
Move the file Hunter.txt to the Trash using mv. The tilde ~ indicates that the .Trash folder is located in the users home.
$ mv Hunter.txt ~/.Trash
“You can't delete racism. It's like a cigarette. You can't stop smoking if you don't want to, and you can't stop racism if people don't want to. But I'll do everything I can to help” ~ Mario Balotelli
Related macOS commands:
cp - Copy files.
find - Find and optionally Delete files.
ls - List information about files.
mv - Move files and/or folders.
rmdir - Remove empty folder.
srm - Securely remove files or directories.
trimforce - Enable TRIM commands on third-party drives.
HT1526 - You can't empty the Trash or move a file to the Trash.