Can't execute "stat" on android 2.3.4 command not found - android

So I am currently connected to my android phone using adb shell. When I do "stat file"
I get this message:
stat: not found
Do I need to install this script or what? Im on a 2.3.4 Gingerbread.

stat does not come with Android/AOSP.
If you run ls /system/bin you will get a list a lot shorter than what you are probably used to being able to do in most *NIX environments:
ATFWD-daemon
abcc
adb
am
app_process
applypatch
atrace
bdAddrLoader
bmgr
bootanimation
bridgemgrd
btnvtool
bu
bugreport
cat
chcon
chmod
chown
clatd
clear
cmp
conn_init
content
cp
dalvikvm
date
dd
debuggerd
dexopt
df
dhcpcd
diag_klog
diag_mdlog
dmesg
dnsmasq
drmserver
ds_fmc_appd
du
dumpstate
dumpsys
e2fsck
efsks
fsck_msdos
getenforce
getevent
getprop
getsebool
grep
gzip
hci_qcomm_init
hd
hostapd
id
ifconfig
iftop
ime
input
insmod
installd
ioctl
ionice
ip
ip6tables
iptables
keystore
kill
ks
linker
ln
load_policy
log
logcat
logwrapper
ls
lsmod
lsof
make_ext4fs
md5
mdnsd
media
mediaserver
mkdir
mksh
mm-qcamera-daemon
monkey
mount
mpdecision
mtpd
mv
nandread
ndc
netcfg
netd
netmgrd
netstat
newfs_msdos
nl_listener
notify
ping
pm
port-bridge
pppd
printenv
ps
qcks
qmuxd
qseecomd
racoon
radish
reboot
renice
requestsync
restorecon
rild
rm
rmdir
rmmod
rmt_storage
route
run-as
runcon
schedtest
schedtop
screencap
screenshot
sdcard
sendevent
sensors.qcom
sensorservice
service
servicemanager
setconsole
setenforce
setprop
setsebool
settings
sh
sleep
smd
start
stop
surfaceflinger
svc
sync
system_server
tc
thermald
toolbox
top
touch
uiautomator
umount
uptime
usbhub
usbhub_init
v4l2-qcamera-app
vdc
vmstat
vold
watchprops
wipe
wm
wpa_supplicant
No stat, as you can see. If you have root privileges, a simple solution is to install something like BusyBox which adds some such bread-and-butter scripted functions as stat to your environment:
[
[[
acpid
addgroup
adduser
adjtimex
ar
arp
arping
ash
awk
basename
beep
blkid
brctl
bunzip2
bzcat
bzip2
cal
cat
catv
chat
chattr
chgrp
chmod
chown
chpasswd
chpst
chroot
chrt
chvt
cksum
clear
cmp
comm
cp
cpio
crond
crontab
cryptpw
cut
date
dc
dd
deallocvt
delgroup
deluser
depmod
devmem
df
dhcprelay
diff
dirname
dmesg
dnsd
dnsdomainname
dos2unix
dpkg
du
dumpkmap
dumpleases
echo
ed
egrep
eject
env
envdir
envuidgid
expand
expr
fakeidentd
false
fbset
fbsplash
fdflush
fdformat
fdisk
fgrep
find
findfs
flash_lock
flash_unlock
fold
free
freeramdisk
fsck
fsck.minix
fsync
ftpd
ftpget
ftpput
fuser
getopt
getty
grep
gunzip
gzip
hd
hdparm
head
hexdump
hostid
hostname
httpd
hush
hwclock
id
ifconfig
ifdown
ifenslave
ifplugd
ifup
inetd
init
inotifyd
insmod
install
ionice
ip
ipaddr
ipcalc
ipcrm
ipcs
iplink
iproute
iprule
iptunnel
kbd_mode
kill
killall
killall5
klogd
last
length
less
linux32
linux64
linuxrc
ln
loadfont
loadkmap
logger
login
logname
logread
losetup
lpd
lpq
lpr
ls
lsattr
lsmod
lzmacat
lzop
lzopcat
makemime
man
md5sum
mdev
mesg
microcom
mkdir
mkdosfs
mkfifo
mkfs.minix
mkfs.vfat
mknod
mkpasswd
mkswap
mktemp
modprobe
more
mount
mountpoint
mt
mv
nameif
nc
netstat
nice
nmeter
nohup
nslookup
od
openvt
passwd
patch
pgrep
pidof
ping
ping6
pipe_progress
pivot_root
pkill
popmaildir
printenv
printf
ps
pscan
pwd
raidautorun
rdate
rdev
readlink
readprofile
realpath
reformime
renice
reset
resize
rm
rmdir
rmmod
route
rpm
rpm2cpio
rtcwake
run-parts
runlevel
runsv
runsvdir
rx
script
scriptreplay
sed
sendmail
seq
setarch
setconsole
setfont
setkeycodes
setlogcons
setsid
setuidgid
sh
sha1sum
sha256sum
sha512sum
showkey
slattach
sleep
softlimit
sort
split
start-stop-daemon
stat
strings
stty
su
sulogin
sum
sv
svlogd
swapoff
swapon
switch_root
sync
sysctl
syslogd
tac
tail
tar
taskset
tcpsvd
tee
telnet
telnetd
test
tftp
tftpd
time
timeout
top
touch
tr
traceroute
true
tty
ttysize
udhcpc
udhcpd
udpsvd
umount
uname
uncompress
unexpand
uniq
unix2dos
unlzma
unlzop
unzip
uptime
usleep
uudecode
uuencode
vconfig
vi
vlock
volname
watch
watchdog
wc
wget
which
who
whoami
xargs
yes
zcat
zcip
Great stuff - you won't need any computer but your phone, anymore!

the stat command is not available on the device. There is a very limited set of commands available. They are listed here:
http://developer.android.com/guide/developing/tools/adb.html
What are you trying to do?

Related

Get application name from its package name via adb [duplicate]

I'm developing an application that uses ADB Shell to interface with android devices, and I need some way of printing out the application name or label of an application, given maybe their package name.
In short, I need a way of getting app names (i.e. "Angry Birds v1.0.0") for user installed applications through adb shell.
Any light on the matter? Any help is appreciated on this.
adb shell pm list packages will give you a list of all installed package names.
You can then use dumpsys | grep -A18 "Package \[my.package\]" to grab the package information such as version identifiers etc
just enter the following command on command prompt after launching the app:
adb shell dumpsys window windows | find "mCurrentFocus"
if executing the command on linux terminal replace find by grep
If you know the app id of the package (like org.mozilla.firefox), it is easy.
First to get the path of actual package file of the appId,
$ adb shell pm list packages -f com.google.android.apps.inbox
package:/data/app/com.google.android.apps.inbox-1/base.apk=com.google.android.apps.inbox
Now you can do some grep|sed magic to extract the path : /data/app/com.google.android.apps.inbox-1/base.apk
After that aapt tool comes in handy :
$ adb shell aapt dump badging /data/app/com.google.android.apps.inbox-1/base.apk
...
application-label:'Inbox'
application-label-hi:'Inbox'
application-label-ru:'Inbox'
...
Again some grep magic to get the Label.
A shell script to accomplish this:
#!/bin/bash
# Remove whitespace
function remWS {
if [ -z "${1}" ]; then
cat | tr -d '[:space:]'
else
echo "${1}" | tr -d '[:space:]'
fi
}
for pkg in $(adb shell pm list packages -3 | cut -d':' -f2); do
apk_loc="$(adb shell pm path $(remWS $pkg) | cut -d':' -f2 | remWS)"
apk_name="$(adb shell aapt dump badging $apk_loc | pcregrep -o1 $'application-label:\'(.+)\'' | remWS)"
apk_info="$(adb shell aapt dump badging $apk_loc | pcregrep -o1 '\b(package: .+)')"
echo "$apk_name v$(echo $apk_info | pcregrep -io1 -e $'\\bversionName=\'(.+?)\'')"
done
Inorder to find an app's name (application label), you need to do the following:
(as shown in other answers)
Find the APK path of the app whose name you want to find.
Using aapt command, find the app label.
But devices don't ship with the aapt binary out-of-the-box.
So you will need to install it first. You can download it from here:
https://github.com/Calsign/APDE/tree/master/APDE/src/main/assets/aapt-binaries
Check this guide for complete steps:
How to find an app name using package name through ADB Android?
(Disclaimer: I am the author of that blog post)
This is what I just came up with. It gives a few errors but works well enough for my needs, matching package names to labels.
It pulls copies of all packages into subdirectories of $PWD, so keep that in mind if storage is a concern.
#!/bin/bash
TOOLS=~/Downloads/adt-bundle-linux-x86_64-20130717/sdk/build-tools/19.1.0
AAPT=$TOOLS/aapt
PMLIST=adb_shell_pm_list_packages_-f.txt
TEMP=$(echo $(adb shell mktemp -d -p /data/local/tmp) | sed 's/\r//')
mkdir -p packages
[ -f $PMLIST ] || eval $(echo $(basename $PMLIST) | tr '_' ' ') > $PMLIST
while read line; do
package=${line##*:}
apk=${package%%=*}
name=${package#*=}
copy=packages$apk
mkdir -p $(dirname $copy)
if [ ! -s $copy ]; then # copy it because `adb pull` doesn't see /mnt/expand/
adb shell cp -f $apk $TEMP/copy.apk
adb pull $TEMP/copy.apk $copy
fi
label=$($AAPT dump badging $copy || echo ERROR in $copy >&2 | \
sed -n 's/^application-label:\(.\)\(.*\)\1$/\2/p')
echo $name:$label
done < <(sed 's/\r//' $PMLIST)
adb shell rm -rf $TEMP
So I extremely grateful to jcomeau_ictx for providing the info on how to extract application-label info from apk and the idea to pull apk from phone directly!
However I had to make several alteration to script it self:
while read line; do done are breaking as a result of commands within while loop interacting with stdin/stdout and as a result while loop runs only once and then stops, as it is discussed in While loop stops reading after the first line in Bash - the comment from cmo I used solution provided and switched while loop to use unused file descriptor number 9.
All that the script really need is a package name and adb shell pm list packages -f is really excessive so I changed it to expect a file with packages list only and provided example on how one can get one from adb.
jcomeau_ictx script variant do not take in to account that some packages may have multiple apk associated with them which breaks the script.
And the least and last, I made every variable to start with underscore, it's just something that makes it easier to read script.
So here another variant of the same script:
#!/bin/bash
_TOOLS=/opt/android-sdk-update-manager/build-tools/29.0.3
_AAPT=${_TOOLS}/aapt
#adb shell pm list packages --user 0 | sed -e 's|^package:||' | sort >./packages_list.txt
_PMLIST=packages_list.txt
rm ./packages_list_with_names.txt
_TEMP=$(echo $(adb shell mktemp -d -p /data/local/tmp) | sed 's/\r//')
mkdir -p packages
[ -f ${_PMLIST} ] || eval $(echo $(basename ${_PMLIST}) | tr '_' ' ') > ${_PMLIST}
while read -u 9 _line; do
_package=${_line##*:}
_apkpath=$(adb shell pm path ${_package} | sed -e 's|^package:||' | head -n 1)
_apkfilename=$(basename "${_apkpath}")
adb shell cp -f ${_apkpath} ${_TEMP}/copy.apk
adb pull ${_TEMP}/copy.apk ./packages
_name=$(${_AAPT} dump badging ./packages/copy.apk | sed -n 's|^application-label:\(.\)\(.*\)\1$|\2|p' )
#'
echo "${_package} - ${_name}" >>./packages_list_with_names.txt
done 9< ${_PMLIST}
adb shell rm -rf $TEMP

How to pass the return value of a command as a parameter to another command in bash?

I'm writing a shell script in Android.
I need to determine the name of the owner of an app's data directory, assign it to a variable and change the owner of another directory (which happens to be a sub-directory of the first directory) to the owner of the first directory using a variable.
#assign the owner of the com.netflix.mediaclient to $user variable
user=`su -c "ls -l /data/data | grep com.netflix.mediaclient | cut -f2 -d' '"`
#change the owner of the shared_prefs directory to the value in $user variable
su -c 'busybox chown -R '$user:$user' /data/data/com.netflix.mediaclient/shared_prefs'
This and many other variations of the syntax of the second line does not work.
#using double quotes
su -c 'busybox chown -R "$user:$user" /data/data/com.netflix.mediaclient/shared_prefs'
#using no quotes
su -c 'busybox chown -R $user:$user /data/data/com.netflix.mediaclient/shared_prefs'
Use stat to find the owner and put $user:$user inside double quotes
user="$( busybox stat -c '%U' /data/data/com.netflix.mediaclient )"
busybox chown -R "$user:$user" /data/data/com.netflix.mediaclient/shared_prefs
Why '$user:$user' will not work?
Anything put inside the singles quotes are taken literally by bash and variable expansion will not occur.

Using ADB : How to get file modification date command

I have following command to get file date with time
adb shell ls -l /sdcard/sample.txt
Result of this command :
-rwxrwxr-x system sdcard_rw 676 2013-09-24 11:23 sample.txt
Now, I want 2013-09-24 11:23 time with seconds as well. like 2013-09-24 11:23:11
How I will get file modification date including seconds.
Thanks.. :)
If you have a stat command:
stat -c %y /sdcard/sample.txt
Seconds since Epoch
stat -c %Y /sdcard/sample.txt
By tweaking the code posted, I was able to get it worked using this command :
adb -d shell ls -l time-style="+%Y-%m-%d %H:%M:%S" path_to_file_in_device
The output is :
-rw-rw-r-- root sdcard_rw 22357385 2013-09-25 22:42 launcher.apk
ls -l --time-style="+%Y-%m-%d %H:%M:%S" /sdcard/sample.txt

How to get the PID from PS command output in Android shell

Can anyone tell how can I get the PID from the output of PS command in Android shell.
For example from the output:
u0_a51 20240 38 132944 22300 ffffffff 40037ebc S com.example.poc_service
pid value 20240 is to be got. I tried
ps -ef | grep com.example.poc_service
but to no avail. Also pgrep is not being recognized.
If you have shell access in Android you can also use pidof:
# pidof com.example.poc_service
20240
However, be careful as there may be multiple processes matching...
Its pretty nasty but it works:
for pid in `ls /proc`; do
cmd=`cat $pid/cmdline 2> /dev/null`;
if [ "X$cmd" == "Xcom.example.poc_service" ]; then
echo $pid;
fi
done
or as one line:
for pid in `ls /proc`; do cmd=`cat $pid/cmdline 2> /dev/null`; if [ "X$cmd" == "X/system/bin/mm-qcamera-daemon" ]; then echo $pid; fi done
Neither grep, egrep, fgrep, rgrep is available in Android.
If you are working on Unix, Linux, Mac or Cygwin, you can pipe the output of adb shell command to get the result you want.
$ adb shell ps |grep settings
system 23846 71 111996 22676 ffffffff 00000000 S com.android.settings
$ adb shell ps |grep settings |awk '{print $2}'
23846

Shell script search & replace in text file

I have to edit text file in android shell.
so I type this shell script.
but my Galaxy Nexus does not have Sed, Awk neither.
shell#android:/ $ sed -e "s/old_pattern/new_pattern/g" file_name > modify_file
/system/bin/sh: sed: not found
it doesn't worked.
how can i modify old_pattern to new_pattern in text file.
is it possible in Shell Script?
Edited Shell Script
#!/system/bin/sh
ARGS=4
BAD=65
if [ $# -ne "$ARGS" ]
then
echo "Usage: `basename $0` TARGET_FILE,OLD_PATTERN,NEW_PATTERN,MODIFY_FILE"
exit $BAD
fi
old_pattern=$2
new_pattern=$3
modify_file=$4
if [ -f "$1" ]
then
target_file=$1
else
echo "\"$3\" Does not exist."
exit $BAD
fi
exit 0
Solution :
shell#android:/ $ while read STRING
>do
>echo "${STRING//old_pattern/new_pattern}" >> modify_file_name
>done < target_file_name
shell#android:/ $
It is possible with pure bash, but it will be very slooow on big files.
while read STRING
do
echo "${STRING//old_pattern/new_pattern}" >> modify_file
done < file_name
ps. Oh, I just mentioned that your shell isn't bash. Seems like it is just sh. That won't work with sh.

Categories

Resources