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}'
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
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.
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 :)
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 want to list all files and folders of music directory of android mobile. Where I am using
ls -l -R /sdcard/music
It gives all files and folders but not in a proper way.
Any one tell me adb shell command that displays files and folders hierarchy of a directory like "tree" linux command.
I don't know if you still looking for this, but I believe this command can help you (works on adb as well):
find . -print | sort | sed 's;[^/]*/;|---;g;s;---|; |;g'
It prints a directory tree (both folders and files) in a more readable way than
ls -l -R
If you want just a folder tree, you can use this one:
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
You need to install busybox for android, it will add many well known linux tools to your phone.
You need a rooted phone for installing busybox.
Check it out here: https://play.google.com/store/apps/details?id=stericson.busybox&hl=fr