Rm (Unix)

{{Short description|Shell command for deleting files}}

{{lowercase title}}

{{Infobox software

| name = rm

| logo =

| screenshot = Rm --help Command - Unix.png

| screenshot size =

| caption = The {{code|rm}} command

| author = Ken Thompson, Dennis Ritchie
(AT&T Bell Laboratories)

| developer = Various open-source and commercial developers

| released = {{Start date and age|1971|11|3}}

| latest release version =

| latest release date =

| programming language = C

| operating system = Unix, Unix-like, V, Plan 9, Inferno, KolibriOS, IBM i

| platform = Cross-platform

| genre = Command

| license = coreutils: GPLv3+
Plan 9: MIT License

| website =

}}

rm, short for remove, is a shell command on Unix and Unix-like operating systems used to remove files (which includes special files such as directories) from the file system.

The rm command may not actually delete a file since it only removes a reference to a file via the unlink() system call. A file can have multiple references (for example, a file with two names). The file will remain on the file system if there are other references to it after the specified links are removed.

The command generally does not destroy (overwrite/clear) file data, since its purpose to merely {{code|unlink()}} references, and the filesystem space freed may still contain leftover data from the removed file. This can be a security concern in some cases, and hardened versions sometimes provide for wiping out the data as the last link is being cut, and programs such as shred and srm are available which specifically provide data wiping capability.

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.{{Cite web|url=http://www.faqs.org/faqs/unix-faq/faq/part3/section-6.html|title=Unix - Frequently Asked Questions (3/7) [Frequent posting]Section - How do I "undelete" a file?|website=www.faqs.org}} 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.

The command is available in Windows via UnxUtils,{{Cite web|url=http://unxutils.sourceforge.net/|title=Native Win32 ports of some GNU utilities|website=unxutils.sourceforge.net}} KolibriOS,{{Cite web|url=http://wiki.kolibrios.org/wiki/Shell|title=Shell - KolibriOS wiki|website=wiki.kolibrios.org}} IBM i,{{cite web |title=IBM System i Version 7.2 Programming Qshell |language=en |author=IBM |website=IBM |author-link=IBM |url=https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc |access-date=2020-09-05 }} and EFI shell.{{cite web

| url = http://software.intel.com/en-us/articles/efi-shells-and-scripting/

| title = EFI Shells and Scripting

| publisher = Intel

| access-date = 2013-09-25

}}

The unlink command provides a similar function. The del command provides a similar capability in MS-DOS, OS/2, and Windows.

History

On some old versions of Unix, the rm command would delete directories if they were empty.{{Cite web|url=http://man.cat-v.org/unix_8th/1/rm|title=rm page from Section 1 of the unix 8th manual|website=man.cat-v.org}} This behaviour can still be obtained in some versions of rm with the -d flag, e.g., the BSDs (such as FreeBSD,{{citation |url=http://modman.unixdev.net/?manpath=FreeBSD-5.4-RELEASE&page=rm&sektion=1&apropos= |title=RM(1) |work=FreeBSD-5.4-RELEASE |access-date=February 5, 2015 }} NetBSD,{{citation |url=http://modman.unixdev.net/?manpath=NetBSD-2.0&page=rm&sektion=1&apropos= |title=RM(1) |work=NetBSD-2.0 |access-date=February 5, 2015 }} OpenBSD{{citation |url=http://modman.unixdev.net/?manpath=OpenBSD-3.6&page=rm&sektion=1&apropos= |title=RM(1) |work=OpenBSD-3.6 |access-date=February 5, 2015 }} and macOS) derived from 4.4BSD-Lite2.

The version of rm bundled in GNU coreutils was written by Paul Rubin, David MacKenzie, Richard Stallman, and Jim Meyering.{{Cite web|url=https://linux.die.net/man/1/rm|title=rm(1): remove files/directories - Linux man page|website=linux.die.net}} This version also provides -d option, to help with compatibility.{{cite web |url=http://git.savannah.gnu.org/cgit/coreutils.git/commit/src/rm.c?id=fdc2da7165d93c8065365999341173ad2a818833 |title=rm: new option --dir (-d) to remove empty directories |date=January 22, 2012 |author=Krzysztof Goj |website=coreutils.git }} The same functionality is provided by the standard rmdir command.

The -i option in Version 7 replaced dsw, or "delete from switches", which debuted in Version 1. Doug McIlroy wrote that dsw "was a desperation tool designed to clean up files with unutterable names".{{cite tech report |first1=M. D. |last1=McIlroy |author-link1=Doug McIlroy |year=1987 |url=http://www.cs.dartmouth.edu/~doug/reader.pdf |title=A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986 |series=CSTR |number=139 |institution=Bell Labs}}

Use

rm deletes specified files, but does not delete a directory by default.{{Cite web|url=https://man7.org/linux/man-pages/man1/rm.1.html|title=rm(1) - Linux manual page|website=man7.org}}rm foo deletes the file "foo" in the directory the user is currently in.

Commonly provided options:

  • -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; delete any empty directories
  • --one-file-system, only remove files on the same file system as the argument; ignore mounted file systems

rm can be overlain by a shell alias (C shell alias, Bourne shell or Bash) function of "rm -i" so as to avoid accidental deletion of files. If a user still wishes to delete a large number of files without confirmation, they can manually cancel out the -i argument by adding the -f option (as the option specified later on the expanded command line "rm -i -f" takes precedence). Unfortunately this approach generates dangerous habits towards the use of wildcarding, leading to its own version of accidental removals.

rm -rf (variously, rm -rf /, rm -rf *, and others) is frequently used in jokes and anecdotes about Unix disasters,{{cite web|last=Gite|first=Vivek|title=Linux/UNIX: Delete a file|url=http://www.cyberciti.biz/faq/howto-linux-unix-delete-remove-file/|publisher=Nixcraft|access-date=24 November 2011}} such as the loss of many files during the production of film Toy Story 2 at Pixar.{{cite web |last1=Panzarino |first1=Matthew |title=How Toy Story 2 Got Deleted Twice, Once on Accident, Again on purpose |url=https://thenextweb.com/news/how-pixars-toy-story-2-was-deleted-twice-once-by-technology-and-again-for-its-own-good |website=TNW {{!}} Media |access-date=27 September 2022 |language=en |date=21 May 2012}} The rm -rf / variant of the command, if run by a superuser, would cause every file accessible from the present file system to be deleted from the machine.

rm is often used in conjunction with xargs to supply a list of files to delete:

xargs rm < filelist

Or, to remove all PNG images in all directories below the current one:

find . -name '*.png' -exec rm {} +

Permissions

Usually, on most filesystems, deleting a file requires write permission on the parent directory (and execute permission, in order to enter the directory in the first place). (Note that, confusingly for beginners, permissions on the file itself are irrelevant. However, GNU rm asks for confirmation if a write-protected file is to be deleted, unless the -f option is used.){{Cite news|url=https://www.theinquirer.net/inquirer/news/2454743/man-destroys-his-entire-company-with-a-five-character-bash-command|archive-url=https://web.archive.org/web/20160416214235/http://www.theinquirer.net/inquirer/news/2454743/man-destroys-his-entire-company-with-a-five-character-bash-command|url-status=unfit|archive-date=April 16, 2016|title=Linux rm command help and examples|date=24 January 2018|work=ComputerHope|access-date=24 January 2019}}

To delete a directory (with rm -r), one must delete all of its contents recursively. This requires that one must have read and write and execute permission to that directory (if it's not empty) and all non-empty subdirectories recursively (if there are any). The read permissions are needed to list the contents of the directory in order to delete them. This sometimes leads to an odd situation where a non-empty directory cannot be deleted because one doesn't have write permission to it and so cannot delete its contents; but if the same directory were empty, one would be able to delete it.{{Cite news|url=https://www.macworld.com/article/2082021/master-the-command-line-deleting-files-and-folders.html|title=Master the command line: Deleting files and folders|last=McElhearn|first=Kirk|date=2 January 2014|work=Macworld|access-date=24 January 2019}}

If a file resides in a directory with the sticky bit set, then deleting the file requires one to be the owner of the file.

Protection of the filesystem root

Sun Microsystems introduced "rm -rf /" protection in Solaris 10, first released in 2005. Upon executing the command, the system now reports that the removal of / is not allowed.{{Cite web|url=https://blogs.oracle.com/jbeck/date/20041001%23rm_rf_protection|archive-url=https://web.archive.org/web/20161103124127/https://blogs.oracle.com/jbeck/date/20041001|archive-date=2016-11-03|url-status=dead|title=Meddling in the Affairs of Wizards}} Shortly after, the same functionality was introduced into FreeBSD version of rm utility.{{Cite web|url=https://github.com/freebsd/freebsd/commit/d6b7bd90c2776d9ccd8bb7f33a899bd213aebf85|title=The previous commit added code to rm(1) to warn about and remove any · freebsd/freebsd@d6b7bd9|website=GitHub}} GNU rm refuses to execute rm -rf / if the --preserve-root option is given,{{Cite web|url=https://www.gnu.org/software/coreutils/manual/html_node/rm-invocation.html|title=rm invocation (GNU Coreutils)|website=www.gnu.org}} which has been the default since version 6.4 of GNU Core Utilities was released in 2006. 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.

User-proofing

Systems administrators, designers, and even users often attempt to defend themselves against accidentally deleting files by creating an alias or function along the lines of:

alias rm="rm -i"

rm () { /bin/rm -i "$@" ; }

This results in rm asking the user to confirm on a file-by-file basis whether it should be deleted, by pressing the Y or N key. Unfortunately, this tends to train users to be careless about the wildcards they hand into their rm commands, as well as encouraging a tendency to alternately pound y and the return key to affirm removes - until just past the one file they needed to keep.{{Citation needed|date=July 2012}} Users have even been seen going as far as "yes | rm files", which automatically inserts "y" for each file. {{Citation needed|date=July 2012}}

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 ()

{

ls -FCsd "$@"

echo 'remove[ny]? ' | tr -d '\012' ; read

if [ "_$REPLY" = "_y" ]; then

/bin/rm -rf "$@"

else

echo '(cancelled)'

fi

}

fi

It is important to note that 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.

There exist third-party alternatives which prevent accidental deletion of important files, such as "safe-rm"{{Cite web|url=https://launchpad.net/safe-rm/+index|title=Safe-rm in Launchpad|website=Launchpad}} or "trash".{{Cite web|url=https://github.com/andreafrancia/trash-cli|title=andreafrancia/trash-cli|date=September 12, 2020|via=GitHub}}

Maximum command line argument limitation

{{Off topic|date=January 2025}}

GNU Core Utilities implementation used in multiple Linux distributions have 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.{{Cite web |title=How to get around the Linux "Too Many Arguments" limit |url=https://stackoverflow.com/questions/33051108/how-to-get-around-the-linux-too-many-arguments-limit |website=Stackoverflow |quote=With the coupling of ARG_MAX to ulim -s / 4 came the introduction of MAX_ARG_STRLEN as max. length of an argument [...] MAX_ARG_STRLEN is defined as 32 times the page size in linux/include/uapi/linux/binfmts.h [...] The default page size is 4 KB so you cannot pass arguments longer than 128 KB [...]}}

For command-line arguments before kernel 2.6.23, (released on 9 October 2007,) 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.{{Cite web |title=Linux_2_6_23 - Linux Kernel Newbies |url=https://kernelnewbies.org/Linux_2_6_23#Variable_argument_length |website=kernelnewbies.org}}{{Cite web |title=kernel/git/torvalds/linux.git - Linux kernel source tree |url=https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b6a2fea39318e43fee84fa7b0b90d68bed92d2ba |website=git.kernel.org}}

Newer kernels{{Which|date=January 2025}} limit the maximum argument length to 25% of the maximum stack limit (ulimit -s). Exceeding the limit would prompt the display of the error message /bin/rm: Argument list too long.{{Cite web|url=https://www.linuxjournal.com/article/6060|title="Argument list too long": Beyond Arguments and Limitations | Linux Journal|website=www.linuxjournal.com}}{{Clarify|reason=Incoherent paragraph.|date=January 2025}}

See also

References

{{Reflist}}

Further reading

  • {{Cite book|author-last=McElhearn|author-first=Kirk|title=The Mac OS X Command Line: Unix Under the Hood|date=2006|publisher=John Wiley & Sons|isbn=978-0470113851}}