6

There was a situation when somebody moved the whole rootdir into a subdir on a remote system, thus all the system tools like cp, mv, etc didn't work anymore. We had an active session though but couldn't find a way to copy/move the files back using only bash built-ins.

Do somebody know of a way to achieve this?

I even thought about copy the cp or mv binary in the currentdir with

while read -r; do echo $LINE; done

and then redirect this to a file, but it didn't work. Guess because of all the special non printable chars in a binary file that can't be copied/displayed using echo.

thanks.

KullDox
  • 353
  • 1
  • 3
  • 11

6 Answers6

12
/newroot/lib/ld-linux.so.2 --library-path /newroot/lib \
    /newroot/bin/mv /newroot/* /

(Similar for Solaris, but I think the dynamic linker is named ld.so.1 or something along those lines.)

Or, if your shell is sh-like (not csh-like),

LD_LIBRARY_PATH=/newroot/lib /newroot/bin/mv /newroot/* /
ephemient
  • 198,619
  • 38
  • 280
  • 391
  • Actually maybe the crle command will be a better solution looking at http://chrismiles.info/systemsadmin/solaris/articles/ld-path-customisation-on-solaris/ Need to check if it's available. – KullDox Apr 08 '10 at 08:47
5

If you have prepared with sash pre-installed, then that is static and has a copy built-in (-cp).

Otherwise LD_LIBRARY_PATH=/copied/to/path/lib /copied/to/path/bin/cp might work?

I think it might have a problem with not having ld-so in the expected place.

Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137
3

Here's a reasonable ghetto replacement for cp. You'll want echo -E if the file ends with a new line (like most text files), echo -nE if it doesn't (like most binaries).

echo -nE "`< in.file`" > out.file
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    This skips (at least) bytes with 0x00 value. For an example of the trickery you have to resort to, see [my answer here](http://stackoverflow.com/questions/2003803/show-hexadecimal-numbers-of-a-file/2004276#2004276) wherein I present a version of `hexdump` written entirely using Bash builtins. And then, as **ephemient** said: "how would you `chmod +x` the copy?" – Dennis Williamson Apr 08 '10 at 00:48
1

Old thread, but got exactly the same stupid mistake. /lib64 was moved to /lib64.bak remotely and everything stopped working.

This was a x86_64 install, so ephemient's solution was not working:

# /lib64.bak/ld-linux.so.2 --library-path /lib64.bak/ /bin/mv /lib64.bak/ /lib64
/bin/mv: error while loading shared libraries: /bin/mv: wrong ELF class: ELFCLASS64

In that case, a different ld-linux had to be used:

# /lib64.bak/ld-linux-x86-64.so.2 --library-path /lib64.bak/ /bin/mv /lib64.bak/ /lib64

Now the system is salvaged. Thanks ephemient!

big_gie
  • 2,829
  • 3
  • 31
  • 45
0
/subdir/bin/mv /subdir /

or am I missing something in your explanation?

Tony Miller
  • 9,059
  • 2
  • 27
  • 46
0

If you have access to another machine, one solution is to download and compile a Busybox binary. It will be a single binary contains most of the common tools you need to restore your system. This might not work if your system is remote though.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
  • Yeah, we've tried to look for something similar. Looked to download the SUNWsutl package but couldn't find any. But even so, how could we upload it to the system? – KullDox Apr 08 '10 at 07:04
  • Any services open on the remote machine (nmap) which you can use to get a binary across? Doesn't the LD_LIBRARY_PATH sugestion from Douglas help for the basic commands? – Noufal Ibrahim Apr 08 '10 at 07:11