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.
Related
I'm trying to do a pipe operation on my Android device through adb (this is for an automated script).
The operation is to fetch the most recently modified file in a particular directory and then delete it.
Let us say this file is file.txt and it is in /sdcard/Android/data/my.app.package on the Android device.
When I try to do adb shell ls -t /sdcard/Android/data/my.app.package | head -1 | xargs rm -f it throws the error:
rm: file.txt: No such file or directory
This is because it expects the full path.
So then I tried ls -t /sdcard/Android/data/my.app.package | head -1 | xargs ls -d | xargs rm -f but it complains with the same error.
Perhaps I need to pass in the $PWD along with the file name to xargs. How can I do that, or is there a better way to do this?
Edit: I have now tried ls -t sdcard/Android/data/my.app.package | head -1 | xargs -I '{}' ls sdcard/Android/data/my.app.package/'{}' and while a similar command works correctly on the Linux system as expected, it does weird stuff on the Android device. Possibly some missing implementation of xargs on Android stack.
A command like
adb shell foo | bar
runs foo in adb shell, and has your local shell run bar and receive its input from adb shell. If bar should run in adb shell too, you want to pass the entire pipeline to adb shell:
adb shell 'foo | bar'
This part of your question is basically a duplicate of
How to type adb shell commands in one line?
Separately, your ls -t command is flawed in several ways. Generally, don't use ls in scripts; but the trivial fix is to run the command in that directory directly. Then you don't need to add the path back on:
adb shell 'cd /sdcard/Android/data/my.app.package &&
ls -t | head -1 | xargs rm -f'
This still suffers from the various vagaries of parsing ls output; probably a better solution is to use find instead. If you have the facilities of GNU find and related utilities available on the remote device, try
adb shell 'find /sdcard/Android/data/my.app.package -printf "%T# %p\\0" |
sort -r -z -n | head -z -n 1 | sed "s/^[^ ]* //" | xargs -0 rm'
(Adapted from https://stackoverflow.com/a/299911/874188 with quoting fixes to allow the overall command to be run inside single quotes.)
If adb shell does not provide access to GNU userspace tools, perhaps just make sure you have very detailed control over what files can land in your directory so that you can reasonably reliably parse the output from ls; or if you can't guarantee that, try hacking something in e.g. Perl.
It is unfortunate that there is no simple standard way to get the oldest or newest n files from a directory in a robust, machine-readable form.
It would be nice if there was a more succinct way than these arguably complex and slightly advanced tricks with find.
This is the solution that works:
adb shell ls /sdcard/Android/data/my.app.package/`adb shell ls -t /sdcard/Android/data/my.app.package | head -1` | adb shell xargs rm -f
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 ./
I am using the following command to copy the most recently added file from a connected device into the directory that I want:
adb pull sdcard/Robotium-Screenshots/filename.jpg D:\jenkins\jobs\
But it can copy only the file that I specify.
How can I copy the newest file from sdcard/Robotium-Screenshots/ to D:\jenkins\jobs\ without specifying it by name?
Here is a one-liner which would pull the last modified file from a specified folder:
adb exec-out "cd /sdcard/Robotium-Screenshots; toybox ls -1t *.jpg 2>/dev/null | head -n1 | xargs cat" > D:\jenkins\jobs\latest.jpg
Known limitations:
It relies on ls command to do the sorting based on modification time. Historically Android devices had multiple sources for the coreutils, namely toolbox and toybox multi-binaries. The toolbox version did not support timestamp based sorting. That basically means that this won't work on anything older than Android 6.0.
It uses adb exec-out command to make sure that binary output does not get mangled by the tty. Make sure to update your platform-tools to the latest version.
If you use a bash-like shell, you can do:
adb pull /sdcard/Robotium-Screenshots/`adb shell ls -t /sdcard/Robotium-Screenshots/ | head -1` ~/Downloads
You can get a bash-like shell through cygwin, msys, git for windows (based on msys). If you are on mac or linux, you already have a bash-like shell.
One way to do this would be to grab the file name using the following command:
adb shell ls -lt /sdcard/Robotium-Screenshots | head -n2 | tail -n+2 | awk '{print $8}'
I am working on android shell. which is actually linux shell only but it is not supporting lots of linux standard command.
My requirement is to list all the directory present in particular directory recursively without using find, busybox, egrep. Because these things are not supported in android.
I tried
ls -R | grep ./
which is giving me output but adding : at the end.
Is there any other way to list all the directory recursively.
You can do something like this
ls -R | grep ./ | cut -f1 -d:
You can try print command to print directories
print */*
Perhaps find does exist as a busybox builtin but it doesn't exist as a link. Try:
busybox find dir -type d
Update: Best solution is to install busybox if you don't have it yet. Of course if you don't know yet how to do it make sure you read guides or tutorials as to not break your android system. Hint: I installed mine as /system/xbin/busybox.
This lists only the directories
ls -FR / | grep /$
This lists everything about everything:
ls -FlatrR /
The "-F" flag adds a trailing flag to the files it lists, with "/" being the flag it adds on the end of directory entries.
To list directories on the device we can us
adb shell ls -l /
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