Hot answers tagged

180

sudo touch /bin/rm && sudo chmod +x /bin/rm apt-get download coreutils sudo dpkg --unpack coreutils* And never again. Why didn't you use sudo with apt-get? Because the download command doesn't require it: download download will download the given binary package into the current directory. So, unless you are ...


178

Use "--" to make rm stop parsing command line options, like this: rm -- --help


99

debian and its derivatives (and probably most other distributions) come with busybox which is used in the initramfs. busybox bundles most core command line utilities in a single executable. You can temporarily symlink /bin/rm to /bin/busybox: ln -s busybox /bin/rm To get a working rm (after which you can do your apt-get install --reinstall coreutils). ...


99

The kernel interprets the line starting with #! and uses it to run the script, passing in the script's name; so this ends up running /bin/rm scriptname which deletes the script. (As Stéphane Chazelas points out, scriptname here is sufficient to find the script — if you specified a relative or absolute path, that's passed in as-is, otherwise whatever path ...


88

The following command will do it for you. Use caution though. rm -rf directoryname


88

The reason why this is permitted is related to what removing a file actually does. Conceptually, rm's job is to remove a name entry from a directory. The fact that the file may then become unreachable if that was the file's only name and that the inode and space occupied by the file can therefore be recovered at that point is almost incidental. The name of ...


83

Removing the current directory does not affect the file system integrity or its logical organization. Preventing . removal is done to follow the POSIX standard which states in the rmdir(2) manual page: If the path argument refers to a path whose final component is either dot or dot-dot, rmdir() shall fail. One rationale can be found in the rm manual ...


81

The find command is the primary tool for recursive, filesystem operations. Use the -type d expression to tell find you're interested in finding directories only (and not plain files). The GNU version of find supports the -empty test, so $ find . -type d -empty -print will print all empty directories below your current directory. Use find ~ -… or find "$...


69

All rm needs is write+execute permission on the parent directory. The permissions of the file itself are irrelevant. Here's a reference which explains the permissions model more clearly than I ever could: Any attempt to access a file's data requires read permission. Any attempt to modify a file's data requires write permission. Any attempt to ...


60

I would advise against immediately installing some utility. Basically your biggest enemy here are disk writes. You want to avoid them at all costs right now. Your best bet is an auto-backup created by your editor--if it exists. If not, I would try the following trick using grep if you remember some unique string in your .tex file: $sudo grep -i -a -B100 -...


59

Using rsync is surprising fast and simple. mkdir empty_dir rsync -a --delete empty_dir/ yourdirectory/ @sarath's answer mentioned another fast choice: Perl! Its benchmarks are faster than rsync -a --delete. cd yourdirectory perl -e 'for(<*>){((stat)[9]<(unlink))}' Sources: http://stackoverflow.com/questions/1795370/unix-fast-remove-...


59

That is evil: rm -r is not for deleting files but for deleting directories. Luckily there are probably no directories matching *.o. What you want is possible with zsh but not with sh or bash (new versions of bash cannot do this by default but if the shell option globstar is enabled: shopt -s globstar). The globbing pattern is **/*.o but that would not be ...


58

Nowhere, gone, vanished. Well, more specifically, the file gets unlinked. The data is still sitting there on disk, but the link to it is removed. It used to be possible to retrieve the data, but nowadays the metadata is cleared and nothings recoverable. There is no Trash can for rm, nor should there be. If you need a Trash can, you should use a higher-...


57

You've explained the situation very well. The final piece to the puzzle is that unzip can handle wildcards itself: http://www.info-zip.org/mans/unzip.html ARGUMENTS file[.zip] ... Wildcard expressions are similar to those supported in commonly used Unix shells (sh, ksh, csh) and may contain: * matches a sequence of 0 or more ...


54

Any of these should work: sudo rm \> sudo rm '>' sudo rm ">" sudo find . -name '>' -delete sudo find . -name '>' -exec rm {} + Note that the last two commands, those using find, will find all files or directories named > in the current folder and all its subfolders. To avoid that, use GNU find: sudo find . -maxdepth 1 -name '>' -...


48

Say you want to run: rm *.txt You can just run: echo rm *.txt or even just: echo *.txt to see what files rm would delete, because it's the shell expanding the *.txt, not rm. The only time this won't help you is for rm -r. If you want to remove files and directories recursively, then you could use find instead of rm -r, e.g. find . -name "*.txt" -...


46

The latest (as of 2013) version of the POSIX spec for the rm utility is here (and the previous one there) and forbids the deletion of . and ... If either of the files dot or dot-dot are specified as the basename portion of an operand (that is, the final pathname component) or if an operand resolves to the root directory, rm shall write a diagnostic ...


45

This almost made me wince. You might want to stop pointing that shotgun at your foot. Basically any kind of parsing of ls is going to be more complicated and error-prone than established methods like find [...] -exec or globs. Unless someone installed a troll distro for you, your shell has Tab completion. Just type rm google and press Tab. If it doesn't ...


44

Ok, according to your comment to ire_and_curses, what you really want to do is make some files immutable. You can do that with the chattr command. For example: e.g. $ cd /tmp $ touch immutable-file $ sudo chattr +i immutable-file $ rm -f immutable-file rm: remove write-protected regular empty file `immutable-file'? y rm: cannot remove `immutable-file': ...


44

-delete will perform better because it doesn't have to spawn an external process for each and every matched file. It is possible that you may see -exec rm {} \; often recommended because -delete does not exist in all versions of find. I can't check right now but I'm pretty sure I've used a find without it. Both methods should be "safe". EDIT per comment ...


43

The correct syntax in bash is the following: rm /tmp/!(lost+found) As @goldilocks wrote in the comments, the original command makes an expansion on the query (it deletes all the files in the /tmp folder, then goes on, and deletes all the files in the current working folder, in your case the home folder). You can try to check if you can recover some of ...


42

Combining GNU find options and predicates, this command should do the job: find . -type d -empty -delete -type d restricts to directories -empty restricts to empty ones -delete removes each directory The tree is walked from the leaves without the need to specify -depth as it is implied by -delete.


40

List the directories deeply-nested-first. find . -depth -type d -exec rmdir {} \; 2>/dev/null (Note that the redirection applies to the find command as a whole, not just to rmdir. Redirecting only for rmdir would cause a significant slowdown as you'd need to invoke an intermediate shell.) You can avoid running rmdir on non-empty directories by passing ...


39

Find can execute arguments with the -exec option for each match it finds. It is a recommended mechanism because you can handle paths with spaces/newlines and other characters in them correctly. You will have to delete the contents of the directory before you can remove the directory itself, so use -r with the rm command to achieve this. For your example you ...


36

find is very useful for selectively performing actions on a whole tree. find . -type f -name ".Apple*" -delete Here, the -type f makes sure it's a file, not a directory, and may not be exactly what you want since it will also skip symlinks, sockets and other things. You can use ! -type d, which literally means not directories, but then you might also ...


35

If you're running bash ≥4.3, then if you have backups, now would be a good time to find them. I assume you're using Bash. The !(...) filename expansion pattern expands to every existing path that doesn't match the pattern at the point it's used. That is: echo rm -f !(/var/www/wp) expands to every filename in the current directory that isn't "/var/www/...


34

Be careful with special file names (spaces, quotes) when piping to rm. There is a safe alternative - the -delete option: find /path/to/directory/ -mindepth 1 -mtime +5 -delete That's it, no separate rm call and you don't need to worry about file names. Replace -delete with -depth -print to test this command before you run it (-delete implies -depth).


33

Make the file immutable with the i attribute. chattr +i file.desktop see man chattr for more information.



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