Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run Linux commands on an Android device?

Tags:

android

shell

adb

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?

like image 802
Wendy Chen Avatar asked Apr 23 '12 09:04

Wendy Chen


People also ask

Can I run Linux commands on an Android phone?

There are several apps that let you use your Android smartphone to practice Linux commands to connect to a remote server via SSH. Of course, you should not expect it to replace your regular Linux terminal emulators available for desktops.

Can you run commands on Android?

Use the Run Program feature to remotely execute Apps / commands on an Android device. (feature also works where the target is PCs or Win Mobile / CE devices).

How to install Linux on Android devices?

This process of installing Linux on an Android device is done through Ubuntu. First, download and install UserLand in the Play Store, then start this app. Next, tap on the Ubuntu, then OK, and tap on Allow for granting all of the required app permissions.

Is it possible to run Linux on Android?

If you talk about running Linux commands in Android OS then answer is “Almost YES”. You can check by downloading Terminal utility from Android Google Play store. You can run most of the Linux commands but commands which give access to core system don't give output… So it is basically a closed system untill you have a rooted Android phone or tablet.

What can you do with a Linux terminal on Android?

The terminal is most of the time termed as Swiss-Knife of Linux-based OS, cause through the power of terminal you can do almost anything on an OS, you can even play games on it, install software and much more cool stuff. However, the power of the terminal isn’t that powerful in Android.

How do I set up a GNU/Linux environment on my Device?

This guide assumes a non-rooted device is being used. To Set up a GNU/Linux environment you will need to download GNURoot Debian and Xserver XSDL followed by applicable Linux commands to complete the process. GNURoot allows you to create a Linux environment within a host OS.


3 Answers

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

Process proc = Runtime.getRuntime().exec("your command");
like image 56
waqaslam Avatar answered Oct 28 '22 11:10

waqaslam


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.

like image 34
keyboardsurfer Avatar answered Oct 28 '22 11:10

keyboardsurfer


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..

like image 34
onur güngör Avatar answered Oct 28 '22 11:10

onur güngör