by theptip · 9y ago · 0 comments
`mv x $(date +%Y%m%d_%s_)x`
where: %Y - 4 digit year
%m - 2 digit month
%d - 2 digit day
_ - underscore literal
%s - linux timestamp (seconds since epoch)
This ensures that the versions you're 'removing' will be lexically sorted from newest to oldest in a way that is easy to interpret and also works if you need to try more than once in a day.In case it's not apparent to some, this command moves the directory (or file) called
'x'
to something like '20170131_1485916040_x'
Then when you're all done (i.e. production is humming and passing tests, no need to ever rush), you can delete the timestamped version, or if space is plentiful, move the old file to an archive directory as an extra redundancy (i.e. as an extra backup, not in lieu of a more thorough backup policy).At some level you have to run an rm, and you better hope you do it right in the middle of an emergency with people breathing over your shoulder.
In an ideal world, this wouldn't ever happen, but it does. Inherited/legacy systems suck.
pushd dir ; find . -type f -ls | less ; find . -type f -exec rm '{}' \; ; popd ; rm -rf dir
It takes a lot longer to do, but I've seen and made enough mistakes over the years that the forced extra time spent feels necessary. It's worked pretty well so far -- knock knock. find ... -delete
avoids any potential shell escaping weirdness and saves you a fork() per file.Very interesting article: https://www.dwheeler.com/essays/fixing-unix-linux-filenames.....
https://gitlab.com/willemmali-sh/chegit/blob/master/chegit#L...
I admit the code can look a little weird, but it was because I had some rather tight contrainst: 1 file, all filenames `\0` separated internally and just POSIX `sh`. I still wanted to reuse code and properly quote variables inside `xargs` invocations (because `sh` does not support `\0`-separated read's), so I ended up having to basically paste function definitions into strings and use some fairly expansive quotation sequences.
I would add a step where you dump the output of find (after filtering) into a textfile, so you have a record of exactly what you deleted. Especially when deleting files recursively based on a regular expression that extra step is very worthwhile.
It's also a good practice to rename instead of delete whenever possible. Rename first, and the next day when you're fresh walk through the list of files you've renamed and only then nuke them for good.
Unfortunately, the answer most places is that the diagnostic account (as opposed to the corrective action account) is fully privileged (or worse, root).
Snapshots are not a backup strategy, but they make me sleep better at night regardless.