How do I retrieve all folders using su in one command?
Without root I use
adb shell "ls -R / | grep /"
And when I try
adb shell su "ls -R / | grep /"
it doesn't work.
How must the syntax be to work?
Use the -c option of su to execute a command. You also need two levels of quotes --
adb shell "su -c 'ls -R / | grep /'"
Related
Hi guys am new to the shell scripting.. please do not mind if acted like a noob.
The following error keeps poping up when am trying to run the shell script on my android device
#!/bin/bash
for i in $(seq 100)
do
echo "Time: $i"
adb -s $* shell am start com.android.camera/.Camera -W -S
adb -s $* shell sleep 3
adb -s $* shell input tap 540 1994
adb -s $* shell input tap 540 1994
adb -s $* shell input tap 540 1994
count=`adb -s $* shell ls /sdcard/DCIM/Camera/ | wc |awk '{print $1}'`
echo Count: $count
if [[ count -gt 500 ]]; then
break
fi
adb -s $* reboot
adb -s $* wait-for-device
adb -s $* shell sleep 40
adb -s $* shell input tap 500 1200
done
Result:
adb.exe: unknown command am
adb.exe: unknown command sleep
you can't run adb commands on your android device. instead the adb binary must installed on host. the script is a linux script and can not run from windows. prepare bootable usb flash drive and run this script from any linux terminal
furthermore some of your commands requires root permissions. therefore sush must invited (and permissions granted on superuser app on touchscreen)
adb shell su -c "am start com.android.camera/.Camera -W -S"
awk is not available on android. to make it more clear that these pipes are actually running on host, quote the android commands (or even better avoid awk at all)
count=$(adb shell "ls -1 /sdcard/DCIM/Camera" | wc | awk '{print $1}')
count=$(adb shell "ls -1 /sdcard/DCIM/Camera | wc -l")
am is actually a shell script pointing to am.jar this requires full booted android. if you try to run in recovery mode you should at least run the framework
I am trying to simply count the number of files in a directory on an Android device. I am using adb shell via a Windows10 command prompt. Utilities like find, wc, tree and tail do not seem to work e.g:
`tree . | tail -1`
gives
`tmp-mksh: tree: not found`
`tmp-mksh: tail: not found`
I could write some java to do it but that would just be silly :) Any ideas?
Hey I had the same issue and I decide to use powershell instead of cmd.
you can get the count of the files in a directory with this:
$counter = $(adb shell 'ls sdcard/DCIM/Camera | wc -l') -as [int]
this will not apply to the folders inside, for that I thing you can get the folders using:
adb shell 'ls -d sdcard/DCIM/*/'
and then do a for loop with those routes ?
foreach ($folder in $(adb shell 'ls -d sdcard/DCIM/*/'))
{
$counter = $counter + $(adb shell 'ls sdcard/DCIM/Camera | wc -l') -as [int]
}
let me know how it goes,
have a great weekend!
Try the following:
$ adb shell
generic_x86:/ $ cd yourDirectory
generic_x86:/yourDirectory $ ls | wc -l
For example, in an emulator:
$ adb shell
generic_x86:/ $ cd sdcard
generic_x86:/sdcard $ ls | wc -l
10
EDIT:
For a device you can try:
$ adb shell
generic_x86:/ $ cd yourDirectory
generic_x86:/yourDirectory $ ls -l | grep . -c
I need to make a script that executes a lots of thing on Android device, my device is rooted, when I enter on the shell, I can give the command su, and it works but I need pass this command like:
adb shell "
su;
mv /sdcard/Download/app_test /data/local;
cd /data/local;
./app_test;
exit;
exit;
"
when I put some commands before the su it works, according what I read su creates a new shell that return immediately, but I need give commands on the su shell, how can I do that?
Well, if your phone is rooted you can run commands with the su -c command.
Here is an example of a cat command on the build.prop file to get a phone's product information.
adb shell "su -c 'cat /system/build.prop |grep "product"'"
This invokes root permission and runs the command inside the ' '.
Notice the 5 end quotes, that is required that you close ALL your end quotes or you will get an error.
For clarification the format is like this.
adb shell "su -c '[your command goes here]'"
Make sure you enter the command EXACTLY the way that you normally would when running it in shell.
On my Linux, I see an error with
adb shell "su -c '[your command goes here]'"
su: invalid uid/gid '-c'
The solution is on Linux
adb shell su 0 '[your command goes here]'
The su command does not execute anything, it just raise your privileges.
Try adb shell su -c YOUR_COMMAND.
By default CM10 only allows root access from Apps not ADB. Go to Settings -> Developer options -> Root access, and change option to "Apps and ADB".
1. adb shell su
win cmd
C:\>adb shell id
uid=2000(shell) gid=2000(shell)
C:\>adb shell 'su -c id'
/system/bin/sh: su -c id: inaccessible or not found
C:\>adb shell "su -c id"
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
C:\>adb shell su -c id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
win msys bash
msys2#bash:~$ adb shell 'su -c id'
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
msys2#bash:~$ adb shell "su -c id"
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
msys2#bash:~$ adb shell su -c id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
2. adb shell -t
if want run am cmd, -t option maybe required:
C:\>adb shell su -c am stack list
cmd: Failure calling service activity: Failed transaction (2147483646)
C:\>adb shell -t su -c am stack list
Stack id=0 bounds=[0,0][1200,1920] displayId=0 userId=0
...
shell options:
shell [-e ESCAPE] [-n] [-Tt] [-x] [COMMAND...]
run remote shell command (interactive shell if no command given)
-e: choose escape character, or "none"; default '~'
-n: don't read from stdin
-T: disable pty allocation
-t: allocate a pty if on a tty (-tt: force pty allocation)
-x: disable remote exit codes and stdout/stderr separation
Android Debug Bridge version 1.0.41
Version 30.0.5-6877874
for my use case, i wanted to grab the SHA1 hash from the magisk config file. the below worked for me.
adb shell "su -c "cat /sbin/.magisk/config | grep SHA | awk -F= '{ print $2 }'""
I opt for the following:
adb shell su root <your command>
e.g., to access the external storage (sd card):
adb shell su root ls storage/0/emulated
I am trying to get the PID of the process INSIDE adb shell. So, I am doing adb shell which gets me to the android shell. Now, if I were to get the PID using a regular shell I would use
adb shell ps | grep android.process.acore | sed 's/\s\s*/ /g' | cut -d ' ' -f 2
OR
adb shell ps | grep android.process.acore | awk '{ print $2 }'
I get the PID (a numeric number - 2nd field of the ps | grep android.process.acore) output.
However, if I run the above commands inside android shell(after doing adb shell), I get /system/bin/sh: sed: not found and /system/bin/sh: awk: not found errors respectively. Which means, these commands are not available inside adb shell. However, grep works.
The output of the ps | grep android.process.acore inside adb shell is:
XXX_x21 11826 441 502296 39028 ffffffff 4010ff6c S android.process.acore
I am looking for the number 11826.
How can I extract it inside adb shell?
Also, please help if there is a direct way to get the PID inside the adb shell.
Regards,
Rumit
Android versions starting with 6.0 already include pidof utility:
usage: pidof [-s] [-o omitpid[,omitpid...]] [NAME]...
Print the PIDs of all processes with the given names.
-s single shot, only return one pid.
-o omit PID(s)
Not sure if you can get the PID directly however you can try the following
set `ps |grep android.process.acore`
echo $2
This has the affect of setting the output of the ps command into variables $1, $2, $3 etc. The PID value is in $2
I tried this one and it seems to work:
adb shell "set "ps | grep android.process.media"; kill -9 $2"
adb shell pidof [package name]
or
adb shell pidof -s [package name]
-s option is for single shot, returning only one pid.
I tried this one and it seems to work:
adb shell
ps -A | grep "android.process.acore"
I plugged several android devices to my laptop. And I can list their SN by
adb devices
output:
List of devices attached
015d4a826e0ffb0f device
015d4a826e43fb16 device
015d41d830240b11 device
015d2578a7280b02 device
I want to perform some operations on every device, like
adb -s $device install foo.apk
But I don't know how to let variable device iterate all the devices obtained by adb devices.
One way to do it in bash. Read the output of your command and iterate it on the second column using a while loop.
while read sn device; do
adb -s "$sn" install foo.apk
done < <(adb devices | sed '1d')
Main trick is to separate serial of device from other output. You need to cut off header and second column. Something like this would work:
for DEVICE in `adb devices | grep -v "List" | awk '{print $1}'`
do
adb -s $DEVICE install foo.apk
done
You could use xargs and awk:
adb devices | awk 'NR>1{print $1}' | xargs -n1 -I% adb -s % install foo.apk
Demo:
I put your input into a file and using echo to check the ouput produces:
$ awk 'NR>1{print $1}' file | xargs -n1 -I% echo adb -s % install foo.apk
adb -s 015d4a826e0ffb0f install foo.apk
adb -s 015d4a826e43fb16 install foo.apk
adb -s 015d41d830240b11 install foo.apk
adb -s 015d2578a7280b02 install foo.apk