How can I safely delete everything on a disk except one specific folder?

0
10
Asked By TechWizard99 On

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

Answered By Curious_Coder33 On

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.

Answered By CommandLineNinja On

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.

Answered By SlackerTechie On

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!

Answered By BackupBoss99 On

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!

Answered By LinuxLover42 On

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.