How to delete all your files
reddit.com
reddit.com
http://web.mit.edu/~simsong/www/ugh.pdf page 28 (68)
>Some Unix victims turn this filename-as-switch bug into a “feature” by keeping a file named “-i” in their directories. Type “rm *” and the shell will expand this to “rm -i filenamelist” which will, presumably, ask for confirmation before deleting each file.
[0]: https://www.defensecode.com/public/DefenseCode_Unix_WildCard... [1]: https://news.ycombinator.com/item?id=8189968 [2]: https://news.ycombinator.com/item?id=17376895
> never use only * as a wildcard. A ./* would save you in most cases
My friend typod that for /* in an rm -rf command but caught it just in time. They seemed like unnecessary characters to me if it does the same as *, and I don't think they do it anymore, but now I'm not sure which is worse...
I suppose good backups just go a long way whatever you do.
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 myself doing ``rm`` in commandline anymore.
rsync also used the local/only/path/./local/and/remote/path convention where the path before the /./ is not sent to the remote side
Maybe we need transactional file systems. At work, on our databases, when I'm running an ad-hoc update or delete on something that's not trivial to recover then in my DB tool I will often have something like:
BEGIN TRANSACTION
UPDATE ...
SELECT ...
-- COMMIT TRANSACTION
If the select validates that the update was good (and the update doesn't say "1000000 rows updated"), I'll just highlight COMMIT TRANSACTION and hit run. This isn't a perfect solution since I either need to hold the transaction open or run the update twice (first time immediately followed by a rollback), but a blip in accessibility is better than having to restore the latest nightly backup and run any processes that updated the table since it was taken.From https://liw.fi/linux-anecdotes/
But if and of are "easy", since they stand for "input file" and "output file"?
Yes, but which hard drive device is which isn't as straightforward. My fault, of course...just got them mixed up.
As an example, here's my #2 result for, "Linux command line create bootable usb drive from iso":
https://www.tecmint.com/create-an-iso-from-a-bootable-usb-in...
It instructs you to run this command, despite explaining what "if" and "of" mean in the following paragraph:
>sudo dd if=/dev/sdb1 of=/home/tecmint/Documents/Linux_Mint_19_XFCE.iso
Proofreading is harder and less profitable than SEO.
But I agree about "copy paste instructions". I remember an intern at work asking me how to switch between virtual terminals on Linux, I told her Ctrl-Alt-Fx, I wanted to explain what virtual terminals are and how the shortcuts are probably configurable, but at that point she already stopped listening...
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.Whatever it got I never noticed. Maybe it was trawling through a bunch of build detritus in `~/.cpan` or something.
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.
mv is a clever idea. rename to something safe, which can be undone if you get it wrong.
i use a similar approach when deleting a lot but not all files from a directory. i first move the files into a new directory, double check that every file is in the right place and then delete the new directory safely.
For example, expand wildcards into a separate list of files that can become controlled commands ("rm -f A/file1", "rm -f A/file2", "rmdir A", ... instead of "rm -Rf ..."). This way, if directory "A" contains anything you didn’t expect, the "rmdir" fails at the end; and, someone can review the list of proposed files to be deleted before you run. Oh, and instead of having that sinking feeling of "rm" taking “too long” to run, your command is merely in the process of constructing a list of 1000 unexpected files instead of blowing away half your disk with shocking efficiency.
Also, file lists are pretty useful when you need to make minor edits (e.g. sometimes it’s a lot easier to find and exclude a few files from a list that you don’t want to touch, as opposed to describing those in a wildcard or search).
Depending on the task (and assuming no filesystem caching) it can be faster to gather up a list once and pass the composed list in to a whole series of commands. This is also good if you technically want the list to remain frozen even if the filesystem is changing underneath you, e.g. new files being added somewhere in the tree.
I remember thinking that way when I started using the command line, but I now respectfully disagree. The `mv` command already does that, and they way `rm` work is actually necessary sometimes (eg. I couldn't live without it now that I'm deleting huge files all day long).
Anecdote: when Windows was my daily driver, emptying the trash had become a mechanical task just after deleting a bunch of files anyway, so it didn't provide more safety than these useless “are you sure you want to X” dialogs you click without thinking about the question. I have lost a pair of files just because of that. When I have to use Windows these days, I mostly use shift+del to delete (ie. bypass the trash).
The nicest answers to data loss I've found so far are filesystem snapshots and proper backups. They not only protect from the occasional `rm` mistype, but from the mistyped shell redirections and bad programs as well.
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 hyphens last when reading directory entries, but I’m not sure that would be a good idea. It would make problems rarer, but that also might mean fewer users would know when and how to avoid them. It certainly wouldn’t help with other file systems.
The problem is of course that wildcard handling ends up being inconsistent between tools or entirely missing, although in practice this became less common as various Win32 calls related to file operations had internal wildcard handling, so applications got a basic form "for free".
As with many things in computing it's hard to say that either approach is superior to the other. PowerShell carries on the DOS tradition of not expanding wildcards in the shell, but provides a more complete and standardized wildcard implementation as part of the API to improve consistency.
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.
command *
(or similar syntax) expanded to something like command /some/fd
where calls to read(/some/fd)
produced "expansion_one␀expansion_two␀expansion_three␀...␀"
Though exactly how one would plumb this is its own question. $ parted
<do a lot of work>
whoops, all the I erased and recreated were... my root disk!I couldn't figure otu the partition table of the running disk, but I was able to rsync all the data elsewhere and recover it.
IMHO if you invoke parted - WITH NO ARGUMENTS - it should not make a choice for you.
https://www.defensecode.com/public/DefenseCode_Unix_WildCard...
BTW, shellcheck detects this sort of thing:
rsync -r ./ ~/Documents/
Why would I even need to use *, if I wanted all files from a directory?I guess every generation of Unix users will have to independently rediscover this the hard way in perpetuity.
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, but I haven't found one yet.
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".
...In a way that can be checked in source control and reviewed?
CLIs are not bad.