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
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.