0

Pls I really need help on how to parse result returned by the " ls -l " Command I really need this to get list and details of private directories ( such as "/data/data" dir) in my file manager app after the user has been granted su access

Below is an example of result expected to be returned after executing the command getRunTime().exec("ls -l /data/data");

> u0_a192@Infinix_X507:/ $ ls -l /storage/sdcard0
drwxrwx--- root     sdcard_r          2015-08-14 22:23 2go
drwxrwx--- root     sdcard_r          2015-08-15 11:26 360
drwxrwx--x root     sdcard_r          2015-08-13 10:25 Android
drwxrwx--- root     sdcard_r          2015-08-29 21:09 AppProjects
drwxrwx--- root     sdcard_r          2014-01-01 00:00 Assistant
drwxrwx--- root     sdcard_r          2015-08-18 10:44 Camera360
drwxrwx--- root     sdcard_r          2015-08-11 21:29 DCIM
-rw-rw---- root     sdcard_r      808 2015-08-19 05:36 Document.txt
drwxrwx--- root     sdcard_r          2015-08-30 07:19 Download
drwxrwx--- root     sdcard_r          2015-08-17 19:25 FM Recording
drwxrwx--- root     sdcard_r          2015-08-24 17:18 Flash Share
drwxrwx--- root     sdcard_r          2015-08-24 10:03 KingMaster
drwxrwx--- root     sdcard_r          2015-08-14 04:39 Kingroot
drwxrwx--- root     sdcard_r          2015-08-21 06:49 ManifestViewer
drwxrwx--- root     sdcard_r          2015-08-30 19:25 Movies
drwxrwx--- root     sdcard_r          2015-08-30 19:25 Music
drwxrwx--- root     sdcard_r          2015-08-13 06:55 OGWhatsApp
drwxrwx--- root     sdcard_r          2015-08-23 14:00 OGWhatsApp Old
drwxrwx--- root     sdcard_r          2015-08-11 17:00 Pictures
drwxrwx--- root     sdcard_r          2015-08-23 10:59 Rec
drwxrwx--- root     sdcard_r          2014-01-01 00:00 Ringtones
drwxrwx--- root     sdcard_r          2015-08-24 17:18 ShareSDK
drwxrwx--- root     sdcard_r          2015-08-21 14:41 Simple Android Server
drwxrwx--- root     sdcard_r          2015-08-23 11:19 VidMate
drwxrwx--- root     sdcard_r          2015-08-14 04:00 WhatsApp
drwxrwx--- root     sdcard_r          2015-08-30 14:55 Wps
drwxrwx--- root     sdcard_r          2015-08-30 17:19 bluetooth
drwxrwx--- root     sdcard_r          2015-08-24 15:11 com.facebook.orca
-rw-rw---- root     sdcard_r  2999645 2015-08-30 14:55 demo.mp4
drwxrwx--- root     sdcard_r          2015-08-29 17:11 dianxin
drwxrwx--- root     sdcard_r          2015-08-29 21:29 documents
-rw-rw---- root     sdcard_r      112 2015-08-15 07:41 e_config
drwxrwx--- root     sdcard_r          2015-08-18 07:00 iQuran
drwxrwx--- root     sdcard_r          2015-08-23 14:00 icloudzone
drwxrwx--- root     sdcard_r          2015-08-14 11:53 jamb_cbt_science
-rw-rw---- root     sdcard_r      112 2015-08-13 20:24 kr-stock-conf
drwxrwx--- root     sdcard_r          2015-08-11 17:45 media
drwxrwx--- root     sdcard_r          2015-08-23 14:00 mtklog
drwxrwx--- root     sdcard_r          2014-01-01 01:00 obb
-rw-rw---- root     sdcard_r  1132508 2015-08-14 11:54 pq_commercial.pdf
-rw-rw---- root     sdcard_r        6 2015-08-23 13:44 qs.pid
drwxrwx--- root     sdcard_r          2015-08-16 07:33 romtoolbox
drwxrwx--- root     sdcard_r          2015-08-24 10:03 tbslog
drwxrwx--- root     sdcard_r          2015-08-17 04:25 tencent
-rw-rw---- root     sdcard_r 69565980 2015-08-23 17:01 update.zip
drwxrwx--- root     sdcard_r          2015-08-14 22:26 viber
-rw-rw---- root     sdcard_r  8514230 2015-08-30 10:43 vlc.apk
u0_a192@Infinix_X507:/ $

I have already seen a similar solution, but was in php using the ftp_rawlist() function, but I want a Java / Android solution please....

I know this would require RegEx or Pattern...etc.. Your help will be highly appreciated

Gergely M
  • 583
  • 4
  • 11
Mkay4real
  • 23
  • 5

3 Answers3

0

You're making this harder than it needs to be; Android has a File class with this very functionality built in:

File f = new File(path);        
File files[] = f.listFiles();

That will give you an array of files and folders in that path. Documentation on File can be found here: http://www.developer.android.com/reference/java/io/File.html

Sector95
  • 641
  • 6
  • 12
  • Thanks, I use that for normal file list but the File class cannot list private directories, especially with su command – Mkay4real Sep 03 '15 at 19:03
  • If the application is running as root (which it would have to be to perform `ls` as root anyway), it shouldn't need the `su` command. What private directories specifically are you speaking about? – Sector95 Sep 03 '15 at 19:17
  • App itslef cannot be run under root account, even when granted superuser permissions - it executs scripts under root account using 'su' shell command. – pelya Sep 03 '15 at 20:03
  • @Sector95, the device does not need to be root to run `ls`. Also, the developer indeed needs to call `su` and keep the shell open to run binaries as root. – MeetTitan Sep 03 '15 at 20:04
0

What data are you trying to parse for? If you're looking for a specific string, you could use grep. (At least, on Linux you can use grep, I'm assuming it's included in Android as well, but I haven't tried it out myself.)

ls -l | grep targetString

camdroid
  • 524
  • 7
  • 24
  • Just adding as I do the same thing with Windows using 'cygwin', a wonderful tool for simulating the linux commands on Windows. – darthvading Sep 04 '15 at 08:00
0

First, you read the output line by line.

Then you split each line by whitespaces:

if (str[0] == 'd') // Directory
    String[] parsed = str.split("\\s+", 6);
else
    String[] parsed = str.split("\\s+", 7);

Then you have a nice string array, where parsed[6] is the file name, or parsed[5] if it's a directory, because ls -l won't print size for directories. It won't mess filenames with spaces inside, as an additional bonus.

Community
  • 1
  • 1
pelya
  • 4,326
  • 2
  • 24
  • 23
  • Thanks very much, your answer really worked, but doesn't for files that neither starts with 'd' or '-', I encountered some problem with those that starts with 'l' – Mkay4real Sep 14 '15 at 08:34