Home > Blockchain >  Fetching most recent file and deleting it via adb
Fetching most recent file and deleting it via adb

Time:09-30

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.

CodePudding user response:

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.

CodePudding user response:

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
  • Related