Similar command to linux tree command in adb shell - android

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

Related

Fetching most recent file and deleting it via adb

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

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 :)

ADB copy the newest file

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}'

List all directory using shell command without using find, busybox command

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 /

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

Categories

Resources