Tag Info

Hot answers tagged

164

If you have rm from GNU coreutils (most probably if it's a regular Linux distro), rm -rf / will be refused by the built-in protection (according to manpage and Wikipedia, haven't tried that). You can override this protection with --no-preserve-root. rm will then remove everything it possibly can, without stopping after having attempted to remove every ...


110

In general there is no easy way back - but see RiMMER's interesting and useful link. You could restore from your daily backup.


97

To rm all but u,p in bash just type: rm !(u|p) This requires the following option to be set: shopt -s extglob See more: glob - Greg's Wiki


82

What I do in those cases is to type rm * Then I press Ctrl+X,* to expand * into all visible file names. Then I can just remove the two files I like to keep from the list and finally execute the command line.


52

Others (such as @RiMMER, @Adam, and @James T) have mentioned that, for most filesystems, it's possible to recover most or all of your data (perhaps without filenames), because the data is not actually zeroed out, only removed from the file-table. This is not just true of Linux - the same is true of Windows and Mac. However, no one has mentioned the most ...


44

rm -- -d -- means "end of options". Anything further on the command line following this is interpreted as an argument (i.e. the file name in your case), and not an option.


44

Use rm \\ (escape the backslash with another backslash). Note that this also works similarily, for directories named \ (using either rmdir, or rm with the -r flag). Example: >mkdir demo >cd demo >touch \\ >ls -l total 0 -rw------- 1 hennes users 0 Jul 29 20:25 \ >rm \\ >ls -l total 0


43

Simple: mv the files you want in a upper directory, rm the directory and then mv them back.


42

The -f option is definitely what you want to be using. The confirmation about file permissions it refers to is this: $ touch myfile $ chmod 400 myfile $ rm myfile rm: remove write-protected regular empty file `myfile'? So rm will warn you if you try to delete a file you don't have write permissions on. This is allowed if you have write ...


38

You can use find find . ! -name u ! -name p -maxdepth 1 -type f -delete ! negates the next expression -name specifies a filename -maxdepth 1 will make find process the specified directory only (find by default traverses directories) -type f will process only files (and not for example directories) -delete will delete the files You can then tune the ...


36

For those who like to do stuff like this visually while listening to techno music. Running rm- rf on Linux (video) Bonus points if you can name the processes as they start dying.


35

Quicker is not necessarily what you want. You may want to actually run slower, so the deletion chews up fewer resources while it's running. Use nice(1) to lower the priority of a command. nice find . -name "*.gif" -delete For I/O-bound processes nice(1) might not be sufficient. The Linux scheduler does take I/O into account, not just CPU, but you may ...


28

rm ./-d is the answer to your question.


27

To directly answer your question, "no - you can't do what you describe with rm". You can, however, do it you combine it with find. Here's one of many ways you could do that: # search for everything in this tree, search for the file pattern, pipe to rm find . | grep <pattern> | xargs rm For example, if you want to nuke all *~ files, you could so ...


27

The following are generic steps to recover text files. First use wall command to tell user that system is going down in a single user mode: # wall System is going down to .... please save your work. Press CTRL+D to send message. Next use init 1 command to take system to a single user mode: # init 1 Using grep (traditional UNIX way) to recover files ...


23

You're right. It's bad because you get used to it. If you're on a system that doesn't have it, and you rm, it immediately starts deleting and you're wondering what's going on. Many users are used to SSH'ing into different systems; so using lots of different systems, sometimes without personalized user accounts (including aliases) set up, is rather common. ...


23

To remove one file you need write permission on the directory that contains¹ this file. Here the permissions are dr-xr-xr-x 3 rayell pg1083760 4096 2010-10-10 10:00 . So nobody (other than root) can remove files inside this directory. The owner must use chmod first. — 1. There are pretty good reasons for that. By ‘removing’ a file with rm, you are in fact ...


20

Set up a VM and try for fun? It'll go quite far... if you're using a gui you might have fun noticing things degrade more visibly. (icons on menus stop loading etc.) If you let it go, the OS will pretty much be beyond recovery though you may be able to get some data back easily. Either way, you'll be wanting to do a reinstall of the OS.


20

On ubuntu or similar: $ sudo apt-get install trash-cli $ alias rm=trash Then put that alias in .bashrc or the appropriate login script for your shell of choice. The trash-cli package is a command-line interface to the same trash can that GNOME and KDE and other use. So anything you delete via the trash command can be restored by GNOME/KDE and vice-versa. ...


19

There is no "undo" for this action, but you may still try recovering your files.


18

ls * | grep -v dont_delete_this_file | xargs rm -rf Example : mkdir test && cd test touch test1 touch test2 touch test3 touch test4 touch test5 To remove all files except 'test2' : ls * | grep -v test2 | xargs rm -rf Then 'ls' output is : test2 EDIT: Thanks for the comment. If the directory contains some files with spaces : mkdir test ...


17

Yes: rm -rf * Be carefull where you are when you run this it will delete everything from the current directory and all subdirectories. If you only want to delete files, and no directories use: rm * As @DanielAndersson very correctly pointed out in the comments, this will not delete hidden files and directories (those beginning with a .). To delete ...


17

If you want a customized rm, don't call it rm but a name of yours, myrm, delete or whatever. The rm='rm -i' alias is an horror because after a while using it, you will expect rm to prompt you by default before removing files. Of course, one day you'll run it with an account that hasn't that alias set and before you understand what's going on, it is too ...


16

It depends on the distribution. The older Linux I'm on right now allows it (I think, didn't test it though :-) ) and states in man rm: --no-preserve-root do not treat '/' specially (the default) --preserve-root fail to operate recursively on '/' On many recent distributions, you need to explicitly add --no-preserve-root to disable the ...


14

In bash: shopt -s extglob rm !(dontdelete)


14

Short version: rm *\([1-9]\)* Do not pipe ls to xargs. Instead, pipe find ... -print0 to xargs -0, in order to avoid such problems. find . -maxdepth 1 -name '*([1-9])*' -print0 | xargs -0 rm ...which can be written as: find . -maxdepth 1 -name '*([1-9])*' -exec rm {} \; and: find . -maxdepth 1 -name '*([1-9])*' -delete which can further be ...


14

in some cases yes .. involves running a script .. check this post out on perlmonks http://www.perlmonks.org/?node%5Fid=106709


13

Why is there no such safeguard in the rm command? There are already three safeguards: The -r switch, without which a directory can't be removed. The -i switch, which, which verifies that you actually want to delete what you've asked to delete. Aliasing rm to rm -i turns that safeguard on unless you add the -f switch to turn it off. File ownership, ...


13

Somewhat similar to this answer but no special options are needed, as far as I know the following is "ancient" functionality supported by any (vaguely) /bin/sh resembling shell (e.g. bash, zsh, ksh, etc) rm [^up]



Only top voted, non community-wiki answers of a minimum length are eligible