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)
(section)
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!
=== 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>
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)