0

Is it possible to output the absolute file path when executing ls -l on Android (e.g. via adb shell or in a terminal app)?

Here is the output I get from ls -l /etc/ (I have root access):

E:\Dropbox\Droid Explorer\src\main\resources\com\droid\explorer\adb>adb shell su root ls -l /etc/
-rw-r--r-- root     root        16656 2009-01-01 03:00 CHANGELOG-CM.txt
-rw-r--r-- root     root         9944 2009-01-01 03:00 CHANGES.txt
-rw-r--r-- root     root       154482 2009-01-01 03:00 NOTICE.html.gz
drwxr-xr-x root     root              2009-01-01 03:00 acdbdata
-rw-r--r-- root     root       598006 2009-01-01 03:00 apns-conf.xml
-rw-r--r-- root     root         5491 2009-01-01 03:00 audio_effects.conf
-rw-r--r-- root     root         6198 2009-01-01 03:00 audio_policy.conf
drwxr-xr-x root     root              2009-01-01 03:00 bash
drwxr-xr-x root     root              2009-01-01 03:00 bluetooth
-rw-r--r-- root     root       101774 2009-01-01 03:00 build-manifest.xml
-rw-r--r-- root     root         1045 2009-01-01 03:00 clatd.conf
drwxr-xr-x root     root              2009-01-01 03:00 dhcpcd
-rw-r--r-- root     root         1362 2009-01-01 03:00 ethertypes
-rw-r--r-- root     root        18300 2009-01-01 03:00 event-log-tags
-rw-r--r-- root     root        14309 2009-01-01 03:00 fallback_fonts.xml

What I would like to get is the absolute path, ex:

E:\Dropbox\Droid Explorer\src\main\resources\com\droid\explorer\adb>adb shell su root ls -l /etc/
-rw-r--r-- root     root        16656 2009-01-01 03:00 /etc/CHANGELOG-CM.txt
-rw-r--r-- root     root         9944 2009-01-01 03:00 /etc/CHANGES.txt
-rw-r--r-- root     root       154482 2009-01-01 03:00 /etc/NOTICE.html.gz
drwxr-xr-x root     root              2009-01-01 03:00 /etc/acdbdata
-rw-r--r-- root     root       598006 2009-01-01 03:00 /etc/apns-conf.xml
-rw-r--r-- root     root         5491 2009-01-01 03:00 /etc/audio_effects.conf
-rw-r--r-- root     root         6198 2009-01-01 03:00 /etc/audio_policy.conf
drwxr-xr-x root     root              2009-01-01 03:00 /etc/bash
drwxr-xr-x root     root              2009-01-01 03:00 /etc/bluetooth
-rw-r--r-- root     root       101774 2009-01-01 03:00 /etc/build-manifest.xml
-rw-r--r-- root     root         1045 2009-01-01 03:00 /etc/clatd.conf
drwxr-xr-x root     root              2009-01-01 03:00 /etc/dhcpcd
-rw-r--r-- root     root         1362 2009-01-01 03:00 /etc/ethertypes
-rw-r--r-- root     root        18300 2009-01-01 03:00 /etc/event-log-tags
-rw-r--r-- root     root        14309 2009-01-01 03:00 /etc/fallback_fonts.xml
0

3 Answers 3

1

ls by default list everything supplied via argument. From man ls:

-d, --directory list directories themselves, not their contents

-l use a long listing format

So you can simly supply everything inside your targer directory, and give the option -d.

[adb shell] [su -c] ls -dl /etc/*
^ optional, depending on your shell environment

Which gives

rwxr--r-- 1 root root 1024 Jun 4 22:32 /etc/hosts
...
(and a lone list)
0

Since the Android is rooted, you can use any of the following commands:

# Replace DIR with the directory's path; prefix adb shell wherever appropriate.

su -c 'busybox ls -ld DIR*'  
su -c 'toybox ls -ld DIR*'
su -c 'ls -d DIR*'  # this does not work with long list format available through -l

(Courtesy of the answer here from An̲̳̳drew for listing the argument -d).

Android Marshmallow comes with Toybox. For any other Android version, you would need to setup BusyBox or Toybox.

Example:

IMG

1
  • So I'm guessing the adb shell ls is limited. Dang I was hoping to do this without busybox. Thanks anyways Commented Apr 25, 2016 at 23:22
0

You can also write a recursive list function in sh - this function can be copy-pasted in adb shell, without root permissions:

function rcrls() { ls -d $1/* | while read f; do echo "$f"; if [ -d "$f" ]; then rcrls "$f"; fi; done }

Then, use it as:

shell@myphone:/ $ rcrls /

Unfortunately, it will persist only for the current adb shell session, once you're out of it, you'll have to paste it again; or you can use it like this:

$ adb shell 'function rcrls() { ls -d $1/* | while read f; do echo "$f"; if [ -d "$f" ]; then rcrls "$f"; fi; done } ; rcrls /' | less

Then you can see files listed as:

...
//acct/uid/1000
//acct/uid/1000/cgroup.clone_children
//acct/uid/1000/cgroup.event_control
//acct/uid/1000/cgroup.procs
//acct/uid/1000/cpuacct.stat
//acct/uid/1000/cpuacct.usage
...

To get a listing with -l info, it's a bit tricky, since there's no working sed on adb shell; however, we can use string variable shell expansion and truncating to remove the basename from the listing, and then we can append the full path:

function rcrls() { 
  ls -d $1/* | while read f; do 
    if [ -d "$f" ]; then 
       fl="$(ls -ld $f/)"; 
       echo "${fl%\ *}" "$f"; 
       rcrls "$f"; 
    else 
       fl="$(ls -l $f)"; 
       echo "${fl%\ *}" "$f"; 
    fi; 
  done 
} ; 
rcrls /

Or, as one liner:

adb shell 'function rcrls() { ls -d $1/* | while read f; do if [ -d "$f" ]; then fl="$(ls -ld $f/)"; echo "${fl%\ *}" "$f"; rcrls "$f"; else fl="$(ls -l $f)"; echo "${fl%\ *}" "$f"; fi; done } ; rcrls /' | less

... and then you get a listing as:

drwxr-xr-x root     root              2017-11-15 02:29 //acct
-rw-r--r-- root     root            0 2017-11-15 02:29 //acct/cgroup.clone_children
--w--w--w- root     root            0 2017-11-15 02:29 //acct/cgroup.event_control
-rw-r--r-- root     root            0 2017-11-15 02:29 //acct/cgroup.procs
-r--r--r-- root     root            0 2017-11-15 02:29 //acct/cpuacct.stat
...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .