Delete files and folders.
Syntax
rm [options] file ...
unlink file
Options
-d Attempt to remove directories.
-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.
-I Request confirmation once if more than three files are being removed or if
a directory is being recursively removed. This is a far less intrusive
option than -i yet provides almost the same level of protection against mistakes.
-P This flag has no effect. It is kept only for backwards compatibility
with 4.4BSD-Lite2. (Overwrite regular files before deleting them.)
-R Remove the entire file hierarchy rooted in each file argument.
(implies -d Delete directories).
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 in a union file
system (see undelete(2)).
-x When removing a hierarchy, do not cross mount points.
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.
It is an error to attempt to remove the files /, . or ..
When the utility is called as unlink, only one argument, which must not be a directory, may be supplied. No options may be supplied in this simple mode of operation, which performs an unlink(2) operation on the passed argument. However, the usual option-end delimiter, --, may optionally precede the argument.
If you are expanding a variable always put quotes around the filename in case it contains spaces:
$ rm -f -- "$filename"
Include the option-end delimiter (--) just in case the variable contains something that looks like a valid rm option.
Also consider the case where the $variable has not been set, or is set to a wildcard: rm -rf /$variable is not a good idea.
The rm command uses getopt(3) to parse its arguments, which allows it to accept the ‘--’ option which will cause it to stop processing flag options at that point. This will allow the removal of file names that begin with a dash (‘-’). For example:
$ rm -- -dashname
The same behavior can be obtained by using an absolute or relative path reference. For example:
$ rm /home/user/-dashname
$ rm ./-dashname
$ unlink -dashnameTo delete a file with non-printable characters in the name: 'bad file name' Use the shell wildcard "?" for each character
rm bad?file?name
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 an old iPhone update file, normally these are automatically deleted:
$ rm "~/Library/iTunes/iPhone Software Updates/iPhone3_17.2.1_21C66_Restore.ipsw"
Recursively remove all files contained within the foobar directory hierarchy:
$ rm -rf foobar
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
Local man page: rm - Command line help page on your local machine.
chflags - Change a file or folder’s flags
cp - Copy files.
find - Find and optionally Delete files.
getopt - Parse positional parameters.
ls - List information about files.
mv - Move files and/or folders.
rmdir - Remove empty folder.
srm - Securely remove files or directories.
trash - Move files and directories to the trash
trimforce - Enable TRIM commands on third-party drives.
HT1526 - You can’t empty the Trash or move a file to the Trash.
HT201583- You can’t empty the Trash or move a file to the Trash.