How To: Android ADB shell basics

Yesterday I did a beginner’s guide to installing and using android ADB, I figured I would expand on that with a basic guide to adb shell.

Overview

In short, ADB shell allows you to run a shell on your phone, issuing commands from your computer and having the phone execute them. It’s the same basic idea as the shell environment in linux/osx terminal. It’s a command line environment, running on your phone, that you communicate with via the USB connection to your computer.

Setting up

In order to take advantage of the ability to run a shell on your phone to the fullest, you need to be rooted, and for the purpose of this article, I’m going to assume you are.

You also really need to have busybox installed. While android has some basic command line utilities (generally located in /system/bin), busybox has basically every command line utility under the sun. This market app installs busybox. Just install and run it and grant it superuser permissions, then hit “install”. If for any reason it gives you a choice, you want it installed to /system/bin.

For some reason, on my dell streak, it hangs up on installation, never telling me it’s done, even though it does successfully install busybox. If it hangs up on you too, failing to do anything after 2 or 3 minutes, just reboot. We’ll know soon enough if it worked.

Start a shell session

To start a shell session, connect your phone to your computer, making sure you have USB debugging enabled on your phone (Settings > Applications > Development). Open terminal and type

adb devices

It should return a device ID, indicating that your phone is connected, and ADB detects it. Now enter

adb shell

You should be rewarded with a dollar sign. Let’s make sure you’re rooted. Pick up your phone, unlock the screen, and keep it in your hand, then type:

su

The first time you do this, you’ll be prompted, on your phone, to grant adb shell superuser permissions. Obviously make sure “always allow” is ticked and select “ok” to grant permissions. It should return a pound sign in place of a dollar sign now. (My rom actually gives you SU permissions by default, a lot of them do not, so you will most likely have a dollar sign when you first enter shell, then a pound sign after granting SU)

Now enter:

busybox

and you should be greeted with something like this:

If you have a pound sign, and you get that response when you enter “busybox”, you’re good to go. That’s means you’re rooted and have busybox installed.

Basic Shell Usage

First, some basic commands.

cd

Change Directory. Just like in any other *nix environment, it’s one of the basics of terminal navigation. When you start a shell session, you’ll be in root, ( / ). From there, let’s say you want to move to the SD card.

cd /sdcard

Now you should be in the /sdcard/ folder. To verify, use

pwd

pwd = Print Working Directory. In other words, output your current location within the file system.

Now let’s see what’s on the sd card using

ls

ls = list. This command will list all files in the current directory.

Let’s say I want to start an adb shell session, get root permissions, go to a folder on the sdcard named “tempfolder”, make sure I’m in the right folder, and then list all files in that folder.

For directories with lots of files, use

ls | more

(The vertical line is above enter, lol)

This will list all files and folders, showing only what fits in the terminal window until you press a key to show more. The space bar will get it to load the next page of results.

For example, just using

ls

with no options in the system/bin folder results in this:

I’m not going to screenshot the whole thing, but based on the scroll bar on the side, you get the idea as to how many files it just spit out. Using the more option cuts the results up into much more manageable chunks:

Starting to make sense?

Busybox usage

Busybox is a bunch of unix terminal utilities that allow you do pretty much anything you want. There’s no way I could even remotely begin to cover all of the utilities included in busybox, much less all of the options for them, so I’ll just hit a few of the very common ones. The wikipedia article has a full list, and you can click a command and learn more about it.

cp

cp = copy. It’s exactly what it sounds like. Let’s say you have a file name myfile.txt in /sdcard/tempfolder, and you want to copy it to /sdcard/tempfolder2

busybox cp /sdcard/tempfolder/myfile.txt /sdcard/tempfolder2/

That will result in the original myfile.txt remaining in /sdcard/tempfolder and a copy of myfile.txt in /sdcard/tempfolder2

mv

mv = move

Again, just what it sounds like. Using the example for cp above:

busybox mv /sdcard/tempfolder/myfile.txt /sdcard/tempfolder2/

will move the original file from /tempfolder to /tempfolder2. The file will no longer be in the original location.

rm

rm – remove

Let’s say we want to get rid of myfile.txt

busybox rm /sdcard/tempfolder2/myfile.txt

mount

Not a lot of people will use this command other than to make a file/folder writable.

busybox mount

will list all mount points, but by far the most common usage of the mount command will be

busybox mount -o remount rw /system

That uses busybox to run the mount command with the option (-o) to remount the directory /system with the ability to both read and write (rw). In other words, if you can’t write to some file in the /system directory, or can’t modify some file located in that directory for a hack, that will remount the /system directory so that it’s writable. It should revert to read only (ro) after you reboot, or you can remount it yourself:

busybox mount -o remount ro /system

mkdir

make directory. This makes a new folder. For example, if you want to make a directory on your sdcard named “vids”

busybox mkdir /sdcard/vids

rmdir

Remove directory. Basically the same thing as the “rm” command, just for directories instead of just files. Let’s say we decide we don’t want that “vids” folder we just made…

busybox rmdir /sdcard/vids

Note that rmdir won’t remove a folder unless it’s empty. To do that we use rm and the recursive option. If you already put a video in the /sdcard/vids folder, and you want to just get rid of the folder and the video in it:

busybox rm -r /sdcard/vids

I’m starting to get cross-eyed from staring at my terminal window, so I’m going to stop for now, I’ll continue with fastboot usage and likely some more adb shell stuff within the next day or two. Hope this helps a little.

1 comment to How To: Android ADB shell basics

You must be logged in to post a comment.