Template:Short description Template:Lowercase title {{#invoke:Infobox|infobox}}Template:Template other{{#invoke:Check for unknown parameters | check | showblankpositional=1 | unknown = Template:Main other | preview = Page using Template:Infobox software with unknown parameter "_VALUE_"|ignoreblank=y | AsOf | author | background | bodystyle | caption | collapsetext | collapsible | developer | discontinued | engine | engines | genre | included with | language | language count | language footnote | latest preview date | latest preview version | latest release date | latest release version | latest_preview_date | latest_preview_version | latest_release_date | latest_release_version | licence | license | logo | logo alt | logo caption | logo upright | logo size | logo title | logo_alt | logo_caption | logo_upright | logo_size | logo_title | middleware | module | name | operating system | operating_system | other_names | platform | programming language | programming_language | released | replaced_by | replaces | repo | screenshot | screenshot alt | screenshot upright | screenshot size | screenshot title | screenshot_alt | screenshot_upright | screenshot_size | screenshot_title | service_name | size | standard | title | ver layout | website | qid }}Template:Main other rm, short for remove, is a shell command for removing files (which includes special files such as directories) from the file system. The command may not actually delete a file (release its storage for reuse) since it only unlinks it Template:Endash removes a hard link to a file via the unlink() system call. If a file has multiple links and less than all are removed, then the file remains in the file system; accessible via its other links. When a file's only link is removed, then the file is deleted Template:Endash releasing its storage space for other use.

Generally, a deleted file's former storage space still contains the file's data until it is overwritten with another file's content. The data is not accessible via normal file operations but can be recovered via specialized tools. Since this is considered a security risk in some contexts, a hardened version of <syntaxhighlight lang="text" class="" style="" inline="1">cp</syntaxhighlight> may wipe the file's storage area when the file is deleted. Commands such as shred and srm specifically provide data wiping.

Since rm does not provide a fallback to recover a file such as a recycle bin, its use involves the risk of accidentally losing information.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> Users tend to wrap calls to rm in safety mechanisms to limit accidental deletion. There are undelete utilities that attempts to reconstruct the index and can bring the file back if its storage was not reused.

Originally, developed for Unix, today it is also available on Unix-like and non Unix-like systems, KolibriOS,<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> IBM i,<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> EFI shell.<ref name="EFI-Shells-and-Scripting">{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> and Windows (via UnxUtils).<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> The del command provides a similar capability in MS-DOS, OS/2, and Command Prompt.

Like <syntaxhighlight lang="text" class="" style="" inline="1">rm</syntaxhighlight>, the unlink command also removes (unlinks) files, but only one file at a time.

HistoryEdit

On some old versions of Unix, the rm command would remove directories if they were empty.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> This behaviour can still be obtained in some versions of rm with the -d flag, e.g., the BSDs (such as FreeBSD,<ref>Template:Citation</ref> NetBSD,<ref>Template:Citation</ref> OpenBSD<ref>Template:Citation</ref> and macOS) derived from 4.4BSD-Lite2.

The version in GNU Core Utilities was written by Paul Rubin, David MacKenzie, Richard Stallman, and Jim Meyering.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> This version provides a -d option to help with compatibility.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> The same functionality is provided by the standard rmdir command.

OptionsEdit

Options commonly provided by a command implementation:

  • -r, recursive; remove directories and their content recursively
  • -i, interactive; ask user to confirm deleting each file
  • -f, force; ignore non-existent files and override any confirmation prompts (effectively canceling -i), does not allow removing files from a write-protected directory
  • -v, verbose; log status
  • -d, directory; remove any empty directories
  • --one-file-system, only remove files on the same file system as the argument; ignore mounted file systems

UseEdit

By default, rm removes specified files, but does not remove a directory.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> For example, the following removes the file named foo

<syntaxhighlight lang="bash"> $ rm foo </syntaxhighlight>

But that command fails if foo is a directory. To delete directory foo:

<syntaxhighlight lang="bash"> $ rm -r foo </syntaxhighlight>

The command is often used with xargs to supply a list of files:

<syntaxhighlight lang="bash"> $ xargs rm < filelist </syntaxhighlight>

To remove all PNG images in all directories below the current one:

<syntaxhighlight lang="bash"> $ find . -name '*.png' -exec rm {} + </syntaxhighlight>

SafetyEdit

PermissionsEdit

On most file systems, removing a file requires write and execute permissions on the containing directory. Some may be confused that permissions on the file to be removed are irrelevant. However, the GNU implementation confirms removing a write-protected file unless the -f option is used.<ref>Template:Cite news</ref>

To remove a directory (using -r), its contents must be removed, recursively. This requires the user to have read, write and execute permissions to the directory (if it's not empty) and any non-empty subdirectories recursively. Read permission is needed to list the contents of the directory. This sometimes leads to an odd situation where a non-empty directory cannot be removed because the user doesn't have write permission to it and so cannot remove its contents, but if the same directory were empty, the user would be able to remove it.<ref>Template:Cite news</ref>

If a file resides in a directory with the sticky bit set, then removing the file requires the user to own the file.

Preventing accidental deletionEdit

Commands like rm -rf * are relatively risky since they can delete many files in an unrecoverable way. Such commands are sometimes referenced in anecdotes about disastrous mistakes,<ref name="Linux/UNIX: Delete a file">{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> such as during the production of the film Toy Story 2.<ref name="Panzarino">{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

To minimize the risk of accidental file deletions, a common technique is to hide the default <syntaxhighlight lang="text" class="" style="" inline="1">rm</syntaxhighlight> command behind an alias or a function that includes the interactive option. For example:

<syntaxhighlight lang="bash"> alias rm="rm -i" </syntaxhighlight>

or

<syntaxhighlight lang="bash"> rm () { /bin/rm -i "$@" ; } </syntaxhighlight>

Then, by default, <syntaxhighlight lang="text" class="" style="" inline="1">rm</syntaxhighlight> requires the user to confirm removing each file by pressing Template:Key or Template:Key plus Template:Key. To bypass confirmation, a user can include the -f option (as the option specified later on the expanded command line "rm -i -f" takes precedence).

Unfortunately this can lead to other accidental removals since it trains users to be careless about the wildcards they hand to rm, as well as encouraging a tendency to mindlessly press Template:Key and Template:Key to confirm.Template:Citation needed Users have even been seen going as far as using yes | rm files, which automatically confirms the deletion of each file. Template:Citation needed

A compromise that allows users to confirm just once, encourages proper wildcarding, and makes verification of the list easier can be achieved with something like:

<syntaxhighlight lang="bash"> if [ -n "$PS1" ] ; then

 rm () 
 { 
     ls -FCsd "$@"
     echo 'remove[ny]? ' | tr -d '\012' ; read
     if [ "_$REPLY" = "_y" ]; then
         /bin/rm -rf "$@"
     else
         echo '(cancelled)'
     fi
 }

fi </syntaxhighlight>

Arguably, this function should not be made into a shell script, which would run a risk of it being found ahead of the system rm in the search path, nor should it be allowed in non-interactive shells where it could break batch jobs. Enclosing the definition in the if [ -n "$PS1" ] ; then ....  ; fi construct protects against the latter.

Other commands are designed to prevent accidental deletion; including <syntaxhighlight lang="text" class="" style="" inline="1">safe-rm</syntaxhighlight><ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> and <syntaxhighlight lang="text" class="" style="" inline="1">trash</syntaxhighlight>.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

Protection of the filesystem rootEdit

The rm -rf / command, if run by a superuser, causes every file of the file system to be deleted. For safety, Sun Microsystems introduced special protection for this command in Solaris 10 (first released in 2005). The implementation reports that removing <syntaxhighlight lang="text" class="" style="" inline="1">/</syntaxhighlight> is not allowed.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> Shortly thereafter, the same functionality was introduced into the FreeBSD implementation.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> The GNU version refuses to execute rm -rf / unless the --preserve-root option is included,<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> which has been the default since version 6.4 of GNU Core Utilities. In newer systems, this failsafe is always active, even without the option. To run the command, user must bypass the failsafe by adding the option --no-preserve-root, even if they are the superuser.

LimitationsEdit

Template:Off topic The GNU Core Utilities implementation has limits on command line arguments. Arguments are nominally limited to 32 times the kernel's allocated page size. Systems with 4KB page size would thus have a argument size limit of 128KB.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> For command-line arguments before kernel 2.6.23, the limits were defined at kernel compile time and can be modified by changing the variable MAX_ARG_PAGES in include/linux/binfmts.h file.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref><ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> Newer kernelsTemplate:Which limit the maximum argument length to 25% of the maximum stack limit (ulimit -s). Exceeding the limit results in an error.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>Template:Clarify

See alsoEdit

ReferencesEdit

Template:Reflist

Further readingEdit

External linksEdit

Template:Sister project

Template:Unix commands Template:Plan 9 commands Template:Core Utilities commands