[ASUS ZenFone 8] bnsmb:
This HowTo contains some hints for using TWRP in general and some tips for using CLI commands in TWRP.

I will update this post in case I find something new & useful regarding TWRP and update the history section at the end of this post.

The test environment for the instructions below was

TWRP 3.6.2.12 for the ASUS Zenfone 8

TWRP 3.7.0_12-0 for the ASUS Zenfone 8

Note:

This is NOT a general HowTo for TWRP.

How to test a TWRP image?

It's recommended to boot from the TWRP image file before installing it into the boot or recovery partition of your phone. To do this do

# connect the phone via USB to your PC

# boot the phone into the bootloader - e.g. via
#
adb reboot bootloader

# boot the phone from the TWRP image (this will not change anything on the phone)
#
sudo fastboot boot <twrpimage_file>

If everything works you can install the TWRP image into the boot or recovery partition using the approbiate menu entry in TWRP.

How to connect via adb to an TWRP session?

TWRP supports adb session as user root, therefor an

adb shell

on your PC is sufficient to open an adb shell as user root on the phone booted into TWRP.

In case you get a permission denied error kill the running adb daemon on the PC and restart the adb command. If that does not fix the error disconnect and reconnect the USB cable between the PC and the Phone.

Be aware that TWRP starts the adb daemon twice after booting. Therefor an adb session to the phone started to early after the boot into TWRP might be killed. In this case just open a new adb shell.

How to check if the phone is booted into TWRP ?

Check the value of the property ro.twrp.boot; this property is only set if TWRP is running

e.g:

ASUS_I006D:/ # getprop ro.twrp.boot
1
ASUS_I006D:/ #

How to check if the running TWRP was booted from an TWRP image or from a boot or recovery partition?

Check the value of the property ro.bootmode; the value of this property is "unknown" if TWRP was booted from an image file via "fastboot boot <twrp_image_file>", e.g.

ASUS_I006D:/ # getprop ro.bootmode
unknown
ASUS_I006D:/ #

-> TWRP was booted from an TWRP image file

ASUS_I006D:/ # getprop ro.bootmode
recovery
ASUS_I006D:/ #

-> TWRP was booted from the boot or recovery partition

How to get the version of the running TWRP?

Check the value of the property getprop ro.twrp.version, e.g.:

ASUS_I006D:/ # getprop ro.twrp.version

3.7.0_12-0
ASUS_I006D:/ #

Where is the log file from TWRP?

The logfile with the messages written while booting TWRP is /data/recovery/log.gz.

e.g.

130|ASUS_I006D:/ # ls -ltr /data/recovery/l*gz
-rw------- 1 system system 8022 2022-12-26 14:03 /data/recovery/log.gz
ASUS_I006D:/ #

ASUS_I006D:/ # gzip -cd /data/recovery/log.gz | head -10
Starting TWRP 3.7.0_12-0-d07fdb3c on Sat Jan 10 00:53:15 1970
(pid 403)
I:Lun file '/config/usb_gadget/g1/functions/mass_storage.0/lun.0/file'
PRODUCT_USE_DYNAMIC_PARTITIONS := true
TW_INCLUDE_CRYPTO := true
I:TW_BRIGHTNESS_PATH := /proc/lcd_brightness
I:Found brightness file at '/proc/lcd_brightness'
I:TWFunc::Set_Brightness: Setting brightness control to 420
I:TW_EXCLUDE_ENCRYPTED_BACKUPS := true
I:LANG: en
ASUS_I006D:/ #

The current logfile of the running TWRP is /tmp/recovery.log, e.g.:

ASUS_I006D:/ # ls -l /tmp/recovery.log
-rw-rw-rw- 1 root root 43303 2023-05-19 20:11 /tmp/recovery.log
ASUS_I006D:/ #

Where are the settings from TWRP stored?

The settings from TWRP are stored in the file

/data/media/0/TWRP/.twrps

This is a binary file.

Note that on phones with active encryption that file is only accessible after /data is decrypted.

How to edit a file while booted into TWRP?

The editor nano is part of TWRP and can be used to edit files in an adb session.

How to mount a partition while inside TWRP?

Use

twrp mount <mountpoint>

to mount a partition while TWRP is running.

Note that you should use this command with caution in adb sessions because the adb session may be killed after the mount

How to decrypt an encrypted /data partition

Use

twrp decrypt <password>

to decrypt an encrypted /data partition, e.g.

ASUS_I006D:/ # ls /data/media/0
+lnodCAAAAAnYFLObBoWS5ZNb5yVMpze 8KdHMCAAAAQOgtDHq,XdSfFPW+XURA2M KlfTJCAAAAwL93jkH2ugnU4eUG3Zzm7M W9W9SCAAAAgh6lwoP0mNiEK9VIWJoH6L
0nUvhAAAAAQ9ptz+L5qPQrpzcAt4G06K DWk4KDAAAAA,B1xVDmT3dZI87g2C0IS8 M6jcmCAAAAAU,4gn9o0zoBZmkysyXy49 bE7t0CAAAAQ2YuHL9styNaf,iSf2cCO,
4nFjMBAAAAwv2oUczXEpKUDfRIOi+nR5 EcHrdAAAAAglBtbhNWl0NvyX8aW+3CHN O5ZVrDAAAAAlKd7PPpo7CaQmkFbZzKRm haPcnBAAAAw9o2lLF97zpPTaC+byP92s
5yL8gCAAAAgekBDBUi4,W78rJAkgFcMF FA1aZAAAAAg2XNF+M859lpPY,3rUC2cY Vda+LCAAAAwR+g6Tav35+h,Epc8latGR mSKc8BAAAAwm+12sMClHPf+zwtHAeW+Q
8CsF+DAAAAwI3tImfOqIS9aHCQ3S7Cdg JpuFgAAAAAwdEzuQerx4681dzGLjlK3X VvEV4DAAAAgRgoqCfrypNqryZxRACnfr rMJEjAAAAAgwTgx,vA5ewWMVizc2LAos
ASUS_I006D:/ #

ASUS_I006D:/ # twrp decrypt 12345
Attempting to decrypt data partition or user data via command line.
Attempting to decrypt FBE for user 0...
User 0 Decrypted Successfully
Data successfully decrypted
Updating partition details...
...done
ASUS_I006D:/ #

ASUS_I006D:/ # ls /data/media/0
Alarms Android Audiobooks DCIM Documents Download Movies Music Notifications Pictures Podcasts Recordings Ringtones TWRP
ASUS_I006D:/ #

see the TWRP Commandline Guide for the known parameter for this command

Note that existing adb sessions will be stopped after sucessfully executing this command.

How to check if the data partition is encrypted from within a booted TWRP

To check if the data partition is encrypted in a script executed in a running TWRP use code like this:

Spoiler content hidden.

How to install a ZIP file via cli command from within a running TWRP?

Copy the zip file to the phone via adb push and then use

twrp install /path/to/update.zip

to install a ZIP file from within TWRP

Note that the parameter must be a fully qualified filename.

Using this method to update the OS only works in a terminal session on the phone; it does not work in a adb session (at least in my tests until now ...)

What commands are available in TWRP?

Most of the Unix commands are available in TWRP

Be aware that a lot of Unix commands in TWRP are implemented using a general binary called toybox (toybox is something like busybox) and do not support all parameter like the original Linux commands

ASUS_I006D:/ # ls -l /bin/ | wc -l
261
ASUS_I006D:/ # ls -l /bin/ | grep toybox | wc -l
175
ASUS_I006D:/ #

What other TRWP specific commands can be used while in a running TWRP?

Spoiler content hidden.

https://twrp.me/faq/openrecoveryscript.html

and a few others like bootctl (see below) or magiskboot (see below).

How to change the active slot for the next reboot while in TWRP?

Use bootctl, e.g.

ASUS_I006D:/ # bootctl get-current-slot
1
ASUS_I006D:/ # bootctl set-active-boot-slot 0

The usage for the command bootctl is

Spoiler content hidden.

see also How to switch the active slot

How to use Magisk binaries while booted into TWRP

Magisk is another great tool for Android phones -- for details about Magisk see here:

https://github.com/topjohnwu/Magisk

The Magisk binary for manipulating the boot partition, magiskboot, is already part of TWRP. This binary can be used to unpack and repack boot partition (or boot partition images), ramdisks, or compress/decompress files. The binary works without problems while booted into TWRP.

Spoiler content hidden.

If Magisk is installed in the Android OS on the phone and the booted TWRP is able to mount the volume for /data the Magisk binary magisk can also be used in an session in TWRP.

For details see the section Using the magisk binary while the phone is booted into TWRP in the post Some hints for using Magisk on Android phones

How to install TWRP into the boot partition using CLI commands from within a booted TWRP?

Use the script

install_twrp_from_within_twrp.sh

Download from here: http://bnsmb.de/files/public/Android/install_twrp_from_within_twrp.sh

How to install TWRP after an OS Upgrade from within the running Android OS?

see How to Install TWRP into the boot partition again after the installation of an OS update

How to add additional files to an existing TWRP image?

see How to add additional files to an TWRP image

How to install TWRP into the boot partition via script?

see How to install TWRP via script

How to build a TWRP image?

seee How to compile TWRP for the ASUS Zenfone 8

How to install an OS image using the twrp command in a script

see How to install an OS image using the TWRP binary twrp?

How to use TWRP if LineageOS 20.x is installed

As of 26.06.2023 booting the phone from the TWRP recovery image using the command

sudo fastboot boot <twrp_image>

Does not work anymore if LineageOS 20.x is installed on the phone.

see How to use TWRP if LineageOS 20.x is installed for a workaroujnd

Spoiler content hidden.