Skip to main content

A brief look at Termux file management: sharing files between Linux environments and Android

·
Categories Smartphones Termux Tutorials
Tags Termux Freedesktop Specifications
Table of Contents

Manage and access files in Termux APP.

Discussing Termux’s directory structure and techniques for sharing files between Linux environments and Android.

Test environment:

  • Android 14
  • Termux 0.119

1. Termux’s own root directory
#

According to the Termux Wiki, Termux does not comply with the FHS standard, and its directory structure differs from ordinary desktop Linux distributions. Therefore, if your software relies heavily on the standard Linux directory structure, it is safer to consider using proot or chroot to create a Linux container environment.

Termux’s file directory (document directory) is located at /data/data/com.termux/files/

Only Termux can access this directory.

Termux provides two environment variables: $TERMUX_PREFIX and $PREFIX. Both point to the /data/data/com.termux/files/usr directory.

Software installed through pkg in Termux is usually placed in /data/data/com.termux/files/usr/bin

Termux’s home directory is located at /data/data/com.termux/files/home

Without root privileges, Termux cannot access the root directory (/) of the Android system

2. Termux’s home directory
#

Termux’s home directory is located at /data/data/com.termux/files/home. When running commands, the home directory path can be replaced with ~.

Note that Termux has only a single user account (confirm with the whoami command), and no root account. Users also cannot add accounts.

Only Termux can access the home directory. Other apps have to read this directory through Android Scoped Storage. See: Access Termux Directory

Usually there are no files in the Termux home directory, but ls -a can show hidden files

~ $ ls -a
.            .bash_history  .ssh
..           .npm           .termux  storage
.Xauthority  .npmrc         .vnc

Most directories starting with a dot are software configuration files. For example, .termux contains program behavior and fonts.

If the XFCE4 desktop environment is installed, Termux’s home directory will automatically generate directories that conform to the XDG Base Directory standard.

If you want to set the $PATH environment variable, put it in ~/.bashrc or ~/.profile.

If you want a text-based file manager, you can install the ranger package in Termux to conveniently manage files under Termux’s home directory.

3. Termux reading Android internal storage
#

By default, Termux cannot access Android user files outside its own environment.

After running the termux-setup-storage command, Termux can access files in Android internal storage.

To be precise, Termux bind mounts Android’s /sdcard path to ~/storage under the Termux home directory

Android internal storage is mounted to ~/storage under the Termux home directory

Use the ls command to view directories under ~/storage/shared, and you can see all files in Android internal storage

~ $ ls ~/storage/shared/

Documents  Android     Download   Movies     Pictures
DCIM        Music

However, without root privileges, Termux cannot access the Android/data directory.

We can use the cp command to copy files inside Termux to the Android phone’s internal storage

cp ~/file.txt ~/storage/shared/Download/

Likewise, files from the Android phone’s internal storage can be copied to the Termux home directory

cp ~/storage/shared/Download/file.txt ~

Be careful: running rm -rf can delete all files in the Android phone’s internal file storage (except directories requiring root privileges). Operate commands carefully.

Please do not put software or scripts in Android internal storage directories outside Termux and execute them there. Permission problems can easily occur.

4. proot-distro reading Android phone internal storage
#

When running the proot-distro command to enter a Linux container, Termux mounts ~/storage/shared to /sdcard inside the container by default

So inside the proot environment, you can access files in the Android phone’s internal storage from the /sdcard path.

5. Termux accessing SD card files
#

After an SD card or OTG device is inserted into an Android phone, it is usually mounted under /storage

Termux cannot request access to external SD cards and OTG devices; root privileges are required to access that directory.

6. Use the termux-open command to share files with other apps
#

Termux provides the termux-open command, which can open files with other apps.

There is also the xdg-open command. This function is similar to the command of the same name on Linux desktops; both are used to open files with the default program. In the Termux environment, it is actually linked to the termux-open file.

For example, to read a jpg image under the home directory, run the termux-open command. The Android phone’s “open file with… app” menu will pop up, and then you can open the image with a gallery app.

# Open the file directly
termux-open --view test.jpg

# Open in sharing mode
termux-open --send test.jpg

# Always show the app selection list
termux-open --view --chooser test.jpg

The termux-open command can only be used to share a single file. If you want to start another app process from Termux, use the ADB command am start:

am start -n com.android.chrome/com.google.android.apps.chrome.Main

6. Termux using root privileges to access Android system directories
#

When Magisk or KernelSU is installed

Use the sudo command to elevate privileges, and you can access any directory in the Android system

sudo ls /

Can it be used with BusyBox to access the Termux environment? Can Busybox be used in a rooted Android Shell to execute packages installed in the Termux environment?

It is somewhat difficult. I do not recommend doing this; I recommend using chroot directly.

7. Back up files in the Termux environment
#

The best way is to copy them directly to Android internal storage

Having root privileges is even better

See Termux Backup

References
#

Related


Thank you for reading. Public comments are not available on this website. I write to explore ideas honestly, not to chase social engagement or traffic. I would be glad to hear your thoughts after reading the article with care. If you found any errors, technical issues, or would like to share feedback, feel free to contact me via the email listed on the About page.