Thursday, December 8, 2011

Basic Linux/Unix Shell Commands - Android

This guide aims to give you a background on some of the linux commands available to you once you obtain an adb shell. In order to obtain an ADB shell, you must have adb set up on your computer (all ADB really consists of in windows is adb + 2 dlls + adb drivers. However, it makes sense to fully set up the Android SDK if you plan on developing for Android. To do that, visit this guide: http://www.freeyourandroid.com/guide/install_android_sdk
 NOTE = All commands in linux/unix are CASE SENSITIVE.

cd - Change Directory

To change to any directory, simply type:
cd dirname 
dir name is the path. For instance, to switch to system/etc, type:
cd /system/etc
'..' will allow you to go back one directory. In our example, typing:
cd ..
would take you back to /system

ls - List Files

To list all the files in whatever directory you're in, simply type:
ls
pressing enter after, of course. This will list all NON-HIDDEN files/directories.
ls /system/etc
will list the contents of system/etc
ls -l 
will list all NON-HIDDEN files in your current directory with additional details
ls -a 
will list all files/directories(including hidden) within your current directory

su - SuperUser

The standard adb shell (unless you're on an insecure kernel/ramdisk), will be a non-root shell ($)
To obtain root priviliges (if your phone is rooted), simply type:
su
on obtaining superuser priviliges, you will be presented with a # symbol, which represent a root shell.

chmod - Change Mode

The two most commonly used 'modes' you'll come across in Android are '777' and '755' 
These numerical pemissions have different meanings, of course. This can be a little confusing, so I hope this explains it in a simple to understand way.
As you can see, there are three numbers in the following example; 'chmod 755'
So, to break that down: The first number in the sequence, '7', represents the USER (aka, you). The second number in the sequence, '5', represents the GROUP (users who are members of the file's group) and the third number, '5' represents OTHERS (aka, everyone else). 
Now to explain why they are 755, and the significance of those numbers, see the following list:
7     Full
6     Read/Write
5     Read/Execute
4     Read Only
3     Write/Execute
2     Write Only
1     Execute
0     None
So in the instance of 777, you can see that USER, GROUP and OTHERS have FULL access to the file in question.
To change the permissions of one file (apns-conf.xml for example, type:
chmod 777 /system/etc/apns-conf.xml

To change the permissions of all files in a directory, use the -R (recursive)option:
chmod 777 -R /system/etc

pwd - Print Working Directory

Couldn't be more simple. if you want to find out which dirctory you're currently in, type:
pwd
and press enter. 

cat - Concatenate (evolved from)

The cat command if used to list a file's contents on your screen; or pass via pipeline to use with other Linux commands. 
cat /proc/mounts
will output the various mount points in your android OS. 
Note that there are many other uses for the cat command. It can be used to copy files, for example. 

dd - Data Dump

This one's useful if you're ever looking to dump your ROM/kernel, etc. After you have run cat /proc/mounts (above) and have a su (#) shell, you can dump system in the format of its current file system to your SD card. Let' say system is on mmcblk0p1 (/dev/block/mmcblk0p1). Type the following:
dd if=/dev/block/mmcblk0p1 of=/sdcard/system.img bs=4096 

There you go - system.img is now on your SD card!

mount - As it says on the tin.

In order to mount system as read write, (using the output from cat /proc/mounts), simply type in the following:

su
mount -o rw,remount /dev/block/mmcblk0p1 /system
Note - you will see many variations of this. Just keep it simple like the command above. You don't need to specify the file system type, for example.

No comments:

Post a Comment

thank you