Tag Info

Hot answers tagged

171

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 ...


110

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


97

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). ...


80

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 ...


59

All rm needs is write 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 execute a ...


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 '>' ...


50

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


49

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 ...


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

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 ...


38

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 ...


36

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': ...


36

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 ...


35

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" ...


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 ...


30

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 ...


29

rm -r works on each of its arguments in turn. If an argument is a directory, it lists the directory (with the opendir and readdir functions or some equivalent method), and operates on each entry in turn. If an entry is a directory, it explores that entry recursively. This is exactly the same method that other applications use to traverse directories ...


28

The file has a name, but it's made of non-printable characters. If you use bash, you can try to remove it by specifying its non-printable name. First ensure that the name is right with: ls -l $'\177' If it shows the right file, then use rm: rm $'\177' Another (a bit more risky) approach is to use rm -i -- * . With the -i option rm requires confirmation ...


27

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


27

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 ...


27

Someone on Twitter suggested using -delete instead of -exec rm -f{} \; This has improved the efficiency of the command, it still uses recursion to go through everything though.


27

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: ...


26

rm /* should delete very little. There is no -r flag in there that would recursively delete anything, and without it directories will not be deleted (and even if directories were deleted, only empty ones can be deleted). This answer is predicated on the assumption that you did not run rm -rf /*. The only files in the root filesystem of consequence may be ...


25

The !(lost+found) in your rm command was probably the fatal mistake: 1978 rm -rf /tmp/* !(lost+found) 1979 sudo rm -rf /tmp/* !(lost+found) I don't know exactly what bash is doing with that, but this command below prints everything in my /tmp/ and also everything my current directory (which is currently ~): echo /tmp/* !(lost+found)


24

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 ...


24

You can just use standard globbing on the rm command: rm -- *\ * This will delete any file whose name contains a space; the space is escaped so the shell doesn't interpret it as a separator. Adding -- will avoid problems with filenames starting with dashes (they won't be interpreted as arguments by rm). If you want to confirm each file before it's ...


22

It's basically removing backup files. *~ means all files ending in ~. Many Unix/Linux systems programs create backup files that end in ~. For example, the emacs and nano editors automatically save a backup copy of each file you edit. When it saves a file, the old version gets saved using the file name with a tilde (~) added to the end. Vim will do the ...



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