Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Overview  





2 History  





3 Syntax  





4 Permissions  





5 Protection of the filesystem root  





6 User-proofing  





7 Maximum command line argument limitation  





8 See also  





9 References  





10 Further reading  





11 External links  














rm (Unix)






العربية
Azərbaycanca
Беларуская
Български
Català
Čeština
Deutsch
Español
فارسی
Français

Հայերեն
Italiano
עברית

Magyar
Mirandés
Nederlands

Polski
Português
Română
Русский
Suomi
Türkçe
Українська


 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 




In other projects  



Wikimedia Commons
Wikiquote
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 


rm
Original author(s)Ken Thompson, Dennis Ritchie
(AT&T Bell Laboratories)
Developer(s)Various open-source and commercial developers
Initial releaseNovember 3, 1971; 52 years ago (1971-11-03)
Written inC
Operating systemUnix, Unix-like, V, Plan 9, Inferno, KolibriOS, IBM i
PlatformCross-platform
TypeCommand
Licensecoreutils: GPLv3+
Plan 9: MIT License

rm (short for remove) is a basic commandonUnix and Unix-like operating systems used to remove objects such as computer files, directories and symbolic links from file systems and also special files such as device nodes, pipes and sockets, similar to the del command in MS-DOS, OS/2, and Microsoft Windows. The command is also available in the EFI shell.[1]

Overview

[edit]

The rm command removes references to objects from the filesystem using the unlink system call, where those objects might have had multiple references (for example, a file with two different names), and the objects themselves are discarded only when all references have been removed and no programs still have open handles to the objects.

This allows for scenarios where a program can open a file, immediately remove it from the filesystem, and then use it for temporary space, knowing that the file's space will be reclaimed after the program exits, even if it exits by crashing.

The command generally does not destroy file data, since its purpose is really merely to 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.

rm is generally only seen on UNIX-derived operating systems, which typically do not provide for recovery of deleted files through a mechanism like the recycle bin,[2] hence the tendency for users to enclose rm in some kind of wrapper to limit accidental file deletion.

There are undelete utilities that will attempt to reconstruct the index and can bring the file back if the parts were not reused.

History

[edit]

On some old versions of Unix, the rm command would delete directories if they were empty.[3] This behaviour can still be obtained in some versions of rm with the -d flag, e.g., the BSDs (such as FreeBSD,[4] NetBSD,[5] OpenBSD[6] 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.[7] This version also provides -d option, to help with compatibility.[8] 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".[9]

The command is available as a separate package for Microsoft Windows as part of the UnxUtils collection of native Win32 ports of common GNU Unix-like utilities.[10] KolibriOS includes an implementation of the rm command.[11] The rm command has also been ported to the IBM i operating system.[12]

Syntax

[edit]

rm deletes the file specified after options are added. Users can use a full path or a relative file path to specify the files to delete. rm doesn't delete a directory by default.[13]rm foo deletes the file "foo" in the directory the user is currently in.

rm, like other commands, uses options to specify how it will behave:

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,[14] such as the loss of many files during the production of film Toy Story 2atPixar.[15] 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

[edit]

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.)[16]

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.[17]

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

[edit]

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.[18] Shortly after, the same functionality was introduced into FreeBSD version of rm utility.[19] GNU rm refuses to execute rm -rf / if the --preserve-root option is given,[20] 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

[edit]

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] Users have even been seen going as far as "yes | rm files", which automatically inserts "y" for each file. [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:

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"[21] or "trash".[22]

Maximum command line argument limitation

[edit]

GNU Core Utilities implementation used in multiple Linux distributions will be limited in command line arguments bytes number of pages that are allocated within the kernel for command-line arguments before kernel 2.6.23 Released on 9 October 2007[23][24] were defined at kernel compile time at variable MAX_ARG_PAGESininclude/linux/binfmts.h file but new kernels limits the maximum argument length to 25% of the maximum stack limit (ulimit -s). Error: /bin/rm: Argument list too long. will prompt if command-line argument limit is exceed[25]

See also

[edit]

References

[edit]
  1. ^ "EFI Shells and Scripting". Intel. Retrieved 2013-09-25.
  • ^ "Unix - Frequently Asked Questions (3/7) [Frequent posting]Section - How do I "undelete" a file?". www.faqs.org.
  • ^ "rm page from Section 1 of the unix 8th manual". man.cat-v.org.
  • ^ "RM(1)", FreeBSD-5.4-RELEASE, retrieved February 5, 2015
  • ^ "RM(1)", NetBSD-2.0, retrieved February 5, 2015
  • ^ "RM(1)", OpenBSD-3.6, retrieved February 5, 2015
  • ^ "rm(1): remove files/directories - Linux man page". linux.die.net.
  • ^ Krzysztof Goj (January 22, 2012). "rm: new option --dir (-d) to remove empty directories". coreutils.git.
  • ^ McIlroy, M. D. (1987). A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986 (PDF) (Technical report). CSTR. Bell Labs. 139.
  • ^ "Native Win32 ports of some GNU utilities". unxutils.sourceforge.net.
  • ^ "Shell - KolibriOS wiki". wiki.kolibrios.org.
  • ^ IBM. "IBM System i Version 7.2 Programming Qshell" (PDF). IBM. Retrieved 2020-09-05.
  • ^ "rm(1) - Linux manual page". man7.org.
  • ^ Gite, Vivek. "Linux/UNIX: Delete a file". Nixcraft. Retrieved 24 November 2011.
  • ^ Panzarino, Matthew (21 May 2012). "How Toy Story 2 Got Deleted Twice, Once on Accident, Again on purpose". TNW | Media. Retrieved 27 September 2022.
  • ^ "Linux rm command help and examples". ComputerHope. 24 January 2018. Archived from the original on April 16, 2016. Retrieved 24 January 2019.{{cite news}}: CS1 maint: unfit URL (link)
  • ^ McElhearn, Kirk (2 January 2014). "Master the command line: Deleting files and folders". Macworld. Retrieved 24 January 2019.
  • ^ "Meddling in the Affairs of Wizards". Archived from the original on 2016-11-03.
  • ^ "The previous commit added code to rm(1) to warn about and remove any · freebsd/freebsd@d6b7bd9". GitHub.
  • ^ "rm invocation (GNU Coreutils)". www.gnu.org.
  • ^ "Safe-rm in Launchpad". Launchpad.
  • ^ "andreafrancia/trash-cli". September 12, 2020 – via GitHub.
  • ^ "Linux_2_6_23 - Linux Kernel Newbies". kernelnewbies.org.
  • ^ "kernel/git/torvalds/linux.git - Linux kernel source tree". git.kernel.org.
  • ^ ""Argument list too long": Beyond Arguments and Limitations | Linux Journal". www.linuxjournal.com.
  • Further reading

    [edit]
    [edit]
    Retrieved from "https://en.wikipedia.org/w/index.php?title=Rm_(Unix)&oldid=1167035332"

    Categories: 
    File deletion
    Standard Unix programs
    Unix SUS2008 utilities
    Plan 9 commands
    Inferno (operating system) commands
    IBM i Qshell commands
    Hidden categories: 
    CS1 maint: unfit URL
    Articles with short description
    Short description is different from Wikidata
    All articles with unsourced statements
    Articles with unsourced statements from July 2012
     



    This page was last edited on 25 July 2023, at 08:06 (UTC).

    Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



    Privacy policy

    About Wikipedia

    Disclaimers

    Contact Wikipedia

    Code of Conduct

    Developers

    Statistics

    Cookie statement

    Mobile view



    Wikimedia Foundation
    Powered by MediaWiki