Rm (command)
   HOME

TheInfoList



OR:

rm, short for remove, is a
shell Shell may refer to: Architecture and design * Shell (structure), a thin structure ** Concrete shell, a thin shell of concrete, usually with no interior columns or exterior buttresses Science Biology * Seashell, a hard outer layer of a marine ani ...
command Command may refer to: Computing * Command (computing), a statement in a computer language * command (Unix), a Unix command * COMMAND.COM, the default operating system shell and command-line interpreter for DOS * Command key, a modifier key on A ...
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 removes a
hard link In computing, a hard link is a directory entry (in a Directory (computing), directory-based file system) that associates a name with a Computer file, file. Thus, each file must have at least one hard link. Creating additional hard links for a fil ...
to a file via the unlink()
system call In computing, a system call (syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, accessing a hard disk drive ...
. 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 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 Security is protection from, or resilience against, potential harm (or other unwanted coercion). Beneficiaries (technically referents) of security may be persons and social groups, objects and institutions, ecosystems, or any other entity or ...
risk in some contexts, a hardened version of 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 A recycling bin (or recycle bin) is a container used to hold recyclables before they are taken to recycling centers. Recycling bins exist in various sizes for use inside and outside of homes, offices, and large public facilities. Separate conta ...
, its use involves the risk of accidentally losing information. Users tend to wrap calls to rm in safety mechanisms to limit accidental deletion. There are
undelete Undeletion is a feature for restoring computer files which have been removed from a file system by file deletion. Deleted data can be recovered on many file systems, but not all file systems provide an undeletion feature. Recovering data witho ...
utilities that attempts to reconstruct the index and can bring the file back if its storage was not reused. Originally, developed for
Unix Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
, today it is also available on
Unix-like A Unix-like (sometimes referred to as UN*X, *nix or *NIX) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Uni ...
and non Unix-like systems,
KolibriOS KolibriOS is an open-source operating system for x86 computers, written completely in FASM assembly language. It has been developed since 2004, forked from MenuetOS, and supports i586 CPU, CPUs or newer. KolibriOS is small sized and fits on a sin ...
,
IBM i IBM i (the ''i'' standing for ''integrated'') is an operating system developed by IBM for IBM Power Systems. It was originally released in 1988 as OS/400, as the sole operating system of the IBM AS/400 line of systems. It was renamed to i5/OS in 2 ...
, EFI shell. and
Windows Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
(via
UnxUtils UnxUtils is a collection of utility programs that provide popular Unix-based shell commands ported from GNU implementations as native Windows programs that depend only on Win32 and the Microsoft C- runtime ( msvcrt.dll). The collection wa ...
). The
del Del, or nabla, is an operator used in mathematics (particularly in vector calculus) as a vector differential operator, usually represented by the nabla symbol ∇. When applied to a function defined on a one-dimensional domain, it denotes ...
command provides a similar capability in
MS-DOS MS-DOS ( ; acronym for Microsoft Disk Operating System, also known as Microsoft DOS) is an operating system for x86-based personal computers mostly developed by Microsoft. Collectively, MS-DOS, its rebranding as IBM PC DOS, and a few op ...
,
OS/2 OS/2 is a Proprietary software, proprietary computer operating system for x86 and PowerPC based personal computers. It was created and initially developed jointly by IBM and Microsoft, under the leadership of IBM software designer Ed Iacobucci, ...
, and
Command Prompt A command-line interface (CLI) is a means of interacting with software via commands each formatted as a line of text. Command-line interfaces emerged in the mid-1960s, on computer terminals, as an interactive and more user-friendly alternativ ...
. Like , the
unlink In the mathematical field of knot theory, an unlink is a link that is equivalent (under ambient isotopy) to finitely many disjoint circles in the plane. The two-component unlink, consisting of two non-interlinked unknots, is the simplest pos ...
command also removes (unlinks) files, but only one file at a time.


History

On some old versions of Unix, the rm command would remove directories if they were empty. This behaviour can still be obtained in some versions of rm with the -d flag, e.g., the
BSDs There are a number of Unix-like operating systems under active development, descended from the Berkeley Software Distribution (BSD) series of UNIX variants developed (originally by Bill Joy) at the University of California, Berkeley, Department o ...
(such as
FreeBSD FreeBSD is a free-software Unix-like operating system descended from the Berkeley Software Distribution (BSD). The first version was released in 1993 developed from 386BSD, one of the first fully functional and free Unix clones on affordable ...
,
NetBSD NetBSD is a free and open-source Unix-like operating system based on the Berkeley Software Distribution (BSD). It was the first open-source BSD descendant officially released after 386BSD was fork (software development), forked. It continues to ...
,
OpenBSD OpenBSD is a security-focused operating system, security-focused, free software, Unix-like operating system based on the Berkeley Software Distribution (BSD). Theo de Raadt created OpenBSD in 1995 by fork (software development), forking NetBSD ...
and
macOS macOS, previously OS X and originally Mac OS X, is a Unix, Unix-based operating system developed and marketed by Apple Inc., Apple since 2001. It is the current operating system for Apple's Mac (computer), Mac computers. With ...
) derived from 4.4BSD-Lite2. The version in
GNU Core Utilities The GNU Core Utilities or coreutils is a collection of GNU software that implements many standard, Unix-based shell commands. The utilities generally provide POSIX compliant interface when the environment variable is set, but otherwise offers ...
was written by Paul Rubin, David MacKenzie,
Richard Stallman Richard Matthew Stallman ( ; born March 16, 1953), also known by his initials, rms, is an American free software movement activist and programmer. He campaigns for software to be distributed in such a manner that its users have the freedom to ...
, and Jim Meyering. This version provides a -d option to help with compatibility. The same functionality is provided by the standard
rmdir In computing, rmdir (or rd) is a command which will remove an empty directory on various operating systems. Implementations The command is available in Unix (e.g. macOS, Solaris, AIX, HP-UX), Unix-like (e.g. FreeBSD, Linux), DOS, Digital ...
command.


Options

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


Use

By default, rm removes specified files, but does not remove a directory. For example, the following removes the file named
foo The terms foobar (), foo, bar, baz, qux, quux, and others are used as metasyntactic variables and placeholder names in computer programming or computer-related documentation. - Etymology of "Foo" They have been used to name entities such as Var ...
$ rm foo But that command fails if foo is a directory. To delete directory foo: $ rm -r foo The command is often used with
xargs xargs (short for "extended arguments") is a command on Unix and most Unix-like operating systems used to build and execute commands from standard input. It converts input from standard input into arguments to a command. Some commands such as gre ...
to supply a list of files: $ xargs rm < filelist To remove all PNG images in all directories below the current one: $ find . -name '*.png' -exec rm +


Safety


Permissions

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. 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. 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 deletion

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, such as during the production of the film ''
Toy Story 2 ''Toy Story 2'' is a 1999 American animated adventure comedy film produced by Pixar Animation Studios, and the first sequel to Toy Story. It is the second installment in the Toy Story (franchise), ''Toy Story'' franchise and was directed by J ...
''. To minimize the risk of accidental file deletions, a common technique is to hide the default command behind an alias or a function that includes the interactive option. For example: alias rm="rm -i" or rm () Then, by default, requires the user to confirm removing each file by pressing or plus . 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 and to confirm. Users have even been seen going as far as using yes , rm ''files'', which automatically confirms the deletion of each file. 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: if -n "$PS1" ; then rm () fi Arguably, this function should not be made into a
shell script A shell script is a computer program designed to be run by a Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be command languages. Typical operations performed by shell scripts include file manipu ...
, 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 and .


Protection of the filesystem root

The rm -rf / command, if run by a
superuser In computing, the superuser is a special user account used for system administration. Depending on the operating system (OS), the actual name of this account might be root, administrator, admin or supervisor. In some cases, the actual name of the ...
, causes every file of the file system to be deleted. For safety,
Sun Microsystems Sun Microsystems, Inc., often known as Sun for short, was an American technology company that existed from 1982 to 2010 which developed and sold computers, computer components, software, and information technology services. Sun contributed sig ...
introduced special protection for this command in
Solaris Solaris is the Latin word for sun. It may refer to: Arts and entertainment Literature, television and film * ''Solaris'' (novel), a 1961 science fiction novel by Stanisław Lem ** ''Solaris'' (1968 film), directed by Boris Nirenburg ** ''Sol ...
10 (first released in 2005). The implementation reports that removing is not allowed. Shortly thereafter, the same functionality was introduced into the
FreeBSD FreeBSD is a free-software Unix-like operating system descended from the Berkeley Software Distribution (BSD). The first version was released in 1993 developed from 386BSD, one of the first fully functional and free Unix clones on affordable ...
implementation. The
GNU GNU ( ) is an extensive collection of free software (394 packages ), which can be used as an operating system or can be used in parts with other operating systems. The use of the completed GNU tools led to the family of operating systems popu ...
version refuses to execute rm -rf / unless the --preserve-root option is included, which has been the default since version 6.4 of
GNU Core Utilities The GNU Core Utilities or coreutils is a collection of GNU software that implements many standard, Unix-based shell commands. The utilities generally provide POSIX compliant interface when the environment variable is set, but otherwise offers ...
. In newer systems, this
failsafe In engineering, a fail-safe is a design feature or practice that, in the event of a failure of the design feature, inherently responds in a way that will cause minimal or no harm to other equipment, to the environment or to people. Unlike inherent ...
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.


Limitations

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. 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. Newer kernels limit the maximum argument length to 25% of the maximum stack limit (ulimit -s). Exceeding the limit results in an error.


See also

* *


References


Further reading

*


External links

* * * {{Core Utilities commands File deletion Standard Unix programs Unix SUS2008 utilities Plan 9 commands Inferno (operating system) commands IBM i Qshell commands