12

On some Android devices, in the ADB shell, I can only run echo, cd, ls. When I run:

tar -cvf //mnt/sdcard/BackUp1669/apk/test.tar /mnt/sdcard/test.apk

Or the command cp, it returns:

sh: tar: not found

Why can I not run these commands? Some devices support these commands. My end goal is to copy a file from the /data/data folder to SD card. I got su and I got the following code:

int timeout = 1000;
String command = "tar -cvf /" + Environment.getExternalStorageDirectory() + "/cp/"
        + packageName + ".tar" + " " + path;
DataOutputStream os = new DataOutputStream(process.getOutputStream());
BufferedReader is = new BufferedReader(new InputStreamReader(new DataInputStream(
        process.getInputStream())), 64);

String inLine;
try {
    StringBuilder sbCommand = new StringBuilder();
    sbCommand.append(command).append(" ");
    sbCommand.append("\n");
    os.writeBytes(command.toString());
    if (is != null) {
        for (int i = 0; i < timeout; i++) {
            if (is.ready())
                break;
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (is.ready()) {
            inLine = is.readLine();
        } else {
        }
    }

} catch (IOException e) {
    e.printStackTrace();
}

It always stops in is.ready(), and when I changed it to process.waitfor() it also stopped. Why?

1
  • 3
    You do realize, that the android shell is far more restricted than a standard Linux os right? Commented Apr 23, 2012 at 9:18

7 Answers 7

14

As far as i know, the only way to run shell commands is:

Process proc = Runtime.getRuntime().exec("your command");
Sign up to request clarification or add additional context in comments.

Comments

12

You can run Linux commands on Android. But there are usually just very few pre-installed. If you want to add more commands you might want to root your device and install busybox on it.

This is not for productive use within an application but can help you to work with your device.

2 Comments

Thanks, I know where the problem is. But my problem still not been resolved.
So how can I run locate command on Android for searching files? It seems this is not an applet in BusyBox.
2

If you have the binaries for your system, you can run anything on your system.

Saying that you have to understand that you have to find the binaries for tar.

Look here http://forum.xda-developers.com/showthread.php?t=872438

And possibly other places..

Comments

0

You can probably get this done by using a Terminal Emulator app. As you wrote above, I don't know how well DOS commands will work. But, a Terminal Emulator works without root.

1 Comment

You should provide more context to your answer. If you do not provide much context then give the questioner a suggestion in the comment section.
0

You can install Termux app on your android device and run Linux command by using that app

Comments

-1

Install busybox, then type the command in the following format:

busybox [linux command]

You cannot use all the linux commands without busybox, because Android doesn't have all the binaries that are available in a standard linux operating system.

FYI, a binary is just a file that contains compiled code. A lot of the default binaries are stored in /system/bin/sh directory. All these commands like 'cp' 'ls' 'get' etc, are actually binaries. You can view them through:

ls -a /system/bin/sh

Hope this helps.

Comments

-2

In reply to Igor Ganapolsky, You would have to have a database set up for locate.

Probably find would be adequate for your needs.

example:

find -name *.apk

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.