I've got a disk mounted at /mnt/newfolder that contains a large folder named 'do_not_delete'. I want to wipe everything else from this disk, as it holds a lot of old system files and hidden junk from a previous server setup. What's the safest way to delete everything except for this one folder and its contents? I've looked into using commands like `find` with `exec rm -rf`, but I'm concerned about messing up due to my lack of experience. Any advice on how to do this safely?
5 Answers
If you're not feeling super confident with the command line yet, a safer approach might be to move your 'do_not_delete' folder to a temporary location, delete everything else, and then move it back. This way, you can avoid potential mistakes while messing around with delete commands. It's a simpler solution than trying to execute a complicated command right away.
If you're using zsh, enabling extended globbing can help you. Just run `setopt extendedglob`, cd to '/mnt/newfolder', and then type `echo rm -r *~do_not_delete` to see what would be deleted. If it looks good, you can remove the echo to actually run it, but maybe consider adding `-i` to confirm each file deletion.
A lazy yet effective workaround is to create a tmpfs mount at the directory you want to keep. This way, when you attempt to delete everything else, the system won't let you remove the directory because it's busy. Just make sure you unmount it afterward!
It’s always smart to change the ownership of your sensitive folder to root and give it restrictive permissions. Then, delete everything as a normal user without sudo. This way, you'll avoid accidental deletions!
Another option is to create a new directory called '/mnt/newfolder/to_delete', move everything except 'do_not_delete' into there, and then just delete that new directory. It's a pretty straightforward way to keep your important folder safe.

Related Questions
Can't Load PhpMyadmin On After Server Update
Redirect www to non-www in Apache Conf
How To Check If Your SSL Cert Is SHA 1
Windows TrackPad Gestures