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
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
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 /'"
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
I tried the following script written by me.
#!/bin/bash
adb -s 015d2578a7280412 shell ls /data/app > apps.txt
while read line
do
apk=/data/app/$line
adb -s 015d2578a7280412 pull $apk apk-nexus7-default
done < apps.txt
I got errors like:
' does not existdata/app/com.StudioOnMars.CSPortable-1.apk
' does not existdata/app/com.adobe.reader-1.apk
...
When I tried
adb -s 015d2578a7280412 pull /data/app/com.adobe.reader-1.apk apk-nexus7-default
It worked.
Any problem with the piece of scripts?
It's a known issue with adb - that even in linux it is using MSDOS style newline characters - CR+LF ('\r\n') instead of just LF ('\n').
The easiest way to mitigate that is to remove '\r' from the adb output
adb -s 015d2578a7280412 shell ls /data/app | tr -d '\r'> apps.txt
Just do:
$ adb -s 015d2578a7280412 pull /data/app/
pull: building file list...
pull: /data/app/some.apk -> ./some.apk
...