Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Rm (Unix)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{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]]<br />([[AT&T Bell Laboratories]]) | developer = Various [[open-source software|open-source]] and [[commercial software|commercial]] developers | released = {{Start date and age|1971|11|3}} | latest release version = | latest release date = | programming language = [[C (programming language)|C]] | operating system = [[Unix]], [[Unix-like]], [[V (operating system)|V]], [[Plan 9 from Bell Labs|Plan 9]], [[Inferno (operating system)|Inferno]], [[KolibriOS]], [[IBM i]] | platform = [[Cross-platform]] | genre = [[Command (computing)|Command]] | license = [[coreutils]]: [[GPLv3+]]<br />Plan 9: [[MIT License]] | website = }} '''<code>rm</code>''', short for '''r'''e'''m'''ove, is a [[shell (computing)|shell]] [[command (computing)|command]] for removing files (which includes [[Unix file type|special files]] such as [[directory (computing)|directories]]) from the [[file system]]. The command may not actually delete a file (release its storage for reuse) since it only unlinks it {{endash}} removes a [[hard link]] to a file via the <code>[[unlink (Unix)|unlink()]]</code> [[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 {{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 [[software security|security]] risk in some contexts, a hardened version of {{code|cp}} may wipe the file's storage area when the file is deleted. Commands such as [[Shred (Unix)|shred]] and [[srm (Unix)|srm]] specifically provide data wiping. Since <code>rm</code> does not provide a fallback to recover a file such as a [[recycle bin (computing)|recycle bin]], its use involves the risk of accidentally losing information.<ref>{{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}}</ref> Users tend to wrap calls to <code>rm</code> 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>{{Cite web|url=http://wiki.kolibrios.org/wiki/Shell|title=Shell - KolibriOS wiki|website=wiki.kolibrios.org}}</ref> [[IBM i]],<ref>{{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 }}</ref> [[Unified Extensible Firmware Interface|EFI shell]].<ref name="EFI-Shells-and-Scripting">{{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 }}</ref> and [[Windows]] (via [[UnxUtils]]).<ref>{{Cite web|url=http://unxutils.sourceforge.net/|title=Native Win32 ports of some GNU utilities|website=unxutils.sourceforge.net}}</ref> The <code>[[Del (command)|del]]</code> command provides a similar capability in [[MS-DOS]], [[OS/2]], and [[Command Prompt]]. Like {{code|rm}}, the <code>[[unlink (Unix)|unlink]]</code> command also removes (unlinks) files, but only one file at a time. == History == On some old versions of Unix, the <code>rm</code> command would remove directories if they were empty.<ref>{{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}}</ref> This behaviour can still be obtained in some versions of <code>rm</code> with the <code>-d</code> flag, e.g., the [[Berkeley Software Distribution|BSDs]] (such as [[FreeBSD]],<ref>{{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 }}</ref> [[NetBSD]],<ref>{{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 }}</ref> [[OpenBSD]]<ref>{{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 }}</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>{{Cite web|url=https://linux.die.net/man/1/rm|title=rm(1): remove files/directories - Linux man page|website=linux.die.net}}</ref> This version provides a <code>-d</code> option to help with compatibility.<ref>{{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 }}</ref> The same functionality is provided by the standard <code>[[rmdir]]</code> command. == Options == Options commonly provided by a command implementation: *<code>-r</code>, '''r'''ecursive; remove directories and their content recursively *<code>-i</code>, '''i'''nteractive; ask user to confirm deleting each file *<code>-f</code>, '''f'''orce; ignore non-existent files and override any confirmation prompts (effectively canceling <code>-i</code>), does ''not'' allow removing files from a write-protected directory *<code>-v</code>, '''v'''erbose; log status *<code>-d</code>, '''d'''irectory; remove any empty directories *<code>--one-file-system</code>, only remove files on the same file system as the argument; ignore mounted file systems == Use == By default, <code>rm</code> removes specified files, but does not remove a directory.<ref>{{Cite web|url=https://man7.org/linux/man-pages/man1/rm.1.html|title=rm(1) - Linux manual page|website=man7.org}}</ref> For example, the following removes the file named '''[[Foobar|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 [[Portable Network Graphics|PNG images]] in all directories below the current one: <syntaxhighlight lang="bash"> $ find . -name '*.png' -exec rm {} + </syntaxhighlight> == 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.<ref>{{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}}</ref> To remove a directory (using <code>-r</code>), 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>{{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}}</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 deletion === Commands like <code>rm -rf *</code> 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">{{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}}</ref> such as during the production of the film ''[[Toy Story 2]]''.<ref name="Panzarino">{{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}}</ref> To minimize the risk of accidental file deletions, a common technique is to hide the default {{code|rm}} 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, {{code|rm}} requires the user to confirm removing each file by pressing {{key|Y}} or {{key|N}} plus {{key|Return}}. To bypass confirmation, a user can include the <code>-f</code> option (as the option specified later on the expanded command line "<code>rm -i -f</code>" takes precedence). Unfortunately this can lead to other accidental removals since it trains users to be careless about the wildcards they hand to <code>rm</code>, as well as encouraging a tendency to mindlessly press {{key|Y}} and {{key|Return}} to confirm.{{Citation needed|date=July 2012}} Users have even been seen going as far as using <code>yes | rm ''files''</code>, which automatically confirms the deletion of 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: <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 <code>rm</code> in the search path, nor should it be allowed in non-interactive shells where it could break batch jobs. Enclosing the definition in the <code>if [ -n "$PS1" ] ; then .... ; fi</code> construct protects against the latter. Other commands are designed to prevent accidental deletion; including {{code|safe-rm}}<ref>{{Cite web|url=https://launchpad.net/safe-rm/+index|title=Safe-rm in Launchpad|website=Launchpad}}</ref> and {{code|trash}}.<ref>{{Cite web|url=https://github.com/andreafrancia/trash-cli|title=andreafrancia/trash-cli|date=September 12, 2020|via=GitHub}}</ref> === Protection of the filesystem root === The <code>rm -rf /</code> 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 (operating system)|Solaris]] 10 (first released in 2005). The implementation reports that removing {{code|/}} is not allowed.<ref>{{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}}</ref> Shortly thereafter, the same functionality was introduced into the [[FreeBSD]] implementation.<ref>{{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}}</ref> The [[GNU]] version refuses to execute <code>rm -rf /</code> unless the <code>--preserve-root</code> option is included,<ref>{{Cite web|url=https://www.gnu.org/software/coreutils/manual/html_node/rm-invocation.html|title=rm invocation (GNU Coreutils)|website=www.gnu.org}}</ref> which has been the default since version 6.4 of [[GNU Core Utilities]]. In newer systems, this [[Fail-safe|failsafe]] is always active, even without the option. To run the command, user must bypass the failsafe by adding the option <code>--no-preserve-root</code>, even if they are the superuser. ==Limitations== {{Off topic|date=January 2025}} 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>{{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=[[Stack Overflow|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 [...]}}</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 <code>MAX_ARG_PAGES</code> in <code>include/linux/binfmts.h</code> file.<ref>{{Cite web |title=Linux_2_6_23 - Linux Kernel Newbies |url=https://kernelnewbies.org/Linux_2_6_23#Variable_argument_length |website=kernelnewbies.org}}</ref><ref>{{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}}</ref> Newer kernels{{Which|date=January 2025}} limit the maximum argument length to 25% of the maximum stack limit (ulimit -s). Exceeding the limit results in an error.<ref>{{Cite web|url=https://www.linuxjournal.com/article/6060|title="Argument list too long": Beyond Arguments and Limitations | Linux Journal|website=www.linuxjournal.com}}</ref>{{Clarify|reason=Incoherent paragraph.|date=January 2025}}<!-- Citation needed; not known if this is generally true for all linux kernels. --> == See also == * {{Annotated link|deltree}} * {{Annotated link|dsw (command)}} == 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}} == External links == {{wikibooks|Guide to Unix|Commands/File System Utilities#rm|rm}} *{{man|cu|rm|SUS|remove directory entries}} *{{man|1|rm|Plan 9}} *{{man|1|rm|Inferno}} {{Unix commands}} {{Plan 9 commands}} {{Core Utilities commands}} [[Category:File deletion]] [[Category:Standard Unix programs]] [[Category:Unix SUS2008 utilities]] [[Category:Plan 9 commands]] [[Category:Inferno (operating system) commands]] [[Category:IBM i Qshell commands]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Annotated link
(
edit
)
Template:Citation
(
edit
)
Template:Citation needed
(
edit
)
Template:Cite book
(
edit
)
Template:Cite news
(
edit
)
Template:Cite web
(
edit
)
Template:Clarify
(
edit
)
Template:Code
(
edit
)
Template:Core Utilities commands
(
edit
)
Template:Endash
(
edit
)
Template:Infobox
(
edit
)
Template:Infobox software
(
edit
)
Template:Key
(
edit
)
Template:Lowercase title
(
edit
)
Template:Main other
(
edit
)
Template:Man
(
edit
)
Template:Off topic
(
edit
)
Template:Plan 9 commands
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Sister project
(
edit
)
Template:Template other
(
edit
)
Template:Unix commands
(
edit
)
Template:Which
(
edit
)
Template:Wikibooks
(
edit
)