How to pull photos with ADB from local storage - android device - android

i have some problems with my Asus Zenfone 2 device (no root).
In particular, some days ago, while i was travelling abroad, my device decided to no more turn on, staying permanently in the loading ASUS screen.
Before doing the hard reset, i want to try to save at least all my photos located in the internal storage (N.B: i do not have an external SDCARD).
i correctly set up the ADB software to recognize my device but i am not able to find the photo directory.
Till now i have only pull successfully 2 directory: "sys" and "cache", using this command:
adb pull / C:\Myfile
It seems to transfer only some file system.
Doas anyone know how to pull photos or other file?

Use adb pull sdcard/DCIM/Camera C:\MyFile
If it didn't work use:
adb shell
echo $EXTERNAL_STORAGE
It will print path to your simulated external storage. Then:
cd $EXTERNAL_STORAGE/DCIM
ls
It will print folder which will contain camera app folders. Lastly, use:
adb pull HERE_PUT_PATH_TO_EXTERNAL/DCIM/SELECTED_CAMERA_APP_FOLDER C:\MyFiles
Note: To leave adb shell type exit

I did 3 things to pull all files of the same type, in this case *.mp4 (all videos)
1. First copy the output of: {all videos listed} with xargs to a file.txt
adb shell ls /storage/7C17-B4FD/DCIM/Camera/*.mp4 | xargs -t -I % echo % >> file.txt
adb shell ls /storage/7C17-B4FD/DCIM/Camera/*.mp4 returns all videos listed in device.
The argument -t show the output from XARGS, and -I lets you use a variable, in this case represented with % then you write the command you want to do. In this case, echo % each line and add it to the file.txt
2. You need to clean the file.txt from spaces and the '\r' you do it with the command tr
tr -d '\r' < file.txt > text-clean.txt
the input file is < file.txt and the new output file is > text-clean.txt
3.Now you can cat the new clean file text-clean.txt and use that input with XARGS command to pass the command adb pull for each file.
cat text-clean.txt | xargs -t -I % adb pull % ./
we cat the content of text-clean.txt and send the output to XARGS, then we see what is the result of command with -t and -I to add variables. adb pull to request file from path which is represented with % and ./ means copy to this directory or currrent directory of where you are.
if you just need to copy one file. just need to find the file with adb shell ls /path/to/file and the just copy to your location with adb pull. example
adb pull /storage/7C717-B4FD/DCIM/Camera/video1.mp4 ./

Related

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.

More files on Android than actual directory after pushing using adb

I pushed a bunch of files on Android sdcard using adb
adb push local_path/directory/ device_path/directory/
But, once it has pushed all the files, when I counted the number of files on both the computer and the android device, I found more files on Android than actual files on the machine.
machinex:My_Tools user$ adb shell ls -R /device_path/directory/ | wc -l
36624
machinex:My_Tools user$ ls -R /local_path/directory/ | wc -l
36617
I tried deleting the directory on Android device, and pushing all the files again, but with same result.
Does anyone have any idea, what may be causing the difference. Are there some hidden files on android, that are generated at their own ?
You can try ls -a command in the so named directory of your board. This will list both hidden and non hidden files in that respective folder.
Steps:
1) adb shell
2) once you are in the board's shell,
cd /path/to/yourdirectory/
3) ls -a
hope this helps you :)

Retrieve database or any other file from the Internal Storage using run-as

On a non-rooted android device, I can navigate to the data folder containing the database using the run-as command with my package name. Most files types I am content with just viewing, but with the database I would like to pull if from the android device.
Is there a download copy or move command from this part of adb shell? I would like to download the database file and view its content using a database browser.
One answer here involves turning entire application package into a compressed archive, but there is no further answer on how to extract that archive once this is done and moved to the machine, leaving me very sidetracked when there might be a more direct solution to begin with
By design user build of Android (that's what you have on your phone until you unlock the bootloader and flash the phone with userdebug or eng software) restricts access to the Internal Storage - every app can only access its own files. Fortunately for software developers not willing to root their phones Google provides a way to access the Internal Storage of debuggable versions of their packages using run-as command.
To download the /data/data/debuggable.app.package.name/databases/file from an Android 5.1+ device run the following command:
adb exec-out run-as debuggable.app.package.name cat databases/file > file
To download multiple files in a folder under the /data/data/debuggable.app.package.name/ at once - use tar:
adb exec-out run-as debuggable.app.package.name tar c databases/ > databases.tar
adb exec-out run-as debuggable.app.package.name tar c shared_prefs/ > shared_prefs.tar
The accepted answer doesn't work anymore for me (blocked by Android?)
So instead I did this:
> adb shell
shell $ run-as com.example.package
shell $ chmod 666 databases/file
shell $ exit ## exit out of 'run-as'
shell $ cp /data/data/package.name/databases/file /sdcard/
shell $ run-as com.example.package
shell $ chmod 600 databases/file
> adb pull /sdcard/file .
If anyone looking for pulling database from debug application may use the procedure below:
search and open device file explorer
Select your handset and then browse to data/data directory
Now find your application package and go to databases folder. You can see the databases there and upon right click, you will get option
to save this in your drive.
I've published a simple shell script for dumping databases:
https://github.com/Pixplicity/humpty-dumpty-android
It performs two distinct methods described here:
First, it tries to make the file accessible for other users, and attempting to pull it from the device.
If that fails, it streams the contents of the file over the terminal to the local machine. It performs an additional trick to remove \r characters that some devices output to the shell.
From here you can use a variety of CLI or GUI SQLite applications, such as sqlite3 or sqlitebrowser, to browse the contents of the database.
I couldn't get anything else to work for me but this:
adb shell
run-as package.name
cat /databases/databaseFileName.db > /sdcard/copiedDatabaseFileName.db
exit
exit
adb pull /sdcard/copiedDatabaseFileName.db /file/location/on/computer/
The first exit is to exit out of the run-as, the second exit is to exit out of adb shell to do the pull.
For app's debug version, it's very convenient to use command adb exec-out run-as xxx.yyy.zzz cat somefile > somefile to extract a single file. But you have to do multiple times for multiple files. Here is a simple script I use to extract the directory.
#!/bin/bash
P=
F=
D=
function usage()
{
echo "$(basename $0) [-f file] [-d directory] -p package"
exit 1
}
while getopts ":p:f:d:" opt
do
case $opt in
p)
P=$OPTARG
echo package is $OPTARG
;;
f)
F=$OPTARG
echo file is $OPTARG
;;
d)
D=$OPTARG
echo directory is $OPTARG
;;
\?)
echo Unknown option -$OPTARG
usage
;;
\:)
echo Required argument not found -$OPTARG
usage
;;
esac
done
[ x$P == x ] && {
echo "package can not be empty"
usage
exit 1
}
[[ x$F == x && x$D == x ]] && {
echo "file or directory can not be empty"
usage
exit 1
}
function file_type()
{
# use printf to avoid carriage return
__t=$(adb shell run-as $P "sh -c \"[ -f $1 ] && printf f || printf d\"")
echo $__t
}
function list_and_pull()
{
t=$(file_type $1)
if [ $t == d ]; then
for f in $(adb shell run-as $P ls $1)
do
# the carriage return output from adb shell should
# be removed
mkdir -p $(echo -e $1 |sed $'s/\r//')
list_and_pull $(echo -e $1/$f |sed $'s/\r//')
done
else
echo pull file $1
[ ! -e $(dirname $1) ] && mkdir -p $(dirname $1)
$(adb exec-out run-as $P cat $1 > $1)
fi
}
[ ! -z $D ] && list_and_pull $D
[ ! -z $F ] && list_and_pull $F
Hope it would be helpful. This script is also available at gist.
Typical usage is
$ ./exec_out.sh -p com.example.myapplication -d databases
then it will extract all files under your apps databases directory, which is /data/data/com.example.myapplication/databases, into current directory.
Much much simpler approach to download the file onto your local computer:
In your PC shell run:
adb -d shell 'run-as <package_name> cat /data/data/<package_name>/databases/<db_name>' > <local_file_name>
#!/bin/bash
#export for adb
export PATH=$PATH:/Users/userMe/Library/Android/sdk/platform-tools
export PATH=$PATH:/Users/userMe/Library/Android/sdk/tools
adb -d shell 'run-as com.android.app cp /data/data/com.android.app/files/db.realm /sdcard'
adb pull sdcard/db.realm /Users/userMe/Desktop/db
You can use this script for get Realm database.
The database file is emtpy when using adb run-as. This can be resolved by calling close() on the RoomDatabase instance. Call close() to let SQLite write its journal to disk.
I've created this button that closes the database connection on request: via GIPHY
Here is how to call close on the RoomDatabase instance.
Steps to pull app db(installed in debug mode) from device
Close DB connection if opened
Open cmd (command prompt) (Change dir to your adb path)
cd C:\Program Files (x86)\Android\android-sdk\platform-tools
(list the app files)
adb -d shell "run-as com.xyz.name ls
/data/data/com.xyz.name/files/"
(copy required file to sdcard)
adb -d shell "run-as com.xyz.name cp
/data/data/com.xyz.name/files/abc.db /sdcard/abc.db"
(copy from sdcard to machine adb folder)
adb pull /sdcard/abc.db
Open DB connection
Destination file path in my case C:\Users{userName}\AppData\Local\VirtualStore\Program Files (x86)\Android\android-sdk\platform-tools
Or Device storage
If someone is looking for another answer that can be used to retrieve Database as well as Shared Preferences then follow this step:
In your build.gradle file of your app add line
debugCompile 'com.amitshekhar.android:debug-db:1.0.0'
now when you run your app in non-release mode then your app will automatically open 8080 port from your device IP address make sure your device is connected via wifi and your laptop is sharing the same network. Now simply visit the url
http://your_mobile_device_ip:8080/
to watch all data of database along with shared preferences.
Here's a solution that works on a device running Android 5.1. The following example is for Windows.
You need sed (or sed.exe on windows, e.g. from cygwin.) ( On Unix, it'll just be there ;) ). To remove bad '\r' characters, at least on windows.
Now just run the following command:
adb exec-out "run-as com.yourcompany.yourapp /data/data/com.yourcompany.yourapp/databases/YourDatabaseName" | c:\cygwin\bin\sed.exe 's/\x0D\x0A/\x0A/'>YourDatabaseName.db
The sed command strips out trailing /r characters.
Of course you should replace "com.yourcompany.yourapp" with the package name of the app and "YourDatabaseName" with the name of the database in the app.

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

How I can simulate "tail" command for file on the Android file system?

I have file on SD-CARD and my app using it as log file.
Is it possible through the adb to watch file with all changes in real time?
Like with tail -f /sdcard/myfile.log command.
This seems to work great for me:
adb shell "while true; do cat; sleep 1; done < /sdcard/myfile.log"
You can install busybox and then:
adb shell
tail -f /path/of/your/file
But remember that you should have root access to install busybox. If you are using the emulator check this one:
How to get root access on Android emulator?
You can do this with logcat. You can add a view that will only show log entries from your app and it will be continuously updated.
There is a great app for this: Terminal IDE. It contains many linux commands, and it does not need root access. You can install it from GooglePlay. Is is free of charge (and open source, GPLv2).
One of its best features is that it can be used through telnet. Start it on your phone, and type telnetd command. It will start a telnet daemon, which listens on port 8080 by default.
After that you can connect it from your PC, with the following command: (use cygwin on windows)
telnet 192.168.1.8 8080
You should use your phone's IP address instead of the above one. After a successful connection you will have an arbitrary sized terminal on your PC, which is capable to run tail -f command on your phone. And many others, such as bash and all of its builtin commands.
Building upon Jesse's answer, to do similar with a file within an app's private storage area:
adb shell "while true; do run-as com.yourdomain.yourapp cat /data/data/com.yourdomain.yourapp/app_flutter/yourfile.txt; sleep 5; done" | egrep -o 'sometext.{0,50}'
(This example is for a flutter app on Android, but is similar minus the app_flutter directory.)
do run-as changes the user under which the command is run to the application. By default adb shell user shouldn't have access to any files under an application's private storage area.
| egrep -o 'sometext.{0,50}' the cat command sends the file contents to STDOUT. egrep is taking the contents & searching for -o (only) sometext + 50 characters" using regex (hence egrep instead of grep).
Last Line Only
Replace cat with tail -n 1.
Add --line-buffered to egrep
adb shell "while true; do run-as com.yourdomain.yourapp tail -n 1 /data/data/com.yourdomain.yourapp/app_flutter/yourfile.txt; sleep 5; done" | egrep --line-buffered -o 'sometext.{0,50}'

Categories

Resources