开发者

Move/copy files/folder in linux/solaris using only bash built-ins

There was a situation when somebody moved the whole rootdir into a subdir开发者_JAVA百科 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.


/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/* /


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.


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


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!


/subdir/bin/mv /subdir /

or am I missing something in your explanation?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜