views:

264

answers:

5

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.

A: 
/subdir/bin/mv /subdir /

or am I missing something in your explanation?

Tony Miller
If `/bin/mv` is dynamically linked (as is common these days), it won't load if `/lib` has also been moved.
ephemient
Yes, it is dynamically linked.
KullDox
+5  A: 

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
Correct, the ld-so place is an issue.
KullDox
A: 

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
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
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
Better yet, what commands do you have?
Noufal Ibrahim
+8  A: 
/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
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
+2  A: 

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
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
Wow, amazing stuff. I shall study your bash wizardry!
John Kugelman