Copy apk files in a bash script - android

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

Related

How to push a file from computer to /data/.../databases/ using adb command?

Below command is to pull a file:
adb -d shell "run-as com.myapp cat /data/data/com.myapp/databases/file.db" > file.db
But how to push it back like Android Studio does via Device File Explorer?
There is no simple command for uploading the file. What Android Studio does when uploading a file using Device File Explorer is this:
Upload the file via adb push to /data/local/tmp/<random file name>
Execute adb shell run-as com.myapp sh -c 'cp /data/local/tmp/<random file name> /data/data/com.myapp/<path>/<final file-name>'
Delete the temp file via adb shell rm /data/local/tmp/<random file name>
Get the updated view for Device File Explorer using adb shell run-as com.myapp sh -c 'ls -al /data/data/com.myapp/<path>/'
I discovered this by capturing the adb traffic on TCP port 5027 using Wireshark. An interesting detail is that each command executed using adb shell command uses the form <command-to-be executed in adb shell> || echo ERR-ERR-ERR-ERR
From Robert's answer now I can do like this:
function dbpull() {
adb shell run-as "com.$1.debug" cat "/data/data/com.$1.debug/databases/$2.db" > "/Users/username/Desktop/$2.db"
}
function dbpush() {
adb push "/Users/username/Desktop/$1.db" "/sdcard/db/tmp/"
}
function dbpush2() {
adb shell run-as "com.$1.debug" cp "/sdcard/db/tmp/$2.db" "/data/data/com.$1.debug/databases/$2.db"
}
function dbcheck() {
adb shell run-as "com.$1.debug" ls -al "/data/data/com.$1.debug/databases/"
}
Just write above code lines in your .bash_profile and then call it in terminal.
dbpull myapp mydata
At this moment I prefer to use Visual Studio Code than Android Studio to develop my Android apps. So, I need to know more about commands in terminal, e.g. adb, gradle, etc.
I hope this would be useful for everyone.
Use this command
adb push <file_path> <android_device_path>
adb pull <android_device_path>

How do I find the number of files in a directory?

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

adb pull multiple files with the same extension

How can we pull multiple files with the same extension by using "abd" command?
More details, I know that we can use command
adb pull sdcard/folder target-folder
to get all file of the folder.
I use this command to filter file in the adb shell.
ls -lR sdcard/folder | grep "ext"
But I want to filter some files with the same extension and pull them.
So now, how can we combine two commands?
adb shell ls sdcard/folder/*.ext | tr '\r' ' ' | xargs -n1 adb pull
See adb pull multiple files
For Windows, even with gitbash installed (so find and xargs available), you have to use CMD for to iterate the file list.
for /f "delims=" %G in ('adb shell find sdcard/DCIM/Camera/20221111*') do adb pull -a "%G"
This will download all photos and videos matching the criteria(in my case, taken on the day 2022 Nov 11st).
Find more info googling for /f and linux find.

adb install apk on multiple connected devices using WINDOWS

I am looking for a solution to install all the apks in a folder on all the connected devices using Windows.
I have looked everywhere and I can only find Bash solution.
here is the best bash I came across:
via this website
http://engineering.meetme.com/2014/07/quick-tip-how-to-install-or-uninstall-apks-on-multiple-android-devices-with-just-one-command/
#!/bin/bash
# Script adb+
# Usage
# You can run any command adb provides on all your currently connected devices
# ./adb+ <command> is the equivalent of ./adb -s <serial number> <command>
# Examples
# ./adb+ version
# ./adb+ install apidemo.apk
# ./adb+ uninstall com.example.android.apis
adb devices | while read line
do
  if [ ! "$line" = "" ] && [ `echo $line | awk '{print $2}'` = "device" ]
  then
      device=`echo $line | awk '{print $1}'`
      echo "adb -s $device $# ..."
      adb -s $device $#
  fi
done
Now I wonder how to convert this into bat file ?
Does anyone know how to do such thing please......
I can do this manually as:
I run adb devices, then copy all the ids and replace in code below
ECHO Running Bat script
#ECHO OFF
:: comment
adb -s <DEVICE_ID> install MyApk.apk
adb -s <DEVICE_ID> install MyApk2.apk
adb -s <DEVICE_ID> install MyApk3.apk
But I need to automate this process.
I need to learn how to get all the connected devices_id as the bash code above. then get all the .apk files in the folder (where this bat file will be).
Then run the install...
Really Appreciate if anyone knows how to help. Thanks.
Finally, managed to implement it correctly (install all apks from current folder to all connected devices):
echo off
setlocal EnableDelayedExpansion
for /f "tokens=*" %%f in ('C:\android\sdk\platform-tools\adb.exe devices') do (
for /r %%p in (*.apk) do (
set devicestr=%%f
set apk=%%p
if "!devicestr!"=="!devicestr:List=!" (
for /f "tokens=1" %%d in ("!devicestr!") do (
set deviceid=%%d
echo !deviceid!
echo !apk!
C:\android\sdk\platform-tools\adb.exe -s !deviceid! install !apk!
)
)
)
)
this line removes "List of connected devices":
if "!devicestr!"=="!devicestr:List=!" (

How to list all the files in android phone by using adb shell?

I just try to write a bash shell for my Android Phone.
When I want list all the files in my Android Phone. I found that the Android shell terminal doesn't support find command.
So I just want to know which is the best way to travel the sdcard files?
I might be wrong but "find -name __" works fine for me. (Maybe it's just my phone.)
If you just want to list all files, you can try
adb shell ls -R /
You probably need the root permission though.
Edit:
As other answers suggest, use ls with grep like this:
adb shell ls -Ral yourDirectory | grep -i yourString
eg.
adb shell ls -Ral / | grep -i myfile
-i is for ignore-case. and / is the root directory.
Open cmd type adb shell then press enter.
Type ls to view files list.
just to add the full command:
adb shell ls -R | grep filename
this is actually a pretty fast lookup on Android
This command will show also if the file is hidden
adb shell ls -laR | grep filename
Some Android phones contain Busybox. Was hard to find.
To see if busybox was around:
ls -lR / | grep busybox
If you know it's around. You need some read/write space. Try you flash drive, /sdcard
cd /sdcard
ls -lR / >lsoutput.txt
upload to your computer. Upload the file. Get some text editor. Search for busybox. Will see what directory the file was found in.
busybox find /sdcard -iname 'python*'
to make busybox easier to access, you could:
cd /sdcard
ln -s /where/ever/busybox/is busybox
/sdcard/busybox find /sdcard -iname 'python*'
Or any other place you want.
R

Categories

Resources