3

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?

Alex P.
  • 30,437
  • 17
  • 118
  • 169
vTad
  • 309
  • 2
  • 13
  • I think you might need to write a script to identify the first file, and then execute the adb pull – krikara Jul 31 '15 at 09:55

3 Answers3

2

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.

Alex P.
  • 30,437
  • 17
  • 118
  • 169
1

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.

qwertzguy
  • 15,699
  • 9
  • 63
  • 66
  • What do the code quoted adb shell mean inside adb pull and why does this return the full path? – nj2237 Sep 30 '21 at 11:16
  • 1
    The back tick is for substituting the result of the command, so it will substitute with the filename of the last modified file. It only returns the filename which is why I put the full path of the folder twice. – qwertzguy Sep 30 '21 at 15:26
0

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}'
  • 1
    vTad is using windows and in your command `head`, `tail` and `awk` are being run on the host side. by using `-1` instead of the `-l` option you could have eliminated both `tail` and `awk`. Also when you need to limit your input to a specific line and you are already using `awk` - there is no need to use the `head` and `tail` combination - just use `awk`'s built-in `NR` variable – Alex P. Dec 03 '16 at 16:33