Live data from Hacker News

How to delete all your files

reddit.com

51–60 of 66 posts

Re: How to delete all your files

#51

Earlier quoted context omitted.

I just did that last weekend! After an allnighter, which isn't easy for me anymore, I typed: ``rm -rf. /*`` with the dot and the space reversed. When the shell threw an error in my face, I thought, "oh, an extra dot." so I deleted the dot and re-run the command. And there goes my configs and most of the dotfiles in my home dir. Luckily, I have backup for some of those, so it wasn't a complete disaster. I don't trust…

This has nothing to do with the OP, but I always rehearse my deletions with "ls -d", and after seeing the output hit the up arrow in my command history and replace it with the rm command I wanted to attempt. I also never use -r without intention - a lot of people use it habitually even when not deleting a directory. Lastly, I never -f, I just chmod first.

I tend to use the command line utility tras-cli instead for these reasons. So easy to screw up with a wild card.

Re: How to delete all your files

#52
post #46

I had a close call one time while trying to delete a directory `~/foo` and its contents. Here's what I typed — can you spot the error? rm -rf ~ /foo I realized after a second or so and hit CONTROL-C. Nothing seemed to have been deleted, so I think it may have been still winding up.

rm -rf on root will query for confirmation. Not sure if this happens on all systems or even all top level directories.

Re: How to delete all your files

#54

Another example of why shell languages are insane interfaces. Only someone who works on writing shell scripts all day could keep in their head all the things like this that could go wrong and how to defend against them.

Sadly this is one of the sanest answers. ./* or always using -- are coping mechanisms . We've internalized the UNIX warts so hard that we no longer perceive them. I've moved to writing scripts in python instead. It's horribly verbose, but at least it's predictable and doesn't require the use of noisy disclaimers on every call to defend against rare cases. There's definitely a need for a terser sane scripting language…

I've started writing in Rust, and have a small function to pass a simple string to "/bin/sh -c"

I can avoid most of the pitfalls of writing in sh, while still being able to glue external programs together.

For the record, I'm only writing it in Rust because I enjoy it, not because "Rust is the one true way".

Re: How to delete all your files

#56
post #15
post #9

An `rm -rf ` always has the potential for some desaster. Especially if you are calling it from bash history via `Ctrl + r` and hit return too fast without editing the argument after `-rf`. Actually the rm command should move files into a `~/.Trash` folder instead of immediately removing them.

At least ctrl-r shows you what command you're about to execute. I often see advice about character sequences in bash that expand to the last command or arguments in the last command, but I have never felt comfortable using them because I can't double-check the command.

I have absolutely no clue what I've done to make it work this way (if anything), but in zsh running a command with !$ (last word from the previous command) I get the opportunity to edit the interpolated version first.

Re: How to delete all your files

#57
post #50

Earlier quoted context omitted.

rm is not like explorer.exe and is rather fast with destruction, so I guess you must have deleted some dotfiles.

Could be. This was on a Mac, around 2013, so it should have been pretty snappy. Whatever it got I never noticed. Maybe it was trawling through a bunch of build detritus in `~/.cpan` or something.

I accidentally did the same on my Mac via a shell script where I assumed mktemp worked the same as gnu mktemp, it does not. IIRC it was my /Applications directory that was first to be hit; most of the directory was unaffected due to file permissions but it did delete some files despite me catching thee error and sending sigkill within seconds.

Re: How to delete all your files

#58
post #46

I had a close call one time while trying to delete a directory `~/foo` and its contents. Here's what I typed — can you spot the error? rm -rf ~ /foo I realized after a second or so and hit CONTROL-C. Nothing seemed to have been deleted, so I think it may have been still winding up.

hah, i had a tool where i configured ~/something as a target directory in its config. except that the tool did not understand ~ and literally created a directory named '~'

guess what i typed to remove it?

i lost a lot of work that day.

since then i developed the habit to always only use rmdir for directories, and first go into the directory to remove files with rm.

i also avoid rm * by finding some common parts like an extension that most files share.

i don't want rm * to be in my history where i might accidentally call it.

Re: How to delete all your files

#59

Earlier quoted context omitted.

Yes thanks. I thought running rsync with a "*" looked odd, but didn't completely understand why until now.

Funnily enough, rsync is one of the commands for which you might be more tempted to use * instead of going up a directory, because the relative path names go over the wire.

besides ./* i also like to use star.ext or even a* b* c* depending on the contents of the directory.

rsync also used the local/only/path/./local/and/remote/path convention where the path before the /./ is not sent to the remote side

Re: How to delete all your files

#60
post #19

“Because the share contains a file named exactly "--delete", and since it gets sorted first, rsync thinks it to be an argument and deletes everything.” Also because Unix made the mistake (edit: calling this a mistake may be unfair) of having the shell expand wildcards. If it had provided a library for doing that, the “rsync thinks it to be an argument” part wouldn’t happen. Alternatively, a file system could sort hyp…

In DOS/Windows the shell does not expand as the sibling comment says (which puts some burden on the programmer - I had to use a Windows DLL in a perl script once to replicate Windows-style argument handling) but as most people know they also have paths with \ and options with /, and / is not a valid filename character.

This use of / for options goes way back to VMS apparently[0] and changing the path separator to avoid ambiguity does seem sensible. Of course, Windows has its own quirks like CON.

0: https://superuser.com/a/176395/206248

Post reply on HN