Accidentally renamed /bin Help!

I have an emergency, Linux and Bash beginner here and messed up trying to write a script to rename some files. The loop accidentally went up the path (ran the script in a folder in Desktop) and renamed /bin to /D_bin (D_ was the prefix I added) so now the system can't use /bin contents, so no bash, no mv to rename, no sudo... The files in /D_bin are ok, not renamed, and can copy-paste them but can't create folder /bin again without bash. The system looks stable but very few things work and have no access to files in Desktop.

The other folders of / like /lib /sbin /etc seem to be ok too, and the graphical desktop is still there. I'm afraid of restarting because I don't know if it will be able to boot.

Is there a shell in root or a way to rename /D_bin back to /bin? Need help please, very important work compromised

My suicidal script :$ :

#!/bin/bash
files=~/Desktop/folder_1/*

for j in $files
do
    cd $j
    for i in 10n*  #file names starting by 10n
    do
       find * -maxdepth 0 ! -path . -exec mv {} D_{} \;
    done
    cd ..
done

:( Thanks!!!!


Solution 1:

There are several ways to fix this issue.

If you have access to a shell (any open terminal), run:

sudo /D_bin/mv -T /D_bin /bin

sudo is in /usr/bin so there is no need to run it with absolute path.

The other thing you can do is, adding the /D_bin to your PATH environment variable, like this:

export PATH=$PATH:/D_bin

If you don't have access to any shell:

  1. reboot the system
  2. when grub appears press e to edit the grub
  3. at the end of the line which starts with linux, add:

    init=/D_bin/bash
    
  4. press CTRL+x

Now you will be dropped into a bash shell, you should remount file system as read and writable.

/D_bin/mount -o remount,rw /

And move the D_bin directory to bin:

/D_bin/mv -T /D_bin /bin

Then reboot the system.

It should work, but if nothing worked for you, you still can boot the system with a live ubuntu disk/usb and fix the issue.

Solution 2:

To fix this problem if you have no running terminal open, I would first try to find a “shell substitute” that you can use instead of bash. Python is in /usr/bin, so that should still work.

Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call(["sudo", "/D_bin/mv", "-T", "/D_bin", "/bin"])

If that doesn't work, I'd just straight boot from live CD / USB and fix everything from a known-sane running environment.

As a general advice, I'd second Jonathan Leffler in the comments: never use cd .. in scripts, it can easily lead to such problems. Better only cd into the $j directory within a subshell, this way you don't have to worry about getting back.

#!/bin/bash
files=~/Desktop/folder_1/*

for j in $files
do
  (
    cd "$j"
    for i in 10n*  #file names starting by 10n
    do
       find * -maxdepth 0 ! -path . -exec mv {} D_{} \;
    done
  )
done

Also, of course, don't run stuff as root unless absolutely necessary.